]> Git Repo - J-linux.git/blob - include/linux/phy.h
e9e85d347587208e8206e33fb2ea17cfa061126d
[J-linux.git] / include / linux / phy.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Framework and drivers for configuring and reading different PHYs
4  * Based on code in sungem_phy.c and (long-removed) gianfar_phy.c
5  *
6  * Author: Andy Fleming
7  *
8  * Copyright (c) 2004 Freescale Semiconductor, Inc.
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/leds.h>
18 #include <linux/linkmode.h>
19 #include <linux/netlink.h>
20 #include <linux/mdio.h>
21 #include <linux/mii.h>
22 #include <linux/mii_timestamper.h>
23 #include <linux/module.h>
24 #include <linux/timer.h>
25 #include <linux/workqueue.h>
26 #include <linux/mod_devicetable.h>
27 #include <linux/u64_stats_sync.h>
28 #include <linux/irqreturn.h>
29 #include <linux/iopoll.h>
30 #include <linux/refcount.h>
31
32 #include <linux/atomic.h>
33
34 #define PHY_DEFAULT_FEATURES    (SUPPORTED_Autoneg | \
35                                  SUPPORTED_TP | \
36                                  SUPPORTED_MII)
37
38 #define PHY_10BT_FEATURES       (SUPPORTED_10baseT_Half | \
39                                  SUPPORTED_10baseT_Full)
40
41 #define PHY_100BT_FEATURES      (SUPPORTED_100baseT_Half | \
42                                  SUPPORTED_100baseT_Full)
43
44 #define PHY_1000BT_FEATURES     (SUPPORTED_1000baseT_Half | \
45                                  SUPPORTED_1000baseT_Full)
46
47 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
48 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
49 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1s_p2mp_features) __ro_after_init;
50 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
51 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
52 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
53 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
54 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
55 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
56 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap1_features) __ro_after_init;
57
58 #define PHY_BASIC_FEATURES ((unsigned long *)&phy_basic_features)
59 #define PHY_BASIC_T1_FEATURES ((unsigned long *)&phy_basic_t1_features)
60 #define PHY_BASIC_T1S_P2MP_FEATURES ((unsigned long *)&phy_basic_t1s_p2mp_features)
61 #define PHY_GBIT_FEATURES ((unsigned long *)&phy_gbit_features)
62 #define PHY_GBIT_FIBRE_FEATURES ((unsigned long *)&phy_gbit_fibre_features)
63 #define PHY_GBIT_ALL_PORTS_FEATURES ((unsigned long *)&phy_gbit_all_ports_features)
64 #define PHY_10GBIT_FEATURES ((unsigned long *)&phy_10gbit_features)
65 #define PHY_10GBIT_FEC_FEATURES ((unsigned long *)&phy_10gbit_fec_features)
66 #define PHY_10GBIT_FULL_FEATURES ((unsigned long *)&phy_10gbit_full_features)
67 #define PHY_EEE_CAP1_FEATURES ((unsigned long *)&phy_eee_cap1_features)
68
69 extern const int phy_basic_ports_array[3];
70 extern const int phy_fibre_port_array[1];
71 extern const int phy_all_ports_features_array[7];
72 extern const int phy_10_100_features_array[4];
73 extern const int phy_basic_t1_features_array[3];
74 extern const int phy_basic_t1s_p2mp_features_array[2];
75 extern const int phy_gbit_features_array[2];
76 extern const int phy_10gbit_features_array[1];
77
78 /*
79  * Set phydev->irq to PHY_POLL if interrupts are not supported,
80  * or not desired for this PHY.  Set to PHY_MAC_INTERRUPT if
81  * the attached MAC driver handles the interrupt
82  */
83 #define PHY_POLL                -1
84 #define PHY_MAC_INTERRUPT       -2
85
86 #define PHY_IS_INTERNAL         0x00000001
87 #define PHY_RST_AFTER_CLK_EN    0x00000002
88 #define PHY_POLL_CABLE_TEST     0x00000004
89 #define PHY_ALWAYS_CALL_SUSPEND 0x00000008
90 #define MDIO_DEVICE_IS_PHY      0x80000000
91
92 /**
93  * enum phy_interface_t - Interface Mode definitions
94  *
95  * @PHY_INTERFACE_MODE_NA: Not Applicable - don't touch
96  * @PHY_INTERFACE_MODE_INTERNAL: No interface, MAC and PHY combined
97  * @PHY_INTERFACE_MODE_MII: Media-independent interface
98  * @PHY_INTERFACE_MODE_GMII: Gigabit media-independent interface
99  * @PHY_INTERFACE_MODE_SGMII: Serial gigabit media-independent interface
100  * @PHY_INTERFACE_MODE_TBI: Ten Bit Interface
101  * @PHY_INTERFACE_MODE_REVMII: Reverse Media Independent Interface
102  * @PHY_INTERFACE_MODE_RMII: Reduced Media Independent Interface
103  * @PHY_INTERFACE_MODE_REVRMII: Reduced Media Independent Interface in PHY role
104  * @PHY_INTERFACE_MODE_RGMII: Reduced gigabit media-independent interface
105  * @PHY_INTERFACE_MODE_RGMII_ID: RGMII with Internal RX+TX delay
106  * @PHY_INTERFACE_MODE_RGMII_RXID: RGMII with Internal RX delay
107  * @PHY_INTERFACE_MODE_RGMII_TXID: RGMII with Internal RX delay
108  * @PHY_INTERFACE_MODE_RTBI: Reduced TBI
109  * @PHY_INTERFACE_MODE_SMII: Serial MII
110  * @PHY_INTERFACE_MODE_XGMII: 10 gigabit media-independent interface
111  * @PHY_INTERFACE_MODE_XLGMII:40 gigabit media-independent interface
112  * @PHY_INTERFACE_MODE_MOCA: Multimedia over Coax
113  * @PHY_INTERFACE_MODE_PSGMII: Penta SGMII
114  * @PHY_INTERFACE_MODE_QSGMII: Quad SGMII
115  * @PHY_INTERFACE_MODE_TRGMII: Turbo RGMII
116  * @PHY_INTERFACE_MODE_100BASEX: 100 BaseX
117  * @PHY_INTERFACE_MODE_1000BASEX: 1000 BaseX
118  * @PHY_INTERFACE_MODE_2500BASEX: 2500 BaseX
119  * @PHY_INTERFACE_MODE_5GBASER: 5G BaseR
120  * @PHY_INTERFACE_MODE_RXAUI: Reduced XAUI
121  * @PHY_INTERFACE_MODE_XAUI: 10 Gigabit Attachment Unit Interface
122  * @PHY_INTERFACE_MODE_10GBASER: 10G BaseR
123  * @PHY_INTERFACE_MODE_25GBASER: 25G BaseR
124  * @PHY_INTERFACE_MODE_USXGMII:  Universal Serial 10GE MII
125  * @PHY_INTERFACE_MODE_10GKR: 10GBASE-KR - with Clause 73 AN
126  * @PHY_INTERFACE_MODE_QUSGMII: Quad Universal SGMII
127  * @PHY_INTERFACE_MODE_1000BASEKX: 1000Base-KX - with Clause 73 AN
128  * @PHY_INTERFACE_MODE_MAX: Book keeping
129  *
130  * Describes the interface between the MAC and PHY.
131  */
132 typedef enum {
133         PHY_INTERFACE_MODE_NA,
134         PHY_INTERFACE_MODE_INTERNAL,
135         PHY_INTERFACE_MODE_MII,
136         PHY_INTERFACE_MODE_GMII,
137         PHY_INTERFACE_MODE_SGMII,
138         PHY_INTERFACE_MODE_TBI,
139         PHY_INTERFACE_MODE_REVMII,
140         PHY_INTERFACE_MODE_RMII,
141         PHY_INTERFACE_MODE_REVRMII,
142         PHY_INTERFACE_MODE_RGMII,
143         PHY_INTERFACE_MODE_RGMII_ID,
144         PHY_INTERFACE_MODE_RGMII_RXID,
145         PHY_INTERFACE_MODE_RGMII_TXID,
146         PHY_INTERFACE_MODE_RTBI,
147         PHY_INTERFACE_MODE_SMII,
148         PHY_INTERFACE_MODE_XGMII,
149         PHY_INTERFACE_MODE_XLGMII,
150         PHY_INTERFACE_MODE_MOCA,
151         PHY_INTERFACE_MODE_PSGMII,
152         PHY_INTERFACE_MODE_QSGMII,
153         PHY_INTERFACE_MODE_TRGMII,
154         PHY_INTERFACE_MODE_100BASEX,
155         PHY_INTERFACE_MODE_1000BASEX,
156         PHY_INTERFACE_MODE_2500BASEX,
157         PHY_INTERFACE_MODE_5GBASER,
158         PHY_INTERFACE_MODE_RXAUI,
159         PHY_INTERFACE_MODE_XAUI,
160         /* 10GBASE-R, XFI, SFI - single lane 10G Serdes */
161         PHY_INTERFACE_MODE_10GBASER,
162         PHY_INTERFACE_MODE_25GBASER,
163         PHY_INTERFACE_MODE_USXGMII,
164         /* 10GBASE-KR - with Clause 73 AN */
165         PHY_INTERFACE_MODE_10GKR,
166         PHY_INTERFACE_MODE_QUSGMII,
167         PHY_INTERFACE_MODE_1000BASEKX,
168         PHY_INTERFACE_MODE_MAX,
169 } phy_interface_t;
170
171 /* PHY interface mode bitmap handling */
172 #define DECLARE_PHY_INTERFACE_MASK(name) \
173         DECLARE_BITMAP(name, PHY_INTERFACE_MODE_MAX)
174
175 static inline void phy_interface_zero(unsigned long *intf)
176 {
177         bitmap_zero(intf, PHY_INTERFACE_MODE_MAX);
178 }
179
180 static inline bool phy_interface_empty(const unsigned long *intf)
181 {
182         return bitmap_empty(intf, PHY_INTERFACE_MODE_MAX);
183 }
184
185 static inline void phy_interface_and(unsigned long *dst, const unsigned long *a,
186                                      const unsigned long *b)
187 {
188         bitmap_and(dst, a, b, PHY_INTERFACE_MODE_MAX);
189 }
190
191 static inline void phy_interface_or(unsigned long *dst, const unsigned long *a,
192                                     const unsigned long *b)
193 {
194         bitmap_or(dst, a, b, PHY_INTERFACE_MODE_MAX);
195 }
196
197 static inline void phy_interface_set_rgmii(unsigned long *intf)
198 {
199         __set_bit(PHY_INTERFACE_MODE_RGMII, intf);
200         __set_bit(PHY_INTERFACE_MODE_RGMII_ID, intf);
201         __set_bit(PHY_INTERFACE_MODE_RGMII_RXID, intf);
202         __set_bit(PHY_INTERFACE_MODE_RGMII_TXID, intf);
203 }
204
205 /*
206  * phy_supported_speeds - return all speeds currently supported by a PHY device
207  */
208 unsigned int phy_supported_speeds(struct phy_device *phy,
209                                       unsigned int *speeds,
210                                       unsigned int size);
211
212 /**
213  * phy_modes - map phy_interface_t enum to device tree binding of phy-mode
214  * @interface: enum phy_interface_t value
215  *
216  * Description: maps enum &phy_interface_t defined in this file
217  * into the device tree binding of 'phy-mode', so that Ethernet
218  * device driver can get PHY interface from device tree.
219  */
220 static inline const char *phy_modes(phy_interface_t interface)
221 {
222         switch (interface) {
223         case PHY_INTERFACE_MODE_NA:
224                 return "";
225         case PHY_INTERFACE_MODE_INTERNAL:
226                 return "internal";
227         case PHY_INTERFACE_MODE_MII:
228                 return "mii";
229         case PHY_INTERFACE_MODE_GMII:
230                 return "gmii";
231         case PHY_INTERFACE_MODE_SGMII:
232                 return "sgmii";
233         case PHY_INTERFACE_MODE_TBI:
234                 return "tbi";
235         case PHY_INTERFACE_MODE_REVMII:
236                 return "rev-mii";
237         case PHY_INTERFACE_MODE_RMII:
238                 return "rmii";
239         case PHY_INTERFACE_MODE_REVRMII:
240                 return "rev-rmii";
241         case PHY_INTERFACE_MODE_RGMII:
242                 return "rgmii";
243         case PHY_INTERFACE_MODE_RGMII_ID:
244                 return "rgmii-id";
245         case PHY_INTERFACE_MODE_RGMII_RXID:
246                 return "rgmii-rxid";
247         case PHY_INTERFACE_MODE_RGMII_TXID:
248                 return "rgmii-txid";
249         case PHY_INTERFACE_MODE_RTBI:
250                 return "rtbi";
251         case PHY_INTERFACE_MODE_SMII:
252                 return "smii";
253         case PHY_INTERFACE_MODE_XGMII:
254                 return "xgmii";
255         case PHY_INTERFACE_MODE_XLGMII:
256                 return "xlgmii";
257         case PHY_INTERFACE_MODE_MOCA:
258                 return "moca";
259         case PHY_INTERFACE_MODE_PSGMII:
260                 return "psgmii";
261         case PHY_INTERFACE_MODE_QSGMII:
262                 return "qsgmii";
263         case PHY_INTERFACE_MODE_TRGMII:
264                 return "trgmii";
265         case PHY_INTERFACE_MODE_1000BASEX:
266                 return "1000base-x";
267         case PHY_INTERFACE_MODE_1000BASEKX:
268                 return "1000base-kx";
269         case PHY_INTERFACE_MODE_2500BASEX:
270                 return "2500base-x";
271         case PHY_INTERFACE_MODE_5GBASER:
272                 return "5gbase-r";
273         case PHY_INTERFACE_MODE_RXAUI:
274                 return "rxaui";
275         case PHY_INTERFACE_MODE_XAUI:
276                 return "xaui";
277         case PHY_INTERFACE_MODE_10GBASER:
278                 return "10gbase-r";
279         case PHY_INTERFACE_MODE_25GBASER:
280                 return "25gbase-r";
281         case PHY_INTERFACE_MODE_USXGMII:
282                 return "usxgmii";
283         case PHY_INTERFACE_MODE_10GKR:
284                 return "10gbase-kr";
285         case PHY_INTERFACE_MODE_100BASEX:
286                 return "100base-x";
287         case PHY_INTERFACE_MODE_QUSGMII:
288                 return "qusgmii";
289         default:
290                 return "unknown";
291         }
292 }
293
294 #define PHY_INIT_TIMEOUT        100000
295 #define PHY_FORCE_TIMEOUT       10
296
297 #define PHY_MAX_ADDR    32
298
299 /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
300 #define PHY_ID_FMT "%s:%02x"
301
302 #define MII_BUS_ID_SIZE 61
303
304 struct device;
305 struct kernel_hwtstamp_config;
306 struct phylink;
307 struct sfp_bus;
308 struct sfp_upstream_ops;
309 struct sk_buff;
310
311 /**
312  * struct mdio_bus_stats - Statistics counters for MDIO busses
313  * @transfers: Total number of transfers, i.e. @writes + @reads
314  * @errors: Number of MDIO transfers that returned an error
315  * @writes: Number of write transfers
316  * @reads: Number of read transfers
317  * @syncp: Synchronisation for incrementing statistics
318  */
319 struct mdio_bus_stats {
320         u64_stats_t transfers;
321         u64_stats_t errors;
322         u64_stats_t writes;
323         u64_stats_t reads;
324         /* Must be last, add new statistics above */
325         struct u64_stats_sync syncp;
326 };
327
328 /**
329  * struct phy_package_shared - Shared information in PHY packages
330  * @base_addr: Base PHY address of PHY package used to combine PHYs
331  *   in one package and for offset calculation of phy_package_read/write
332  * @refcnt: Number of PHYs connected to this shared data
333  * @flags: Initialization of PHY package
334  * @priv_size: Size of the shared private data @priv
335  * @priv: Driver private data shared across a PHY package
336  *
337  * Represents a shared structure between different phydev's in the same
338  * package, for example a quad PHY. See phy_package_join() and
339  * phy_package_leave().
340  */
341 struct phy_package_shared {
342         u8 base_addr;
343         refcount_t refcnt;
344         unsigned long flags;
345         size_t priv_size;
346
347         /* private data pointer */
348         /* note that this pointer is shared between different phydevs and
349          * the user has to take care of appropriate locking. It is allocated
350          * and freed automatically by phy_package_join() and
351          * phy_package_leave().
352          */
353         void *priv;
354 };
355
356 /* used as bit number in atomic bitops */
357 #define PHY_SHARED_F_INIT_DONE  0
358 #define PHY_SHARED_F_PROBE_DONE 1
359
360 /**
361  * struct mii_bus - Represents an MDIO bus
362  *
363  * @owner: Who owns this device
364  * @name: User friendly name for this MDIO device, or driver name
365  * @id: Unique identifier for this bus, typical from bus hierarchy
366  * @priv: Driver private data
367  *
368  * The Bus class for PHYs.  Devices which provide access to
369  * PHYs should register using this structure
370  */
371 struct mii_bus {
372         struct module *owner;
373         const char *name;
374         char id[MII_BUS_ID_SIZE];
375         void *priv;
376         /** @read: Perform a read transfer on the bus */
377         int (*read)(struct mii_bus *bus, int addr, int regnum);
378         /** @write: Perform a write transfer on the bus */
379         int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val);
380         /** @read_c45: Perform a C45 read transfer on the bus */
381         int (*read_c45)(struct mii_bus *bus, int addr, int devnum, int regnum);
382         /** @write_c45: Perform a C45 write transfer on the bus */
383         int (*write_c45)(struct mii_bus *bus, int addr, int devnum,
384                          int regnum, u16 val);
385         /** @reset: Perform a reset of the bus */
386         int (*reset)(struct mii_bus *bus);
387
388         /** @stats: Statistic counters per device on the bus */
389         struct mdio_bus_stats stats[PHY_MAX_ADDR];
390
391         /**
392          * @mdio_lock: A lock to ensure that only one thing can read/write
393          * the MDIO bus at a time
394          */
395         struct mutex mdio_lock;
396
397         /** @parent: Parent device of this bus */
398         struct device *parent;
399         /** @state: State of bus structure */
400         enum {
401                 MDIOBUS_ALLOCATED = 1,
402                 MDIOBUS_REGISTERED,
403                 MDIOBUS_UNREGISTERED,
404                 MDIOBUS_RELEASED,
405         } state;
406
407         /** @dev: Kernel device representation */
408         struct device dev;
409
410         /** @mdio_map: list of all MDIO devices on bus */
411         struct mdio_device *mdio_map[PHY_MAX_ADDR];
412
413         /** @phy_mask: PHY addresses to be ignored when probing */
414         u32 phy_mask;
415
416         /** @phy_ignore_ta_mask: PHY addresses to ignore the TA/read failure */
417         u32 phy_ignore_ta_mask;
418
419         /**
420          * @irq: An array of interrupts, each PHY's interrupt at the index
421          * matching its address
422          */
423         int irq[PHY_MAX_ADDR];
424
425         /** @reset_delay_us: GPIO reset pulse width in microseconds */
426         int reset_delay_us;
427         /** @reset_post_delay_us: GPIO reset deassert delay in microseconds */
428         int reset_post_delay_us;
429         /** @reset_gpiod: Reset GPIO descriptor pointer */
430         struct gpio_desc *reset_gpiod;
431
432         /** @shared_lock: protect access to the shared element */
433         struct mutex shared_lock;
434
435         /** @shared: shared state across different PHYs */
436         struct phy_package_shared *shared[PHY_MAX_ADDR];
437 };
438 #define to_mii_bus(d) container_of(d, struct mii_bus, dev)
439
440 struct mii_bus *mdiobus_alloc_size(size_t size);
441
442 /**
443  * mdiobus_alloc - Allocate an MDIO bus structure
444  *
445  * The internal state of the MDIO bus will be set of MDIOBUS_ALLOCATED ready
446  * for the driver to register the bus.
447  */
448 static inline struct mii_bus *mdiobus_alloc(void)
449 {
450         return mdiobus_alloc_size(0);
451 }
452
453 int __mdiobus_register(struct mii_bus *bus, struct module *owner);
454 int __devm_mdiobus_register(struct device *dev, struct mii_bus *bus,
455                             struct module *owner);
456 #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
457 #define devm_mdiobus_register(dev, bus) \
458                 __devm_mdiobus_register(dev, bus, THIS_MODULE)
459
460 void mdiobus_unregister(struct mii_bus *bus);
461 void mdiobus_free(struct mii_bus *bus);
462 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
463 static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
464 {
465         return devm_mdiobus_alloc_size(dev, 0);
466 }
467
468 struct mii_bus *mdio_find_bus(const char *mdio_name);
469 struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr);
470
471 #define PHY_INTERRUPT_DISABLED  false
472 #define PHY_INTERRUPT_ENABLED   true
473
474 /**
475  * enum phy_state - PHY state machine states:
476  *
477  * @PHY_DOWN: PHY device and driver are not ready for anything.  probe
478  * should be called if and only if the PHY is in this state,
479  * given that the PHY device exists.
480  * - PHY driver probe function will set the state to @PHY_READY
481  *
482  * @PHY_READY: PHY is ready to send and receive packets, but the
483  * controller is not.  By default, PHYs which do not implement
484  * probe will be set to this state by phy_probe().
485  * - start will set the state to UP
486  *
487  * @PHY_UP: The PHY and attached device are ready to do work.
488  * Interrupts should be started here.
489  * - timer moves to @PHY_NOLINK or @PHY_RUNNING
490  *
491  * @PHY_NOLINK: PHY is up, but not currently plugged in.
492  * - irq or timer will set @PHY_RUNNING if link comes back
493  * - phy_stop moves to @PHY_HALTED
494  *
495  * @PHY_RUNNING: PHY is currently up, running, and possibly sending
496  * and/or receiving packets
497  * - irq or timer will set @PHY_NOLINK if link goes down
498  * - phy_stop moves to @PHY_HALTED
499  *
500  * @PHY_CABLETEST: PHY is performing a cable test. Packet reception/sending
501  * is not expected to work, carrier will be indicated as down. PHY will be
502  * poll once per second, or on interrupt for it current state.
503  * Once complete, move to UP to restart the PHY.
504  * - phy_stop aborts the running test and moves to @PHY_HALTED
505  *
506  * @PHY_HALTED: PHY is up, but no polling or interrupts are done.
507  * - phy_start moves to @PHY_UP
508  *
509  * @PHY_ERROR: PHY is up, but is in an error state.
510  * - phy_stop moves to @PHY_HALTED
511  */
512 enum phy_state {
513         PHY_DOWN = 0,
514         PHY_READY,
515         PHY_HALTED,
516         PHY_ERROR,
517         PHY_UP,
518         PHY_RUNNING,
519         PHY_NOLINK,
520         PHY_CABLETEST,
521 };
522
523 #define MDIO_MMD_NUM 32
524
525 /**
526  * struct phy_c45_device_ids - 802.3-c45 Device Identifiers
527  * @devices_in_package: IEEE 802.3 devices in package register value.
528  * @mmds_present: bit vector of MMDs present.
529  * @device_ids: The device identifer for each present device.
530  */
531 struct phy_c45_device_ids {
532         u32 devices_in_package;
533         u32 mmds_present;
534         u32 device_ids[MDIO_MMD_NUM];
535 };
536
537 struct macsec_context;
538 struct macsec_ops;
539
540 /**
541  * struct phy_device - An instance of a PHY
542  *
543  * @mdio: MDIO bus this PHY is on
544  * @drv: Pointer to the driver for this PHY instance
545  * @devlink: Create a link between phy dev and mac dev, if the external phy
546  *           used by current mac interface is managed by another mac interface.
547  * @phy_id: UID for this device found during discovery
548  * @c45_ids: 802.3-c45 Device Identifiers if is_c45.
549  * @is_c45:  Set to true if this PHY uses clause 45 addressing.
550  * @is_internal: Set to true if this PHY is internal to a MAC.
551  * @is_pseudo_fixed_link: Set to true if this PHY is an Ethernet switch, etc.
552  * @is_gigabit_capable: Set to true if PHY supports 1000Mbps
553  * @has_fixups: Set to true if this PHY has fixups/quirks.
554  * @suspended: Set to true if this PHY has been suspended successfully.
555  * @suspended_by_mdio_bus: Set to true if this PHY was suspended by MDIO bus.
556  * @sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
557  * @loopback_enabled: Set true if this PHY has been loopbacked successfully.
558  * @downshifted_rate: Set true if link speed has been downshifted.
559  * @is_on_sfp_module: Set true if PHY is located on an SFP module.
560  * @mac_managed_pm: Set true if MAC driver takes of suspending/resuming PHY
561  * @wol_enabled: Set to true if the PHY or the attached MAC have Wake-on-LAN
562  *               enabled.
563  * @state: State of the PHY for management purposes
564  * @dev_flags: Device-specific flags used by the PHY driver.
565  *
566  *      - Bits [15:0] are free to use by the PHY driver to communicate
567  *        driver specific behavior.
568  *      - Bits [23:16] are currently reserved for future use.
569  *      - Bits [31:24] are reserved for defining generic
570  *        PHY driver behavior.
571  * @irq: IRQ number of the PHY's interrupt (-1 if none)
572  * @phy_timer: The timer for handling the state machine
573  * @phylink: Pointer to phylink instance for this PHY
574  * @sfp_bus_attached: Flag indicating whether the SFP bus has been attached
575  * @sfp_bus: SFP bus attached to this PHY's fiber port
576  * @attached_dev: The attached enet driver's device instance ptr
577  * @adjust_link: Callback for the enet controller to respond to changes: in the
578  *               link state.
579  * @phy_link_change: Callback for phylink for notification of link change
580  * @macsec_ops: MACsec offloading ops.
581  *
582  * @speed: Current link speed
583  * @duplex: Current duplex
584  * @port: Current port
585  * @pause: Current pause
586  * @asym_pause: Current asymmetric pause
587  * @supported: Combined MAC/PHY supported linkmodes
588  * @advertising: Currently advertised linkmodes
589  * @adv_old: Saved advertised while power saving for WoL
590  * @supported_eee: supported PHY EEE linkmodes
591  * @advertising_eee: Currently advertised EEE linkmodes
592  * @eee_enabled: Flag indicating whether the EEE feature is enabled
593  * @lp_advertising: Current link partner advertised linkmodes
594  * @host_interfaces: PHY interface modes supported by host
595  * @eee_broken_modes: Energy efficient ethernet modes which should be prohibited
596  * @autoneg: Flag autoneg being used
597  * @rate_matching: Current rate matching mode
598  * @link: Current link state
599  * @autoneg_complete: Flag auto negotiation of the link has completed
600  * @mdix: Current crossover
601  * @mdix_ctrl: User setting of crossover
602  * @pma_extable: Cached value of PMA/PMD Extended Abilities Register
603  * @interrupts: Flag interrupts have been enabled
604  * @irq_suspended: Flag indicating PHY is suspended and therefore interrupt
605  *                 handling shall be postponed until PHY has resumed
606  * @irq_rerun: Flag indicating interrupts occurred while PHY was suspended,
607  *             requiring a rerun of the interrupt handler after resume
608  * @interface: enum phy_interface_t value
609  * @possible_interfaces: bitmap if interface modes that the attached PHY
610  *                       will switch between depending on media speed.
611  * @skb: Netlink message for cable diagnostics
612  * @nest: Netlink nest used for cable diagnostics
613  * @ehdr: nNtlink header for cable diagnostics
614  * @phy_led_triggers: Array of LED triggers
615  * @phy_num_led_triggers: Number of triggers in @phy_led_triggers
616  * @led_link_trigger: LED trigger for link up/down
617  * @last_triggered: last LED trigger for link speed
618  * @leds: list of PHY LED structures
619  * @master_slave_set: User requested master/slave configuration
620  * @master_slave_get: Current master/slave advertisement
621  * @master_slave_state: Current master/slave configuration
622  * @mii_ts: Pointer to time stamper callbacks
623  * @psec: Pointer to Power Sourcing Equipment control struct
624  * @lock:  Mutex for serialization access to PHY
625  * @state_queue: Work queue for state machine
626  * @link_down_events: Number of times link was lost
627  * @shared: Pointer to private data shared by phys in one package
628  * @priv: Pointer to driver private data
629  *
630  * interrupts currently only supports enabled or disabled,
631  * but could be changed in the future to support enabling
632  * and disabling specific interrupts
633  *
634  * Contains some infrastructure for polling and interrupt
635  * handling, as well as handling shifts in PHY hardware state
636  */
637 struct phy_device {
638         struct mdio_device mdio;
639
640         /* Information about the PHY type */
641         /* And management functions */
642         struct phy_driver *drv;
643
644         struct device_link *devlink;
645
646         u32 phy_id;
647
648         struct phy_c45_device_ids c45_ids;
649         unsigned is_c45:1;
650         unsigned is_internal:1;
651         unsigned is_pseudo_fixed_link:1;
652         unsigned is_gigabit_capable:1;
653         unsigned has_fixups:1;
654         unsigned suspended:1;
655         unsigned suspended_by_mdio_bus:1;
656         unsigned sysfs_links:1;
657         unsigned loopback_enabled:1;
658         unsigned downshifted_rate:1;
659         unsigned is_on_sfp_module:1;
660         unsigned mac_managed_pm:1;
661         unsigned wol_enabled:1;
662
663         unsigned autoneg:1;
664         /* The most recently read link state */
665         unsigned link:1;
666         unsigned autoneg_complete:1;
667
668         /* Interrupts are enabled */
669         unsigned interrupts:1;
670         unsigned irq_suspended:1;
671         unsigned irq_rerun:1;
672
673         int rate_matching;
674
675         enum phy_state state;
676
677         u32 dev_flags;
678
679         phy_interface_t interface;
680         DECLARE_PHY_INTERFACE_MASK(possible_interfaces);
681
682         /*
683          * forced speed & duplex (no autoneg)
684          * partner speed & duplex & pause (autoneg)
685          */
686         int speed;
687         int duplex;
688         int port;
689         int pause;
690         int asym_pause;
691         u8 master_slave_get;
692         u8 master_slave_set;
693         u8 master_slave_state;
694
695         /* Union of PHY and Attached devices' supported link modes */
696         /* See ethtool.h for more info */
697         __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
698         __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
699         __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
700         /* used with phy_speed_down */
701         __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
702         /* used for eee validation */
703         __ETHTOOL_DECLARE_LINK_MODE_MASK(supported_eee);
704         __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising_eee);
705         bool eee_enabled;
706
707         /* Host supported PHY interface types. Should be ignored if empty. */
708         DECLARE_PHY_INTERFACE_MASK(host_interfaces);
709
710         /* Energy efficient ethernet modes which should be prohibited */
711         u32 eee_broken_modes;
712
713 #ifdef CONFIG_LED_TRIGGER_PHY
714         struct phy_led_trigger *phy_led_triggers;
715         unsigned int phy_num_led_triggers;
716         struct phy_led_trigger *last_triggered;
717
718         struct phy_led_trigger *led_link_trigger;
719 #endif
720         struct list_head leds;
721
722         /*
723          * Interrupt number for this PHY
724          * -1 means no interrupt
725          */
726         int irq;
727
728         /* private data pointer */
729         /* For use by PHYs to maintain extra state */
730         void *priv;
731
732         /* shared data pointer */
733         /* For use by PHYs inside the same package that need a shared state. */
734         struct phy_package_shared *shared;
735
736         /* Reporting cable test results */
737         struct sk_buff *skb;
738         void *ehdr;
739         struct nlattr *nest;
740
741         /* Interrupt and Polling infrastructure */
742         struct delayed_work state_queue;
743
744         struct mutex lock;
745
746         /* This may be modified under the rtnl lock */
747         bool sfp_bus_attached;
748         struct sfp_bus *sfp_bus;
749         struct phylink *phylink;
750         struct net_device *attached_dev;
751         struct mii_timestamper *mii_ts;
752         struct pse_control *psec;
753
754         u8 mdix;
755         u8 mdix_ctrl;
756
757         int pma_extable;
758
759         unsigned int link_down_events;
760
761         void (*phy_link_change)(struct phy_device *phydev, bool up);
762         void (*adjust_link)(struct net_device *dev);
763
764 #if IS_ENABLED(CONFIG_MACSEC)
765         /* MACsec management functions */
766         const struct macsec_ops *macsec_ops;
767 #endif
768 };
769
770 /* Generic phy_device::dev_flags */
771 #define PHY_F_NO_IRQ            0x80000000
772
773 static inline struct phy_device *to_phy_device(const struct device *dev)
774 {
775         return container_of(to_mdio_device(dev), struct phy_device, mdio);
776 }
777
778 /**
779  * struct phy_tdr_config - Configuration of a TDR raw test
780  *
781  * @first: Distance for first data collection point
782  * @last: Distance for last data collection point
783  * @step: Step between data collection points
784  * @pair: Bitmap of cable pairs to collect data for
785  *
786  * A structure containing possible configuration parameters
787  * for a TDR cable test. The driver does not need to implement
788  * all the parameters, but should report what is actually used.
789  * All distances are in centimeters.
790  */
791 struct phy_tdr_config {
792         u32 first;
793         u32 last;
794         u32 step;
795         s8 pair;
796 };
797 #define PHY_PAIR_ALL -1
798
799 /**
800  * struct phy_plca_cfg - Configuration of the PLCA (Physical Layer Collision
801  * Avoidance) Reconciliation Sublayer.
802  *
803  * @version: read-only PLCA register map version. -1 = not available. Ignored
804  *   when setting the configuration. Format is the same as reported by the PLCA
805  *   IDVER register (31.CA00). -1 = not available.
806  * @enabled: PLCA configured mode (enabled/disabled). -1 = not available / don't
807  *   set. 0 = disabled, anything else = enabled.
808  * @node_id: the PLCA local node identifier. -1 = not available / don't set.
809  *   Allowed values [0 .. 254]. 255 = node disabled.
810  * @node_cnt: the PLCA node count (maximum number of nodes having a TO). Only
811  *   meaningful for the coordinator (node_id = 0). -1 = not available / don't
812  *   set. Allowed values [1 .. 255].
813  * @to_tmr: The value of the PLCA to_timer in bit-times, which determines the
814  *   PLCA transmit opportunity window opening. See IEEE802.3 Clause 148 for
815  *   more details. The to_timer shall be set equal over all nodes.
816  *   -1 = not available / don't set. Allowed values [0 .. 255].
817  * @burst_cnt: controls how many additional frames a node is allowed to send in
818  *   single transmit opportunity (TO). The default value of 0 means that the
819  *   node is allowed exactly one frame per TO. A value of 1 allows two frames
820  *   per TO, and so on. -1 = not available / don't set.
821  *   Allowed values [0 .. 255].
822  * @burst_tmr: controls how many bit times to wait for the MAC to send a new
823  *   frame before interrupting the burst. This value should be set to a value
824  *   greater than the MAC inter-packet gap (which is typically 96 bits).
825  *   -1 = not available / don't set. Allowed values [0 .. 255].
826  *
827  * A structure containing configuration parameters for setting/getting the PLCA
828  * RS configuration. The driver does not need to implement all the parameters,
829  * but should report what is actually used.
830  */
831 struct phy_plca_cfg {
832         int version;
833         int enabled;
834         int node_id;
835         int node_cnt;
836         int to_tmr;
837         int burst_cnt;
838         int burst_tmr;
839 };
840
841 /**
842  * struct phy_plca_status - Status of the PLCA (Physical Layer Collision
843  * Avoidance) Reconciliation Sublayer.
844  *
845  * @pst: The PLCA status as reported by the PST bit in the PLCA STATUS
846  *      register(31.CA03), indicating BEACON activity.
847  *
848  * A structure containing status information of the PLCA RS configuration.
849  * The driver does not need to implement all the parameters, but should report
850  * what is actually used.
851  */
852 struct phy_plca_status {
853         bool pst;
854 };
855
856 /**
857  * struct phy_led: An LED driven by the PHY
858  *
859  * @list: List of LEDs
860  * @phydev: PHY this LED is attached to
861  * @led_cdev: Standard LED class structure
862  * @index: Number of the LED
863  */
864 struct phy_led {
865         struct list_head list;
866         struct phy_device *phydev;
867         struct led_classdev led_cdev;
868         u8 index;
869 };
870
871 #define to_phy_led(d) container_of(d, struct phy_led, led_cdev)
872
873 /**
874  * struct phy_driver - Driver structure for a particular PHY type
875  *
876  * @mdiodrv: Data common to all MDIO devices
877  * @phy_id: The result of reading the UID registers of this PHY
878  *   type, and ANDing them with the phy_id_mask.  This driver
879  *   only works for PHYs with IDs which match this field
880  * @name: The friendly name of this PHY type
881  * @phy_id_mask: Defines the important bits of the phy_id
882  * @features: A mandatory list of features (speed, duplex, etc)
883  *   supported by this PHY
884  * @flags: A bitfield defining certain other features this PHY
885  *   supports (like interrupts)
886  * @driver_data: Static driver data
887  *
888  * All functions are optional. If config_aneg or read_status
889  * are not implemented, the phy core uses the genphy versions.
890  * Note that none of these functions should be called from
891  * interrupt time. The goal is for the bus read/write functions
892  * to be able to block when the bus transaction is happening,
893  * and be freed up by an interrupt (The MPC85xx has this ability,
894  * though it is not currently supported in the driver).
895  */
896 struct phy_driver {
897         struct mdio_driver_common mdiodrv;
898         u32 phy_id;
899         char *name;
900         u32 phy_id_mask;
901         const unsigned long * const features;
902         u32 flags;
903         const void *driver_data;
904
905         /**
906          * @soft_reset: Called to issue a PHY software reset
907          */
908         int (*soft_reset)(struct phy_device *phydev);
909
910         /**
911          * @config_init: Called to initialize the PHY,
912          * including after a reset
913          */
914         int (*config_init)(struct phy_device *phydev);
915
916         /**
917          * @probe: Called during discovery.  Used to set
918          * up device-specific structures, if any
919          */
920         int (*probe)(struct phy_device *phydev);
921
922         /**
923          * @get_features: Probe the hardware to determine what
924          * abilities it has.  Should only set phydev->supported.
925          */
926         int (*get_features)(struct phy_device *phydev);
927
928         /**
929          * @get_rate_matching: Get the supported type of rate matching for a
930          * particular phy interface. This is used by phy consumers to determine
931          * whether to advertise lower-speed modes for that interface. It is
932          * assumed that if a rate matching mode is supported on an interface,
933          * then that interface's rate can be adapted to all slower link speeds
934          * supported by the phy. If the interface is not supported, this should
935          * return %RATE_MATCH_NONE.
936          */
937         int (*get_rate_matching)(struct phy_device *phydev,
938                                    phy_interface_t iface);
939
940         /* PHY Power Management */
941         /** @suspend: Suspend the hardware, saving state if needed */
942         int (*suspend)(struct phy_device *phydev);
943         /** @resume: Resume the hardware, restoring state if needed */
944         int (*resume)(struct phy_device *phydev);
945
946         /**
947          * @config_aneg: Configures the advertisement and resets
948          * autonegotiation if phydev->autoneg is on,
949          * forces the speed to the current settings in phydev
950          * if phydev->autoneg is off
951          */
952         int (*config_aneg)(struct phy_device *phydev);
953
954         /** @aneg_done: Determines the auto negotiation result */
955         int (*aneg_done)(struct phy_device *phydev);
956
957         /** @read_status: Determines the negotiated speed and duplex */
958         int (*read_status)(struct phy_device *phydev);
959
960         /**
961          * @config_intr: Enables or disables interrupts.
962          * It should also clear any pending interrupts prior to enabling the
963          * IRQs and after disabling them.
964          */
965         int (*config_intr)(struct phy_device *phydev);
966
967         /** @handle_interrupt: Override default interrupt handling */
968         irqreturn_t (*handle_interrupt)(struct phy_device *phydev);
969
970         /** @remove: Clears up any memory if needed */
971         void (*remove)(struct phy_device *phydev);
972
973         /**
974          * @match_phy_device: Returns true if this is a suitable
975          * driver for the given phydev.  If NULL, matching is based on
976          * phy_id and phy_id_mask.
977          */
978         int (*match_phy_device)(struct phy_device *phydev);
979
980         /**
981          * @set_wol: Some devices (e.g. qnap TS-119P II) require PHY
982          * register changes to enable Wake on LAN, so set_wol is
983          * provided to be called in the ethernet driver's set_wol
984          * function.
985          */
986         int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
987
988         /**
989          * @get_wol: See set_wol, but for checking whether Wake on LAN
990          * is enabled.
991          */
992         void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
993
994         /**
995          * @link_change_notify: Called to inform a PHY device driver
996          * when the core is about to change the link state. This
997          * callback is supposed to be used as fixup hook for drivers
998          * that need to take action when the link state
999          * changes. Drivers are by no means allowed to mess with the
1000          * PHY device structure in their implementations.
1001          */
1002         void (*link_change_notify)(struct phy_device *dev);
1003
1004         /**
1005          * @read_mmd: PHY specific driver override for reading a MMD
1006          * register.  This function is optional for PHY specific
1007          * drivers.  When not provided, the default MMD read function
1008          * will be used by phy_read_mmd(), which will use either a
1009          * direct read for Clause 45 PHYs or an indirect read for
1010          * Clause 22 PHYs.  devnum is the MMD device number within the
1011          * PHY device, regnum is the register within the selected MMD
1012          * device.
1013          */
1014         int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
1015
1016         /**
1017          * @write_mmd: PHY specific driver override for writing a MMD
1018          * register.  This function is optional for PHY specific
1019          * drivers.  When not provided, the default MMD write function
1020          * will be used by phy_write_mmd(), which will use either a
1021          * direct write for Clause 45 PHYs, or an indirect write for
1022          * Clause 22 PHYs.  devnum is the MMD device number within the
1023          * PHY device, regnum is the register within the selected MMD
1024          * device.  val is the value to be written.
1025          */
1026         int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
1027                          u16 val);
1028
1029         /** @read_page: Return the current PHY register page number */
1030         int (*read_page)(struct phy_device *dev);
1031         /** @write_page: Set the current PHY register page number */
1032         int (*write_page)(struct phy_device *dev, int page);
1033
1034         /**
1035          * @module_info: Get the size and type of the eeprom contained
1036          * within a plug-in module
1037          */
1038         int (*module_info)(struct phy_device *dev,
1039                            struct ethtool_modinfo *modinfo);
1040
1041         /**
1042          * @module_eeprom: Get the eeprom information from the plug-in
1043          * module
1044          */
1045         int (*module_eeprom)(struct phy_device *dev,
1046                              struct ethtool_eeprom *ee, u8 *data);
1047
1048         /** @cable_test_start: Start a cable test */
1049         int (*cable_test_start)(struct phy_device *dev);
1050
1051         /**  @cable_test_tdr_start: Start a raw TDR cable test */
1052         int (*cable_test_tdr_start)(struct phy_device *dev,
1053                                     const struct phy_tdr_config *config);
1054
1055         /**
1056          * @cable_test_get_status: Once per second, or on interrupt,
1057          * request the status of the test.
1058          */
1059         int (*cable_test_get_status)(struct phy_device *dev, bool *finished);
1060
1061         /* Get statistics from the PHY using ethtool */
1062         /** @get_sset_count: Number of statistic counters */
1063         int (*get_sset_count)(struct phy_device *dev);
1064         /** @get_strings: Names of the statistic counters */
1065         void (*get_strings)(struct phy_device *dev, u8 *data);
1066         /** @get_stats: Return the statistic counter values */
1067         void (*get_stats)(struct phy_device *dev,
1068                           struct ethtool_stats *stats, u64 *data);
1069
1070         /* Get and Set PHY tunables */
1071         /** @get_tunable: Return the value of a tunable */
1072         int (*get_tunable)(struct phy_device *dev,
1073                            struct ethtool_tunable *tuna, void *data);
1074         /** @set_tunable: Set the value of a tunable */
1075         int (*set_tunable)(struct phy_device *dev,
1076                             struct ethtool_tunable *tuna,
1077                             const void *data);
1078         /** @set_loopback: Set the loopback mood of the PHY */
1079         int (*set_loopback)(struct phy_device *dev, bool enable);
1080         /** @get_sqi: Get the signal quality indication */
1081         int (*get_sqi)(struct phy_device *dev);
1082         /** @get_sqi_max: Get the maximum signal quality indication */
1083         int (*get_sqi_max)(struct phy_device *dev);
1084
1085         /* PLCA RS interface */
1086         /** @get_plca_cfg: Return the current PLCA configuration */
1087         int (*get_plca_cfg)(struct phy_device *dev,
1088                             struct phy_plca_cfg *plca_cfg);
1089         /** @set_plca_cfg: Set the PLCA configuration */
1090         int (*set_plca_cfg)(struct phy_device *dev,
1091                             const struct phy_plca_cfg *plca_cfg);
1092         /** @get_plca_status: Return the current PLCA status info */
1093         int (*get_plca_status)(struct phy_device *dev,
1094                                struct phy_plca_status *plca_st);
1095
1096         /**
1097          * @led_brightness_set: Set a PHY LED brightness. Index
1098          * indicates which of the PHYs led should be set. Value
1099          * follows the standard LED class meaning, e.g. LED_OFF,
1100          * LED_HALF, LED_FULL.
1101          */
1102         int (*led_brightness_set)(struct phy_device *dev,
1103                                   u8 index, enum led_brightness value);
1104
1105         /**
1106          * @led_blink_set: Set a PHY LED brightness.  Index indicates
1107          * which of the PHYs led should be configured to blink. Delays
1108          * are in milliseconds and if both are zero then a sensible
1109          * default should be chosen.  The call should adjust the
1110          * timings in that case and if it can't match the values
1111          * specified exactly.
1112          */
1113         int (*led_blink_set)(struct phy_device *dev, u8 index,
1114                              unsigned long *delay_on,
1115                              unsigned long *delay_off);
1116         /**
1117          * @led_hw_is_supported: Can the HW support the given rules.
1118          * @dev: PHY device which has the LED
1119          * @index: Which LED of the PHY device
1120          * @rules The core is interested in these rules
1121          *
1122          * Return 0 if yes,  -EOPNOTSUPP if not, or an error code.
1123          */
1124         int (*led_hw_is_supported)(struct phy_device *dev, u8 index,
1125                                    unsigned long rules);
1126         /**
1127          * @led_hw_control_set: Set the HW to control the LED
1128          * @dev: PHY device which has the LED
1129          * @index: Which LED of the PHY device
1130          * @rules The rules used to control the LED
1131          *
1132          * Returns 0, or a an error code.
1133          */
1134         int (*led_hw_control_set)(struct phy_device *dev, u8 index,
1135                                   unsigned long rules);
1136         /**
1137          * @led_hw_control_get: Get how the HW is controlling the LED
1138          * @dev: PHY device which has the LED
1139          * @index: Which LED of the PHY device
1140          * @rules Pointer to the rules used to control the LED
1141          *
1142          * Set *@rules to how the HW is currently blinking. Returns 0
1143          * on success, or a error code if the current blinking cannot
1144          * be represented in rules, or some other error happens.
1145          */
1146         int (*led_hw_control_get)(struct phy_device *dev, u8 index,
1147                                   unsigned long *rules);
1148
1149 };
1150 #define to_phy_driver(d) container_of(to_mdio_common_driver(d),         \
1151                                       struct phy_driver, mdiodrv)
1152
1153 #define PHY_ANY_ID "MATCH ANY PHY"
1154 #define PHY_ANY_UID 0xffffffff
1155
1156 #define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 0)
1157 #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
1158 #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
1159
1160 /**
1161  * phy_id_compare - compare @id1 with @id2 taking account of @mask
1162  * @id1: first PHY ID
1163  * @id2: second PHY ID
1164  * @mask: the PHY ID mask, set bits are significant in matching
1165  *
1166  * Return true if the bits from @id1 and @id2 specified by @mask match.
1167  * This uses an equivalent test to (@id & @mask) == (@phy_id & @mask).
1168  */
1169 static inline bool phy_id_compare(u32 id1, u32 id2, u32 mask)
1170 {
1171         return !((id1 ^ id2) & mask);
1172 }
1173
1174 /**
1175  * phydev_id_compare - compare @id with the PHY's Clause 22 ID
1176  * @phydev: the PHY device
1177  * @id: the PHY ID to be matched
1178  *
1179  * Compare the @phydev clause 22 ID with the provided @id and return true or
1180  * false depending whether it matches, using the bound driver mask. The
1181  * @phydev must be bound to a driver.
1182  */
1183 static inline bool phydev_id_compare(struct phy_device *phydev, u32 id)
1184 {
1185         return phy_id_compare(id, phydev->phy_id, phydev->drv->phy_id_mask);
1186 }
1187
1188 /* A Structure for boards to register fixups with the PHY Lib */
1189 struct phy_fixup {
1190         struct list_head list;
1191         char bus_id[MII_BUS_ID_SIZE + 3];
1192         u32 phy_uid;
1193         u32 phy_uid_mask;
1194         int (*run)(struct phy_device *phydev);
1195 };
1196
1197 const char *phy_speed_to_str(int speed);
1198 const char *phy_duplex_to_str(unsigned int duplex);
1199 const char *phy_rate_matching_to_str(int rate_matching);
1200
1201 int phy_interface_num_ports(phy_interface_t interface);
1202
1203 /* A structure for mapping a particular speed and duplex
1204  * combination to a particular SUPPORTED and ADVERTISED value
1205  */
1206 struct phy_setting {
1207         u32 speed;
1208         u8 duplex;
1209         u8 bit;
1210 };
1211
1212 const struct phy_setting *
1213 phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
1214                    bool exact);
1215 size_t phy_speeds(unsigned int *speeds, size_t size,
1216                   unsigned long *mask);
1217 void of_set_phy_supported(struct phy_device *phydev);
1218 void of_set_phy_eee_broken(struct phy_device *phydev);
1219 int phy_speed_down_core(struct phy_device *phydev);
1220
1221 /**
1222  * phy_is_started - Convenience function to check whether PHY is started
1223  * @phydev: The phy_device struct
1224  */
1225 static inline bool phy_is_started(struct phy_device *phydev)
1226 {
1227         return phydev->state >= PHY_UP;
1228 }
1229
1230 void phy_resolve_aneg_pause(struct phy_device *phydev);
1231 void phy_resolve_aneg_linkmode(struct phy_device *phydev);
1232 void phy_check_downshift(struct phy_device *phydev);
1233
1234 /**
1235  * phy_read - Convenience function for reading a given PHY register
1236  * @phydev: the phy_device struct
1237  * @regnum: register number to read
1238  *
1239  * NOTE: MUST NOT be called from interrupt context,
1240  * because the bus read/write functions may wait for an interrupt
1241  * to conclude the operation.
1242  */
1243 static inline int phy_read(struct phy_device *phydev, u32 regnum)
1244 {
1245         return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
1246 }
1247
1248 #define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
1249                                 timeout_us, sleep_before_read) \
1250 ({ \
1251         int __ret, __val; \
1252         __ret = read_poll_timeout(__val = phy_read, val, \
1253                                   __val < 0 || (cond), \
1254                 sleep_us, timeout_us, sleep_before_read, phydev, regnum); \
1255         if (__val < 0) \
1256                 __ret = __val; \
1257         if (__ret) \
1258                 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
1259         __ret; \
1260 })
1261
1262 /**
1263  * __phy_read - convenience function for reading a given PHY register
1264  * @phydev: the phy_device struct
1265  * @regnum: register number to read
1266  *
1267  * The caller must have taken the MDIO bus lock.
1268  */
1269 static inline int __phy_read(struct phy_device *phydev, u32 regnum)
1270 {
1271         return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
1272 }
1273
1274 /**
1275  * phy_write - Convenience function for writing a given PHY register
1276  * @phydev: the phy_device struct
1277  * @regnum: register number to write
1278  * @val: value to write to @regnum
1279  *
1280  * NOTE: MUST NOT be called from interrupt context,
1281  * because the bus read/write functions may wait for an interrupt
1282  * to conclude the operation.
1283  */
1284 static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
1285 {
1286         return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
1287 }
1288
1289 /**
1290  * __phy_write - Convenience function for writing a given PHY register
1291  * @phydev: the phy_device struct
1292  * @regnum: register number to write
1293  * @val: value to write to @regnum
1294  *
1295  * The caller must have taken the MDIO bus lock.
1296  */
1297 static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
1298 {
1299         return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum,
1300                                val);
1301 }
1302
1303 /**
1304  * __phy_modify_changed() - Convenience function for modifying a PHY register
1305  * @phydev: a pointer to a &struct phy_device
1306  * @regnum: register number
1307  * @mask: bit mask of bits to clear
1308  * @set: bit mask of bits to set
1309  *
1310  * Unlocked helper function which allows a PHY register to be modified as
1311  * new register value = (old register value & ~mask) | set
1312  *
1313  * Returns negative errno, 0 if there was no change, and 1 in case of change
1314  */
1315 static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
1316                                        u16 mask, u16 set)
1317 {
1318         return __mdiobus_modify_changed(phydev->mdio.bus, phydev->mdio.addr,
1319                                         regnum, mask, set);
1320 }
1321
1322 /*
1323  * phy_read_mmd - Convenience function for reading a register
1324  * from an MMD on a given PHY.
1325  */
1326 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1327
1328 /**
1329  * phy_read_mmd_poll_timeout - Periodically poll a PHY register until a
1330  *                             condition is met or a timeout occurs
1331  *
1332  * @phydev: The phy_device struct
1333  * @devaddr: The MMD to read from
1334  * @regnum: The register on the MMD to read
1335  * @val: Variable to read the register into
1336  * @cond: Break condition (usually involving @val)
1337  * @sleep_us: Maximum time to sleep between reads in us (0
1338  *            tight-loops).  Should be less than ~20ms since usleep_range
1339  *            is used (see Documentation/timers/timers-howto.rst).
1340  * @timeout_us: Timeout in us, 0 means never timeout
1341  * @sleep_before_read: if it is true, sleep @sleep_us before read.
1342  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
1343  * case, the last read value at @args is stored in @val. Must not
1344  * be called from atomic context if sleep_us or timeout_us are used.
1345  */
1346 #define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
1347                                   sleep_us, timeout_us, sleep_before_read) \
1348 ({ \
1349         int __ret, __val; \
1350         __ret = read_poll_timeout(__val = phy_read_mmd, val, \
1351                                   __val < 0 || (cond), \
1352                                   sleep_us, timeout_us, sleep_before_read, \
1353                                   phydev, devaddr, regnum); \
1354         if (__val < 0) \
1355                 __ret = __val; \
1356         if (__ret) \
1357                 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
1358         __ret; \
1359 })
1360
1361 /*
1362  * __phy_read_mmd - Convenience function for reading a register
1363  * from an MMD on a given PHY.
1364  */
1365 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1366
1367 /*
1368  * phy_write_mmd - Convenience function for writing a register
1369  * on an MMD on a given PHY.
1370  */
1371 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1372
1373 /*
1374  * __phy_write_mmd - Convenience function for writing a register
1375  * on an MMD on a given PHY.
1376  */
1377 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1378
1379 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1380                          u16 set);
1381 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1382                        u16 set);
1383 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1384 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1385
1386 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1387                              u16 mask, u16 set);
1388 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1389                            u16 mask, u16 set);
1390 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1391                      u16 mask, u16 set);
1392 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1393                    u16 mask, u16 set);
1394
1395 /**
1396  * __phy_set_bits - Convenience function for setting bits in a PHY register
1397  * @phydev: the phy_device struct
1398  * @regnum: register number to write
1399  * @val: bits to set
1400  *
1401  * The caller must have taken the MDIO bus lock.
1402  */
1403 static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1404 {
1405         return __phy_modify(phydev, regnum, 0, val);
1406 }
1407
1408 /**
1409  * __phy_clear_bits - Convenience function for clearing bits in a PHY register
1410  * @phydev: the phy_device struct
1411  * @regnum: register number to write
1412  * @val: bits to clear
1413  *
1414  * The caller must have taken the MDIO bus lock.
1415  */
1416 static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum,
1417                                    u16 val)
1418 {
1419         return __phy_modify(phydev, regnum, val, 0);
1420 }
1421
1422 /**
1423  * phy_set_bits - Convenience function for setting bits in a PHY register
1424  * @phydev: the phy_device struct
1425  * @regnum: register number to write
1426  * @val: bits to set
1427  */
1428 static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1429 {
1430         return phy_modify(phydev, regnum, 0, val);
1431 }
1432
1433 /**
1434  * phy_clear_bits - Convenience function for clearing bits in a PHY register
1435  * @phydev: the phy_device struct
1436  * @regnum: register number to write
1437  * @val: bits to clear
1438  */
1439 static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val)
1440 {
1441         return phy_modify(phydev, regnum, val, 0);
1442 }
1443
1444 /**
1445  * __phy_set_bits_mmd - Convenience function for setting bits in a register
1446  * on MMD
1447  * @phydev: the phy_device struct
1448  * @devad: the MMD containing register to modify
1449  * @regnum: register number to modify
1450  * @val: bits to set
1451  *
1452  * The caller must have taken the MDIO bus lock.
1453  */
1454 static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
1455                 u32 regnum, u16 val)
1456 {
1457         return __phy_modify_mmd(phydev, devad, regnum, 0, val);
1458 }
1459
1460 /**
1461  * __phy_clear_bits_mmd - Convenience function for clearing bits in a register
1462  * on MMD
1463  * @phydev: the phy_device struct
1464  * @devad: the MMD containing register to modify
1465  * @regnum: register number to modify
1466  * @val: bits to clear
1467  *
1468  * The caller must have taken the MDIO bus lock.
1469  */
1470 static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1471                 u32 regnum, u16 val)
1472 {
1473         return __phy_modify_mmd(phydev, devad, regnum, val, 0);
1474 }
1475
1476 /**
1477  * phy_set_bits_mmd - Convenience function for setting bits in a register
1478  * on MMD
1479  * @phydev: the phy_device struct
1480  * @devad: the MMD containing register to modify
1481  * @regnum: register number to modify
1482  * @val: bits to set
1483  */
1484 static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
1485                 u32 regnum, u16 val)
1486 {
1487         return phy_modify_mmd(phydev, devad, regnum, 0, val);
1488 }
1489
1490 /**
1491  * phy_clear_bits_mmd - Convenience function for clearing bits in a register
1492  * on MMD
1493  * @phydev: the phy_device struct
1494  * @devad: the MMD containing register to modify
1495  * @regnum: register number to modify
1496  * @val: bits to clear
1497  */
1498 static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1499                 u32 regnum, u16 val)
1500 {
1501         return phy_modify_mmd(phydev, devad, regnum, val, 0);
1502 }
1503
1504 /**
1505  * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
1506  * @phydev: the phy_device struct
1507  *
1508  * NOTE: must be kept in sync with addition/removal of PHY_POLL and
1509  * PHY_MAC_INTERRUPT
1510  */
1511 static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
1512 {
1513         return phydev->irq != PHY_POLL && phydev->irq != PHY_MAC_INTERRUPT;
1514 }
1515
1516 /**
1517  * phy_polling_mode - Convenience function for testing whether polling is
1518  * used to detect PHY status changes
1519  * @phydev: the phy_device struct
1520  */
1521 static inline bool phy_polling_mode(struct phy_device *phydev)
1522 {
1523         if (phydev->state == PHY_CABLETEST)
1524                 if (phydev->drv->flags & PHY_POLL_CABLE_TEST)
1525                         return true;
1526
1527         return phydev->irq == PHY_POLL;
1528 }
1529
1530 /**
1531  * phy_has_hwtstamp - Tests whether a PHY time stamp configuration.
1532  * @phydev: the phy_device struct
1533  */
1534 static inline bool phy_has_hwtstamp(struct phy_device *phydev)
1535 {
1536         return phydev && phydev->mii_ts && phydev->mii_ts->hwtstamp;
1537 }
1538
1539 /**
1540  * phy_has_rxtstamp - Tests whether a PHY supports receive time stamping.
1541  * @phydev: the phy_device struct
1542  */
1543 static inline bool phy_has_rxtstamp(struct phy_device *phydev)
1544 {
1545         return phydev && phydev->mii_ts && phydev->mii_ts->rxtstamp;
1546 }
1547
1548 /**
1549  * phy_has_tsinfo - Tests whether a PHY reports time stamping and/or
1550  * PTP hardware clock capabilities.
1551  * @phydev: the phy_device struct
1552  */
1553 static inline bool phy_has_tsinfo(struct phy_device *phydev)
1554 {
1555         return phydev && phydev->mii_ts && phydev->mii_ts->ts_info;
1556 }
1557
1558 /**
1559  * phy_has_txtstamp - Tests whether a PHY supports transmit time stamping.
1560  * @phydev: the phy_device struct
1561  */
1562 static inline bool phy_has_txtstamp(struct phy_device *phydev)
1563 {
1564         return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp;
1565 }
1566
1567 static inline int phy_hwtstamp(struct phy_device *phydev,
1568                                struct kernel_hwtstamp_config *cfg,
1569                                struct netlink_ext_ack *extack)
1570 {
1571         return phydev->mii_ts->hwtstamp(phydev->mii_ts, cfg, extack);
1572 }
1573
1574 static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb,
1575                                 int type)
1576 {
1577         return phydev->mii_ts->rxtstamp(phydev->mii_ts, skb, type);
1578 }
1579
1580 static inline int phy_ts_info(struct phy_device *phydev,
1581                               struct ethtool_ts_info *tsinfo)
1582 {
1583         return phydev->mii_ts->ts_info(phydev->mii_ts, tsinfo);
1584 }
1585
1586 static inline void phy_txtstamp(struct phy_device *phydev, struct sk_buff *skb,
1587                                 int type)
1588 {
1589         phydev->mii_ts->txtstamp(phydev->mii_ts, skb, type);
1590 }
1591
1592 /**
1593  * phy_is_internal - Convenience function for testing if a PHY is internal
1594  * @phydev: the phy_device struct
1595  */
1596 static inline bool phy_is_internal(struct phy_device *phydev)
1597 {
1598         return phydev->is_internal;
1599 }
1600
1601 /**
1602  * phy_on_sfp - Convenience function for testing if a PHY is on an SFP module
1603  * @phydev: the phy_device struct
1604  */
1605 static inline bool phy_on_sfp(struct phy_device *phydev)
1606 {
1607         return phydev->is_on_sfp_module;
1608 }
1609
1610 /**
1611  * phy_interface_mode_is_rgmii - Convenience function for testing if a
1612  * PHY interface mode is RGMII (all variants)
1613  * @mode: the &phy_interface_t enum
1614  */
1615 static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
1616 {
1617         return mode >= PHY_INTERFACE_MODE_RGMII &&
1618                 mode <= PHY_INTERFACE_MODE_RGMII_TXID;
1619 };
1620
1621 /**
1622  * phy_interface_mode_is_8023z() - does the PHY interface mode use 802.3z
1623  *   negotiation
1624  * @mode: one of &enum phy_interface_t
1625  *
1626  * Returns true if the PHY interface mode uses the 16-bit negotiation
1627  * word as defined in 802.3z. (See 802.3-2015 37.2.1 Config_Reg encoding)
1628  */
1629 static inline bool phy_interface_mode_is_8023z(phy_interface_t mode)
1630 {
1631         return mode == PHY_INTERFACE_MODE_1000BASEX ||
1632                mode == PHY_INTERFACE_MODE_2500BASEX;
1633 }
1634
1635 /**
1636  * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
1637  * is RGMII (all variants)
1638  * @phydev: the phy_device struct
1639  */
1640 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
1641 {
1642         return phy_interface_mode_is_rgmii(phydev->interface);
1643 };
1644
1645 /**
1646  * phy_is_pseudo_fixed_link - Convenience function for testing if this
1647  * PHY is the CPU port facing side of an Ethernet switch, or similar.
1648  * @phydev: the phy_device struct
1649  */
1650 static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
1651 {
1652         return phydev->is_pseudo_fixed_link;
1653 }
1654
1655 int phy_save_page(struct phy_device *phydev);
1656 int phy_select_page(struct phy_device *phydev, int page);
1657 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
1658 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
1659 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
1660 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
1661                              u16 mask, u16 set);
1662 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
1663                      u16 mask, u16 set);
1664
1665 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
1666                                      bool is_c45,
1667                                      struct phy_c45_device_ids *c45_ids);
1668 #if IS_ENABLED(CONFIG_PHYLIB)
1669 int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id);
1670 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode);
1671 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode);
1672 struct phy_device *device_phy_find_device(struct device *dev);
1673 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode);
1674 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
1675 int phy_device_register(struct phy_device *phy);
1676 void phy_device_free(struct phy_device *phydev);
1677 #else
1678 static inline int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id)
1679 {
1680         return 0;
1681 }
1682 static inline
1683 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
1684 {
1685         return 0;
1686 }
1687
1688 static inline
1689 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
1690 {
1691         return NULL;
1692 }
1693
1694 static inline struct phy_device *device_phy_find_device(struct device *dev)
1695 {
1696         return NULL;
1697 }
1698
1699 static inline
1700 struct fwnode_handle *fwnode_get_phy_node(struct fwnode_handle *fwnode)
1701 {
1702         return NULL;
1703 }
1704
1705 static inline
1706 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
1707 {
1708         return NULL;
1709 }
1710
1711 static inline int phy_device_register(struct phy_device *phy)
1712 {
1713         return 0;
1714 }
1715
1716 static inline void phy_device_free(struct phy_device *phydev) { }
1717 #endif /* CONFIG_PHYLIB */
1718 void phy_device_remove(struct phy_device *phydev);
1719 int phy_get_c45_ids(struct phy_device *phydev);
1720 int phy_init_hw(struct phy_device *phydev);
1721 int phy_suspend(struct phy_device *phydev);
1722 int phy_resume(struct phy_device *phydev);
1723 int __phy_resume(struct phy_device *phydev);
1724 int phy_loopback(struct phy_device *phydev, bool enable);
1725 void phy_sfp_attach(void *upstream, struct sfp_bus *bus);
1726 void phy_sfp_detach(void *upstream, struct sfp_bus *bus);
1727 int phy_sfp_probe(struct phy_device *phydev,
1728                   const struct sfp_upstream_ops *ops);
1729 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1730                               phy_interface_t interface);
1731 struct phy_device *phy_find_first(struct mii_bus *bus);
1732 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1733                       u32 flags, phy_interface_t interface);
1734 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1735                        void (*handler)(struct net_device *),
1736                        phy_interface_t interface);
1737 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1738                                void (*handler)(struct net_device *),
1739                                phy_interface_t interface);
1740 void phy_disconnect(struct phy_device *phydev);
1741 void phy_detach(struct phy_device *phydev);
1742 void phy_start(struct phy_device *phydev);
1743 void phy_stop(struct phy_device *phydev);
1744 int phy_config_aneg(struct phy_device *phydev);
1745 int _phy_start_aneg(struct phy_device *phydev);
1746 int phy_start_aneg(struct phy_device *phydev);
1747 int phy_aneg_done(struct phy_device *phydev);
1748 int phy_speed_down(struct phy_device *phydev, bool sync);
1749 int phy_speed_up(struct phy_device *phydev);
1750 bool phy_check_valid(int speed, int duplex, unsigned long *features);
1751
1752 int phy_restart_aneg(struct phy_device *phydev);
1753 int phy_reset_after_clk_enable(struct phy_device *phydev);
1754
1755 #if IS_ENABLED(CONFIG_PHYLIB)
1756 int phy_start_cable_test(struct phy_device *phydev,
1757                          struct netlink_ext_ack *extack);
1758 int phy_start_cable_test_tdr(struct phy_device *phydev,
1759                              struct netlink_ext_ack *extack,
1760                              const struct phy_tdr_config *config);
1761 #else
1762 static inline
1763 int phy_start_cable_test(struct phy_device *phydev,
1764                          struct netlink_ext_ack *extack)
1765 {
1766         NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1767         return -EOPNOTSUPP;
1768 }
1769 static inline
1770 int phy_start_cable_test_tdr(struct phy_device *phydev,
1771                              struct netlink_ext_ack *extack,
1772                              const struct phy_tdr_config *config)
1773 {
1774         NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1775         return -EOPNOTSUPP;
1776 }
1777 #endif
1778
1779 static inline void phy_device_reset(struct phy_device *phydev, int value)
1780 {
1781         mdio_device_reset(&phydev->mdio, value);
1782 }
1783
1784 #define phydev_err(_phydev, format, args...)    \
1785         dev_err(&_phydev->mdio.dev, format, ##args)
1786
1787 #define phydev_err_probe(_phydev, err, format, args...) \
1788         dev_err_probe(&_phydev->mdio.dev, err, format, ##args)
1789
1790 #define phydev_info(_phydev, format, args...)   \
1791         dev_info(&_phydev->mdio.dev, format, ##args)
1792
1793 #define phydev_warn(_phydev, format, args...)   \
1794         dev_warn(&_phydev->mdio.dev, format, ##args)
1795
1796 #define phydev_dbg(_phydev, format, args...)    \
1797         dev_dbg(&_phydev->mdio.dev, format, ##args)
1798
1799 static inline const char *phydev_name(const struct phy_device *phydev)
1800 {
1801         return dev_name(&phydev->mdio.dev);
1802 }
1803
1804 static inline void phy_lock_mdio_bus(struct phy_device *phydev)
1805 {
1806         mutex_lock(&phydev->mdio.bus->mdio_lock);
1807 }
1808
1809 static inline void phy_unlock_mdio_bus(struct phy_device *phydev)
1810 {
1811         mutex_unlock(&phydev->mdio.bus->mdio_lock);
1812 }
1813
1814 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1815         __printf(2, 3);
1816 char *phy_attached_info_irq(struct phy_device *phydev)
1817         __malloc;
1818 void phy_attached_info(struct phy_device *phydev);
1819
1820 /* Clause 22 PHY */
1821 int genphy_read_abilities(struct phy_device *phydev);
1822 int genphy_setup_forced(struct phy_device *phydev);
1823 int genphy_restart_aneg(struct phy_device *phydev);
1824 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1825 int genphy_config_eee_advert(struct phy_device *phydev);
1826 int __genphy_config_aneg(struct phy_device *phydev, bool changed);
1827 int genphy_aneg_done(struct phy_device *phydev);
1828 int genphy_update_link(struct phy_device *phydev);
1829 int genphy_read_lpa(struct phy_device *phydev);
1830 int genphy_read_status_fixed(struct phy_device *phydev);
1831 int genphy_read_status(struct phy_device *phydev);
1832 int genphy_read_master_slave(struct phy_device *phydev);
1833 int genphy_suspend(struct phy_device *phydev);
1834 int genphy_resume(struct phy_device *phydev);
1835 int genphy_loopback(struct phy_device *phydev, bool enable);
1836 int genphy_soft_reset(struct phy_device *phydev);
1837 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev);
1838
1839 static inline int genphy_config_aneg(struct phy_device *phydev)
1840 {
1841         return __genphy_config_aneg(phydev, false);
1842 }
1843
1844 static inline int genphy_no_config_intr(struct phy_device *phydev)
1845 {
1846         return 0;
1847 }
1848 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
1849                                 u16 regnum);
1850 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1851                                  u16 regnum, u16 val);
1852
1853 /* Clause 37 */
1854 int genphy_c37_config_aneg(struct phy_device *phydev);
1855 int genphy_c37_read_status(struct phy_device *phydev);
1856
1857 /* Clause 45 PHY */
1858 int genphy_c45_restart_aneg(struct phy_device *phydev);
1859 int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1860 int genphy_c45_aneg_done(struct phy_device *phydev);
1861 int genphy_c45_read_link(struct phy_device *phydev);
1862 int genphy_c45_read_lpa(struct phy_device *phydev);
1863 int genphy_c45_read_pma(struct phy_device *phydev);
1864 int genphy_c45_pma_setup_forced(struct phy_device *phydev);
1865 int genphy_c45_pma_baset1_setup_master_slave(struct phy_device *phydev);
1866 int genphy_c45_an_config_aneg(struct phy_device *phydev);
1867 int genphy_c45_an_disable_aneg(struct phy_device *phydev);
1868 int genphy_c45_read_mdix(struct phy_device *phydev);
1869 int genphy_c45_pma_read_abilities(struct phy_device *phydev);
1870 int genphy_c45_pma_read_ext_abilities(struct phy_device *phydev);
1871 int genphy_c45_pma_baset1_read_abilities(struct phy_device *phydev);
1872 int genphy_c45_read_eee_abilities(struct phy_device *phydev);
1873 int genphy_c45_pma_baset1_read_master_slave(struct phy_device *phydev);
1874 int genphy_c45_read_status(struct phy_device *phydev);
1875 int genphy_c45_baset1_read_status(struct phy_device *phydev);
1876 int genphy_c45_config_aneg(struct phy_device *phydev);
1877 int genphy_c45_loopback(struct phy_device *phydev, bool enable);
1878 int genphy_c45_pma_resume(struct phy_device *phydev);
1879 int genphy_c45_pma_suspend(struct phy_device *phydev);
1880 int genphy_c45_fast_retrain(struct phy_device *phydev, bool enable);
1881 int genphy_c45_plca_get_cfg(struct phy_device *phydev,
1882                             struct phy_plca_cfg *plca_cfg);
1883 int genphy_c45_plca_set_cfg(struct phy_device *phydev,
1884                             const struct phy_plca_cfg *plca_cfg);
1885 int genphy_c45_plca_get_status(struct phy_device *phydev,
1886                                struct phy_plca_status *plca_st);
1887 int genphy_c45_eee_is_active(struct phy_device *phydev, unsigned long *adv,
1888                              unsigned long *lp, bool *is_enabled);
1889 int genphy_c45_ethtool_get_eee(struct phy_device *phydev,
1890                                struct ethtool_eee *data);
1891 int genphy_c45_ethtool_set_eee(struct phy_device *phydev,
1892                                struct ethtool_eee *data);
1893 int genphy_c45_write_eee_adv(struct phy_device *phydev, unsigned long *adv);
1894 int genphy_c45_an_config_eee_aneg(struct phy_device *phydev);
1895 int genphy_c45_read_eee_adv(struct phy_device *phydev, unsigned long *adv);
1896
1897 /* Generic C45 PHY driver */
1898 extern struct phy_driver genphy_c45_driver;
1899
1900 /* The gen10g_* functions are the old Clause 45 stub */
1901 int gen10g_config_aneg(struct phy_device *phydev);
1902
1903 static inline int phy_read_status(struct phy_device *phydev)
1904 {
1905         if (!phydev->drv)
1906                 return -EIO;
1907
1908         if (phydev->drv->read_status)
1909                 return phydev->drv->read_status(phydev);
1910         else
1911                 return genphy_read_status(phydev);
1912 }
1913
1914 void phy_driver_unregister(struct phy_driver *drv);
1915 void phy_drivers_unregister(struct phy_driver *drv, int n);
1916 int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
1917 int phy_drivers_register(struct phy_driver *new_driver, int n,
1918                          struct module *owner);
1919 void phy_error(struct phy_device *phydev);
1920 void phy_state_machine(struct work_struct *work);
1921 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
1922 void phy_trigger_machine(struct phy_device *phydev);
1923 void phy_mac_interrupt(struct phy_device *phydev);
1924 void phy_start_machine(struct phy_device *phydev);
1925 void phy_stop_machine(struct phy_device *phydev);
1926 void phy_ethtool_ksettings_get(struct phy_device *phydev,
1927                                struct ethtool_link_ksettings *cmd);
1928 int phy_ethtool_ksettings_set(struct phy_device *phydev,
1929                               const struct ethtool_link_ksettings *cmd);
1930 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
1931 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
1932 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd);
1933 int phy_disable_interrupts(struct phy_device *phydev);
1934 void phy_request_interrupt(struct phy_device *phydev);
1935 void phy_free_interrupt(struct phy_device *phydev);
1936 void phy_print_status(struct phy_device *phydev);
1937 int phy_get_rate_matching(struct phy_device *phydev,
1938                             phy_interface_t iface);
1939 void phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
1940 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
1941 void phy_advertise_supported(struct phy_device *phydev);
1942 void phy_support_sym_pause(struct phy_device *phydev);
1943 void phy_support_asym_pause(struct phy_device *phydev);
1944 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
1945                        bool autoneg);
1946 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
1947 bool phy_validate_pause(struct phy_device *phydev,
1948                         struct ethtool_pauseparam *pp);
1949 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause);
1950
1951 s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
1952                            const int *delay_values, int size, bool is_rx);
1953
1954 void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv,
1955                        bool *tx_pause, bool *rx_pause);
1956
1957 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
1958                        int (*run)(struct phy_device *));
1959 int phy_register_fixup_for_id(const char *bus_id,
1960                               int (*run)(struct phy_device *));
1961 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
1962                                int (*run)(struct phy_device *));
1963
1964 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
1965 int phy_unregister_fixup_for_id(const char *bus_id);
1966 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
1967
1968 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
1969 int phy_get_eee_err(struct phy_device *phydev);
1970 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
1971 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
1972 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
1973 void phy_ethtool_get_wol(struct phy_device *phydev,
1974                          struct ethtool_wolinfo *wol);
1975 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1976                                    struct ethtool_link_ksettings *cmd);
1977 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1978                                    const struct ethtool_link_ksettings *cmd);
1979 int phy_ethtool_nway_reset(struct net_device *ndev);
1980 int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size);
1981 void phy_package_leave(struct phy_device *phydev);
1982 int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
1983                           int base_addr, size_t priv_size);
1984
1985 int __init mdio_bus_init(void);
1986 void mdio_bus_exit(void);
1987
1988 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data);
1989 int phy_ethtool_get_sset_count(struct phy_device *phydev);
1990 int phy_ethtool_get_stats(struct phy_device *phydev,
1991                           struct ethtool_stats *stats, u64 *data);
1992 int phy_ethtool_get_plca_cfg(struct phy_device *phydev,
1993                              struct phy_plca_cfg *plca_cfg);
1994 int phy_ethtool_set_plca_cfg(struct phy_device *phydev,
1995                              const struct phy_plca_cfg *plca_cfg,
1996                              struct netlink_ext_ack *extack);
1997 int phy_ethtool_get_plca_status(struct phy_device *phydev,
1998                                 struct phy_plca_status *plca_st);
1999
2000 int __phy_hwtstamp_get(struct phy_device *phydev,
2001                        struct kernel_hwtstamp_config *config);
2002 int __phy_hwtstamp_set(struct phy_device *phydev,
2003                        struct kernel_hwtstamp_config *config,
2004                        struct netlink_ext_ack *extack);
2005
2006 static inline int phy_package_address(struct phy_device *phydev,
2007                                       unsigned int addr_offset)
2008 {
2009         struct phy_package_shared *shared = phydev->shared;
2010         u8 base_addr = shared->base_addr;
2011
2012         if (addr_offset >= PHY_MAX_ADDR - base_addr)
2013                 return -EIO;
2014
2015         /* we know that addr will be in the range 0..31 and thus the
2016          * implicit cast to a signed int is not a problem.
2017          */
2018         return base_addr + addr_offset;
2019 }
2020
2021 static inline int phy_package_read(struct phy_device *phydev,
2022                                    unsigned int addr_offset, u32 regnum)
2023 {
2024         int addr = phy_package_address(phydev, addr_offset);
2025
2026         if (addr < 0)
2027                 return addr;
2028
2029         return mdiobus_read(phydev->mdio.bus, addr, regnum);
2030 }
2031
2032 static inline int __phy_package_read(struct phy_device *phydev,
2033                                      unsigned int addr_offset, u32 regnum)
2034 {
2035         int addr = phy_package_address(phydev, addr_offset);
2036
2037         if (addr < 0)
2038                 return addr;
2039
2040         return __mdiobus_read(phydev->mdio.bus, addr, regnum);
2041 }
2042
2043 static inline int phy_package_write(struct phy_device *phydev,
2044                                     unsigned int addr_offset, u32 regnum,
2045                                     u16 val)
2046 {
2047         int addr = phy_package_address(phydev, addr_offset);
2048
2049         if (addr < 0)
2050                 return addr;
2051
2052         return mdiobus_write(phydev->mdio.bus, addr, regnum, val);
2053 }
2054
2055 static inline int __phy_package_write(struct phy_device *phydev,
2056                                       unsigned int addr_offset, u32 regnum,
2057                                       u16 val)
2058 {
2059         int addr = phy_package_address(phydev, addr_offset);
2060
2061         if (addr < 0)
2062                 return addr;
2063
2064         return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
2065 }
2066
2067 int __phy_package_read_mmd(struct phy_device *phydev,
2068                            unsigned int addr_offset, int devad,
2069                            u32 regnum);
2070
2071 int phy_package_read_mmd(struct phy_device *phydev,
2072                          unsigned int addr_offset, int devad,
2073                          u32 regnum);
2074
2075 int __phy_package_write_mmd(struct phy_device *phydev,
2076                             unsigned int addr_offset, int devad,
2077                             u32 regnum, u16 val);
2078
2079 int phy_package_write_mmd(struct phy_device *phydev,
2080                           unsigned int addr_offset, int devad,
2081                           u32 regnum, u16 val);
2082
2083 static inline bool __phy_package_set_once(struct phy_device *phydev,
2084                                           unsigned int b)
2085 {
2086         struct phy_package_shared *shared = phydev->shared;
2087
2088         if (!shared)
2089                 return false;
2090
2091         return !test_and_set_bit(b, &shared->flags);
2092 }
2093
2094 static inline bool phy_package_init_once(struct phy_device *phydev)
2095 {
2096         return __phy_package_set_once(phydev, PHY_SHARED_F_INIT_DONE);
2097 }
2098
2099 static inline bool phy_package_probe_once(struct phy_device *phydev)
2100 {
2101         return __phy_package_set_once(phydev, PHY_SHARED_F_PROBE_DONE);
2102 }
2103
2104 extern struct bus_type mdio_bus_type;
2105
2106 struct mdio_board_info {
2107         const char      *bus_id;
2108         char            modalias[MDIO_NAME_SIZE];
2109         int             mdio_addr;
2110         const void      *platform_data;
2111 };
2112
2113 #if IS_ENABLED(CONFIG_MDIO_DEVICE)
2114 int mdiobus_register_board_info(const struct mdio_board_info *info,
2115                                 unsigned int n);
2116 #else
2117 static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
2118                                               unsigned int n)
2119 {
2120         return 0;
2121 }
2122 #endif
2123
2124
2125 /**
2126  * phy_module_driver() - Helper macro for registering PHY drivers
2127  * @__phy_drivers: array of PHY drivers to register
2128  * @__count: Numbers of members in array
2129  *
2130  * Helper macro for PHY drivers which do not do anything special in module
2131  * init/exit. Each module may only use this macro once, and calling it
2132  * replaces module_init() and module_exit().
2133  */
2134 #define phy_module_driver(__phy_drivers, __count)                       \
2135 static int __init phy_module_init(void)                                 \
2136 {                                                                       \
2137         return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
2138 }                                                                       \
2139 module_init(phy_module_init);                                           \
2140 static void __exit phy_module_exit(void)                                \
2141 {                                                                       \
2142         phy_drivers_unregister(__phy_drivers, __count);                 \
2143 }                                                                       \
2144 module_exit(phy_module_exit)
2145
2146 #define module_phy_driver(__phy_drivers)                                \
2147         phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
2148
2149 bool phy_driver_is_genphy(struct phy_device *phydev);
2150 bool phy_driver_is_genphy_10g(struct phy_device *phydev);
2151
2152 #endif /* __PHY_H */
This page took 0.143469 seconds and 2 git commands to generate.