Merge branch 'tegra/dt64' into arm/fixes
[linux-2.6-microblaze.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/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
46 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
47 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
48 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
49 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
50 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
51 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
52 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
53 extern __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
64 extern const int phy_basic_ports_array[3];
65 extern const int phy_fibre_port_array[1];
66 extern const int phy_all_ports_features_array[7];
67 extern const int phy_10_100_features_array[4];
68 extern const int phy_basic_t1_features_array[2];
69 extern const int phy_gbit_features_array[2];
70 extern const int phy_10gbit_features_array[1];
71
72 /*
73  * Set phydev->irq to PHY_POLL if interrupts are not supported,
74  * or not desired for this PHY.  Set to PHY_MAC_INTERRUPT if
75  * the attached MAC driver handles the interrupt
76  */
77 #define PHY_POLL                -1
78 #define PHY_MAC_INTERRUPT       -2
79
80 #define PHY_IS_INTERNAL         0x00000001
81 #define PHY_RST_AFTER_CLK_EN    0x00000002
82 #define PHY_POLL_CABLE_TEST     0x00000004
83 #define MDIO_DEVICE_IS_PHY      0x80000000
84
85 /**
86  * enum phy_interface_t - Interface Mode definitions
87  *
88  * @PHY_INTERFACE_MODE_NA: Not Applicable - don't touch
89  * @PHY_INTERFACE_MODE_INTERNAL: No interface, MAC and PHY combined
90  * @PHY_INTERFACE_MODE_MII: Median-independent interface
91  * @PHY_INTERFACE_MODE_GMII: Gigabit median-independent interface
92  * @PHY_INTERFACE_MODE_SGMII: Serial gigabit media-independent interface
93  * @PHY_INTERFACE_MODE_TBI: Ten Bit Interface
94  * @PHY_INTERFACE_MODE_REVMII: Reverse Media Independent Interface
95  * @PHY_INTERFACE_MODE_RMII: Reduced Media Independent Interface
96  * @PHY_INTERFACE_MODE_RGMII: Reduced gigabit media-independent interface
97  * @PHY_INTERFACE_MODE_RGMII_ID: RGMII with Internal RX+TX delay
98  * @PHY_INTERFACE_MODE_RGMII_RXID: RGMII with Internal RX delay
99  * @PHY_INTERFACE_MODE_RGMII_TXID: RGMII with Internal RX delay
100  * @PHY_INTERFACE_MODE_RTBI: Reduced TBI
101  * @PHY_INTERFACE_MODE_SMII: ??? MII
102  * @PHY_INTERFACE_MODE_XGMII: 10 gigabit media-independent interface
103  * @PHY_INTERFACE_MODE_XLGMII:40 gigabit media-independent interface
104  * @PHY_INTERFACE_MODE_MOCA: Multimedia over Coax
105  * @PHY_INTERFACE_MODE_QSGMII: Quad SGMII
106  * @PHY_INTERFACE_MODE_TRGMII: Turbo RGMII
107  * @PHY_INTERFACE_MODE_100BASEX: 100 BaseX
108  * @PHY_INTERFACE_MODE_1000BASEX: 1000 BaseX
109  * @PHY_INTERFACE_MODE_2500BASEX: 2500 BaseX
110  * @PHY_INTERFACE_MODE_5GBASER: 5G BaseR
111  * @PHY_INTERFACE_MODE_RXAUI: Reduced XAUI
112  * @PHY_INTERFACE_MODE_XAUI: 10 Gigabit Attachment Unit Interface
113  * @PHY_INTERFACE_MODE_10GBASER: 10G BaseR
114  * @PHY_INTERFACE_MODE_USXGMII:  Universal Serial 10GE MII
115  * @PHY_INTERFACE_MODE_10GKR: 10GBASE-KR - with Clause 73 AN
116  * @PHY_INTERFACE_MODE_MAX: Book keeping
117  *
118  * Describes the interface between the MAC and PHY.
119  */
120 typedef enum {
121         PHY_INTERFACE_MODE_NA,
122         PHY_INTERFACE_MODE_INTERNAL,
123         PHY_INTERFACE_MODE_MII,
124         PHY_INTERFACE_MODE_GMII,
125         PHY_INTERFACE_MODE_SGMII,
126         PHY_INTERFACE_MODE_TBI,
127         PHY_INTERFACE_MODE_REVMII,
128         PHY_INTERFACE_MODE_RMII,
129         PHY_INTERFACE_MODE_RGMII,
130         PHY_INTERFACE_MODE_RGMII_ID,
131         PHY_INTERFACE_MODE_RGMII_RXID,
132         PHY_INTERFACE_MODE_RGMII_TXID,
133         PHY_INTERFACE_MODE_RTBI,
134         PHY_INTERFACE_MODE_SMII,
135         PHY_INTERFACE_MODE_XGMII,
136         PHY_INTERFACE_MODE_XLGMII,
137         PHY_INTERFACE_MODE_MOCA,
138         PHY_INTERFACE_MODE_QSGMII,
139         PHY_INTERFACE_MODE_TRGMII,
140         PHY_INTERFACE_MODE_100BASEX,
141         PHY_INTERFACE_MODE_1000BASEX,
142         PHY_INTERFACE_MODE_2500BASEX,
143         PHY_INTERFACE_MODE_5GBASER,
144         PHY_INTERFACE_MODE_RXAUI,
145         PHY_INTERFACE_MODE_XAUI,
146         /* 10GBASE-R, XFI, SFI - single lane 10G Serdes */
147         PHY_INTERFACE_MODE_10GBASER,
148         PHY_INTERFACE_MODE_USXGMII,
149         /* 10GBASE-KR - with Clause 73 AN */
150         PHY_INTERFACE_MODE_10GKR,
151         PHY_INTERFACE_MODE_MAX,
152 } phy_interface_t;
153
154 /*
155  * phy_supported_speeds - return all speeds currently supported by a PHY device
156  */
157 unsigned int phy_supported_speeds(struct phy_device *phy,
158                                       unsigned int *speeds,
159                                       unsigned int size);
160
161 /**
162  * phy_modes - map phy_interface_t enum to device tree binding of phy-mode
163  * @interface: enum phy_interface_t value
164  *
165  * Description: maps enum &phy_interface_t defined in this file
166  * into the device tree binding of 'phy-mode', so that Ethernet
167  * device driver can get PHY interface from device tree.
168  */
169 static inline const char *phy_modes(phy_interface_t interface)
170 {
171         switch (interface) {
172         case PHY_INTERFACE_MODE_NA:
173                 return "";
174         case PHY_INTERFACE_MODE_INTERNAL:
175                 return "internal";
176         case PHY_INTERFACE_MODE_MII:
177                 return "mii";
178         case PHY_INTERFACE_MODE_GMII:
179                 return "gmii";
180         case PHY_INTERFACE_MODE_SGMII:
181                 return "sgmii";
182         case PHY_INTERFACE_MODE_TBI:
183                 return "tbi";
184         case PHY_INTERFACE_MODE_REVMII:
185                 return "rev-mii";
186         case PHY_INTERFACE_MODE_RMII:
187                 return "rmii";
188         case PHY_INTERFACE_MODE_RGMII:
189                 return "rgmii";
190         case PHY_INTERFACE_MODE_RGMII_ID:
191                 return "rgmii-id";
192         case PHY_INTERFACE_MODE_RGMII_RXID:
193                 return "rgmii-rxid";
194         case PHY_INTERFACE_MODE_RGMII_TXID:
195                 return "rgmii-txid";
196         case PHY_INTERFACE_MODE_RTBI:
197                 return "rtbi";
198         case PHY_INTERFACE_MODE_SMII:
199                 return "smii";
200         case PHY_INTERFACE_MODE_XGMII:
201                 return "xgmii";
202         case PHY_INTERFACE_MODE_XLGMII:
203                 return "xlgmii";
204         case PHY_INTERFACE_MODE_MOCA:
205                 return "moca";
206         case PHY_INTERFACE_MODE_QSGMII:
207                 return "qsgmii";
208         case PHY_INTERFACE_MODE_TRGMII:
209                 return "trgmii";
210         case PHY_INTERFACE_MODE_1000BASEX:
211                 return "1000base-x";
212         case PHY_INTERFACE_MODE_2500BASEX:
213                 return "2500base-x";
214         case PHY_INTERFACE_MODE_5GBASER:
215                 return "5gbase-r";
216         case PHY_INTERFACE_MODE_RXAUI:
217                 return "rxaui";
218         case PHY_INTERFACE_MODE_XAUI:
219                 return "xaui";
220         case PHY_INTERFACE_MODE_10GBASER:
221                 return "10gbase-r";
222         case PHY_INTERFACE_MODE_USXGMII:
223                 return "usxgmii";
224         case PHY_INTERFACE_MODE_10GKR:
225                 return "10gbase-kr";
226         case PHY_INTERFACE_MODE_100BASEX:
227                 return "100base-x";
228         default:
229                 return "unknown";
230         }
231 }
232
233
234 #define PHY_INIT_TIMEOUT        100000
235 #define PHY_FORCE_TIMEOUT       10
236
237 #define PHY_MAX_ADDR    32
238
239 /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
240 #define PHY_ID_FMT "%s:%02x"
241
242 #define MII_BUS_ID_SIZE 61
243
244 struct device;
245 struct phylink;
246 struct sfp_bus;
247 struct sfp_upstream_ops;
248 struct sk_buff;
249
250 /**
251  * struct mdio_bus_stats - Statistics counters for MDIO busses
252  * @transfers: Total number of transfers, i.e. @writes + @reads
253  * @errors: Number of MDIO transfers that returned an error
254  * @writes: Number of write transfers
255  * @reads: Number of read transfers
256  * @syncp: Synchronisation for incrementing statistics
257  */
258 struct mdio_bus_stats {
259         u64_stats_t transfers;
260         u64_stats_t errors;
261         u64_stats_t writes;
262         u64_stats_t reads;
263         /* Must be last, add new statistics above */
264         struct u64_stats_sync syncp;
265 };
266
267 /**
268  * struct phy_package_shared - Shared information in PHY packages
269  * @addr: Common PHY address used to combine PHYs in one package
270  * @refcnt: Number of PHYs connected to this shared data
271  * @flags: Initialization of PHY package
272  * @priv_size: Size of the shared private data @priv
273  * @priv: Driver private data shared across a PHY package
274  *
275  * Represents a shared structure between different phydev's in the same
276  * package, for example a quad PHY. See phy_package_join() and
277  * phy_package_leave().
278  */
279 struct phy_package_shared {
280         int addr;
281         refcount_t refcnt;
282         unsigned long flags;
283         size_t priv_size;
284
285         /* private data pointer */
286         /* note that this pointer is shared between different phydevs and
287          * the user has to take care of appropriate locking. It is allocated
288          * and freed automatically by phy_package_join() and
289          * phy_package_leave().
290          */
291         void *priv;
292 };
293
294 /* used as bit number in atomic bitops */
295 #define PHY_SHARED_F_INIT_DONE  0
296 #define PHY_SHARED_F_PROBE_DONE 1
297
298 /**
299  * struct mii_bus - Represents an MDIO bus
300  *
301  * @owner: Who owns this device
302  * @name: User friendly name for this MDIO device, or driver name
303  * @id: Unique identifier for this bus, typical from bus hierarchy
304  * @priv: Driver private data
305  *
306  * The Bus class for PHYs.  Devices which provide access to
307  * PHYs should register using this structure
308  */
309 struct mii_bus {
310         struct module *owner;
311         const char *name;
312         char id[MII_BUS_ID_SIZE];
313         void *priv;
314         /** @read: Perform a read transfer on the bus */
315         int (*read)(struct mii_bus *bus, int addr, int regnum);
316         /** @write: Perform a write transfer on the bus */
317         int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val);
318         /** @reset: Perform a reset of the bus */
319         int (*reset)(struct mii_bus *bus);
320
321         /** @stats: Statistic counters per device on the bus */
322         struct mdio_bus_stats stats[PHY_MAX_ADDR];
323
324         /**
325          * @mdio_lock: A lock to ensure that only one thing can read/write
326          * the MDIO bus at a time
327          */
328         struct mutex mdio_lock;
329
330         /** @parent: Parent device of this bus */
331         struct device *parent;
332         /** @state: State of bus structure */
333         enum {
334                 MDIOBUS_ALLOCATED = 1,
335                 MDIOBUS_REGISTERED,
336                 MDIOBUS_UNREGISTERED,
337                 MDIOBUS_RELEASED,
338         } state;
339
340         /** @dev: Kernel device representation */
341         struct device dev;
342
343         /** @mdio_map: list of all MDIO devices on bus */
344         struct mdio_device *mdio_map[PHY_MAX_ADDR];
345
346         /** @phy_mask: PHY addresses to be ignored when probing */
347         u32 phy_mask;
348
349         /** @phy_ignore_ta_mask: PHY addresses to ignore the TA/read failure */
350         u32 phy_ignore_ta_mask;
351
352         /**
353          * @irq: An array of interrupts, each PHY's interrupt at the index
354          * matching its address
355          */
356         int irq[PHY_MAX_ADDR];
357
358         /** @reset_delay_us: GPIO reset pulse width in microseconds */
359         int reset_delay_us;
360         /** @reset_post_delay_us: GPIO reset deassert delay in microseconds */
361         int reset_post_delay_us;
362         /** @reset_gpiod: Reset GPIO descriptor pointer */
363         struct gpio_desc *reset_gpiod;
364
365         /** @probe_capabilities: bus capabilities, used for probing */
366         enum {
367                 MDIOBUS_NO_CAP = 0,
368                 MDIOBUS_C22,
369                 MDIOBUS_C45,
370                 MDIOBUS_C22_C45,
371         } probe_capabilities;
372
373         /** @shared_lock: protect access to the shared element */
374         struct mutex shared_lock;
375
376         /** @shared: shared state across different PHYs */
377         struct phy_package_shared *shared[PHY_MAX_ADDR];
378 };
379 #define to_mii_bus(d) container_of(d, struct mii_bus, dev)
380
381 struct mii_bus *mdiobus_alloc_size(size_t size);
382
383 /**
384  * mdiobus_alloc - Allocate an MDIO bus structure
385  *
386  * The internal state of the MDIO bus will be set of MDIOBUS_ALLOCATED ready
387  * for the driver to register the bus.
388  */
389 static inline struct mii_bus *mdiobus_alloc(void)
390 {
391         return mdiobus_alloc_size(0);
392 }
393
394 int __mdiobus_register(struct mii_bus *bus, struct module *owner);
395 int __devm_mdiobus_register(struct device *dev, struct mii_bus *bus,
396                             struct module *owner);
397 #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
398 #define devm_mdiobus_register(dev, bus) \
399                 __devm_mdiobus_register(dev, bus, THIS_MODULE)
400
401 void mdiobus_unregister(struct mii_bus *bus);
402 void mdiobus_free(struct mii_bus *bus);
403 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
404 static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
405 {
406         return devm_mdiobus_alloc_size(dev, 0);
407 }
408
409 struct mii_bus *mdio_find_bus(const char *mdio_name);
410 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
411
412 #define PHY_INTERRUPT_DISABLED  false
413 #define PHY_INTERRUPT_ENABLED   true
414
415 /**
416  * enum phy_state - PHY state machine states:
417  *
418  * @PHY_DOWN: PHY device and driver are not ready for anything.  probe
419  * should be called if and only if the PHY is in this state,
420  * given that the PHY device exists.
421  * - PHY driver probe function will set the state to @PHY_READY
422  *
423  * @PHY_READY: PHY is ready to send and receive packets, but the
424  * controller is not.  By default, PHYs which do not implement
425  * probe will be set to this state by phy_probe().
426  * - start will set the state to UP
427  *
428  * @PHY_UP: The PHY and attached device are ready to do work.
429  * Interrupts should be started here.
430  * - timer moves to @PHY_NOLINK or @PHY_RUNNING
431  *
432  * @PHY_NOLINK: PHY is up, but not currently plugged in.
433  * - irq or timer will set @PHY_RUNNING if link comes back
434  * - phy_stop moves to @PHY_HALTED
435  *
436  * @PHY_RUNNING: PHY is currently up, running, and possibly sending
437  * and/or receiving packets
438  * - irq or timer will set @PHY_NOLINK if link goes down
439  * - phy_stop moves to @PHY_HALTED
440  *
441  * @PHY_CABLETEST: PHY is performing a cable test. Packet reception/sending
442  * is not expected to work, carrier will be indicated as down. PHY will be
443  * poll once per second, or on interrupt for it current state.
444  * Once complete, move to UP to restart the PHY.
445  * - phy_stop aborts the running test and moves to @PHY_HALTED
446  *
447  * @PHY_HALTED: PHY is up, but no polling or interrupts are done. Or
448  * PHY is in an error state.
449  * - phy_start moves to @PHY_UP
450  */
451 enum phy_state {
452         PHY_DOWN = 0,
453         PHY_READY,
454         PHY_HALTED,
455         PHY_UP,
456         PHY_RUNNING,
457         PHY_NOLINK,
458         PHY_CABLETEST,
459 };
460
461 #define MDIO_MMD_NUM 32
462
463 /**
464  * struct phy_c45_device_ids - 802.3-c45 Device Identifiers
465  * @devices_in_package: IEEE 802.3 devices in package register value.
466  * @mmds_present: bit vector of MMDs present.
467  * @device_ids: The device identifer for each present device.
468  */
469 struct phy_c45_device_ids {
470         u32 devices_in_package;
471         u32 mmds_present;
472         u32 device_ids[MDIO_MMD_NUM];
473 };
474
475 struct macsec_context;
476 struct macsec_ops;
477
478 /**
479  * struct phy_device - An instance of a PHY
480  *
481  * @mdio: MDIO bus this PHY is on
482  * @drv: Pointer to the driver for this PHY instance
483  * @phy_id: UID for this device found during discovery
484  * @c45_ids: 802.3-c45 Device Identifiers if is_c45.
485  * @is_c45:  Set to true if this PHY uses clause 45 addressing.
486  * @is_internal: Set to true if this PHY is internal to a MAC.
487  * @is_pseudo_fixed_link: Set to true if this PHY is an Ethernet switch, etc.
488  * @is_gigabit_capable: Set to true if PHY supports 1000Mbps
489  * @has_fixups: Set to true if this PHY has fixups/quirks.
490  * @suspended: Set to true if this PHY has been suspended successfully.
491  * @suspended_by_mdio_bus: Set to true if this PHY was suspended by MDIO bus.
492  * @sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
493  * @loopback_enabled: Set true if this PHY has been loopbacked successfully.
494  * @downshifted_rate: Set true if link speed has been downshifted.
495  * @is_on_sfp_module: Set true if PHY is located on an SFP module.
496  * @state: State of the PHY for management purposes
497  * @dev_flags: Device-specific flags used by the PHY driver.
498  * @irq: IRQ number of the PHY's interrupt (-1 if none)
499  * @phy_timer: The timer for handling the state machine
500  * @phylink: Pointer to phylink instance for this PHY
501  * @sfp_bus_attached: Flag indicating whether the SFP bus has been attached
502  * @sfp_bus: SFP bus attached to this PHY's fiber port
503  * @attached_dev: The attached enet driver's device instance ptr
504  * @adjust_link: Callback for the enet controller to respond to changes: in the
505  *               link state.
506  * @phy_link_change: Callback for phylink for notification of link change
507  * @macsec_ops: MACsec offloading ops.
508  *
509  * @speed: Current link speed
510  * @duplex: Current duplex
511  * @port: Current port
512  * @pause: Current pause
513  * @asym_pause: Current asymmetric pause
514  * @supported: Combined MAC/PHY supported linkmodes
515  * @advertising: Currently advertised linkmodes
516  * @adv_old: Saved advertised while power saving for WoL
517  * @lp_advertising: Current link partner advertised linkmodes
518  * @eee_broken_modes: Energy efficient ethernet modes which should be prohibited
519  * @autoneg: Flag autoneg being used
520  * @link: Current link state
521  * @autoneg_complete: Flag auto negotiation of the link has completed
522  * @mdix: Current crossover
523  * @mdix_ctrl: User setting of crossover
524  * @interrupts: Flag interrupts have been enabled
525  * @interface: enum phy_interface_t value
526  * @skb: Netlink message for cable diagnostics
527  * @nest: Netlink nest used for cable diagnostics
528  * @ehdr: nNtlink header for cable diagnostics
529  * @phy_led_triggers: Array of LED triggers
530  * @phy_num_led_triggers: Number of triggers in @phy_led_triggers
531  * @led_link_trigger: LED trigger for link up/down
532  * @last_triggered: last LED trigger for link speed
533  * @master_slave_set: User requested master/slave configuration
534  * @master_slave_get: Current master/slave advertisement
535  * @master_slave_state: Current master/slave configuration
536  * @mii_ts: Pointer to time stamper callbacks
537  * @lock:  Mutex for serialization access to PHY
538  * @state_queue: Work queue for state machine
539  * @shared: Pointer to private data shared by phys in one package
540  * @priv: Pointer to driver private data
541  *
542  * interrupts currently only supports enabled or disabled,
543  * but could be changed in the future to support enabling
544  * and disabling specific interrupts
545  *
546  * Contains some infrastructure for polling and interrupt
547  * handling, as well as handling shifts in PHY hardware state
548  */
549 struct phy_device {
550         struct mdio_device mdio;
551
552         /* Information about the PHY type */
553         /* And management functions */
554         struct phy_driver *drv;
555
556         u32 phy_id;
557
558         struct phy_c45_device_ids c45_ids;
559         unsigned is_c45:1;
560         unsigned is_internal:1;
561         unsigned is_pseudo_fixed_link:1;
562         unsigned is_gigabit_capable:1;
563         unsigned has_fixups:1;
564         unsigned suspended:1;
565         unsigned suspended_by_mdio_bus:1;
566         unsigned sysfs_links:1;
567         unsigned loopback_enabled:1;
568         unsigned downshifted_rate:1;
569         unsigned is_on_sfp_module:1;
570
571         unsigned autoneg:1;
572         /* The most recently read link state */
573         unsigned link:1;
574         unsigned autoneg_complete:1;
575
576         /* Interrupts are enabled */
577         unsigned interrupts:1;
578
579         enum phy_state state;
580
581         u32 dev_flags;
582
583         phy_interface_t interface;
584
585         /*
586          * forced speed & duplex (no autoneg)
587          * partner speed & duplex & pause (autoneg)
588          */
589         int speed;
590         int duplex;
591         int port;
592         int pause;
593         int asym_pause;
594         u8 master_slave_get;
595         u8 master_slave_set;
596         u8 master_slave_state;
597
598         /* Union of PHY and Attached devices' supported link modes */
599         /* See ethtool.h for more info */
600         __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
601         __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
602         __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
603         /* used with phy_speed_down */
604         __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
605
606         /* Energy efficient ethernet modes which should be prohibited */
607         u32 eee_broken_modes;
608
609 #ifdef CONFIG_LED_TRIGGER_PHY
610         struct phy_led_trigger *phy_led_triggers;
611         unsigned int phy_num_led_triggers;
612         struct phy_led_trigger *last_triggered;
613
614         struct phy_led_trigger *led_link_trigger;
615 #endif
616
617         /*
618          * Interrupt number for this PHY
619          * -1 means no interrupt
620          */
621         int irq;
622
623         /* private data pointer */
624         /* For use by PHYs to maintain extra state */
625         void *priv;
626
627         /* shared data pointer */
628         /* For use by PHYs inside the same package that need a shared state. */
629         struct phy_package_shared *shared;
630
631         /* Reporting cable test results */
632         struct sk_buff *skb;
633         void *ehdr;
634         struct nlattr *nest;
635
636         /* Interrupt and Polling infrastructure */
637         struct delayed_work state_queue;
638
639         struct mutex lock;
640
641         /* This may be modified under the rtnl lock */
642         bool sfp_bus_attached;
643         struct sfp_bus *sfp_bus;
644         struct phylink *phylink;
645         struct net_device *attached_dev;
646         struct mii_timestamper *mii_ts;
647
648         u8 mdix;
649         u8 mdix_ctrl;
650
651         void (*phy_link_change)(struct phy_device *phydev, bool up);
652         void (*adjust_link)(struct net_device *dev);
653
654 #if IS_ENABLED(CONFIG_MACSEC)
655         /* MACsec management functions */
656         const struct macsec_ops *macsec_ops;
657 #endif
658 };
659
660 static inline struct phy_device *to_phy_device(const struct device *dev)
661 {
662         return container_of(to_mdio_device(dev), struct phy_device, mdio);
663 }
664
665 /**
666  * struct phy_tdr_config - Configuration of a TDR raw test
667  *
668  * @first: Distance for first data collection point
669  * @last: Distance for last data collection point
670  * @step: Step between data collection points
671  * @pair: Bitmap of cable pairs to collect data for
672  *
673  * A structure containing possible configuration parameters
674  * for a TDR cable test. The driver does not need to implement
675  * all the parameters, but should report what is actually used.
676  * All distances are in centimeters.
677  */
678 struct phy_tdr_config {
679         u32 first;
680         u32 last;
681         u32 step;
682         s8 pair;
683 };
684 #define PHY_PAIR_ALL -1
685
686 /**
687  * struct phy_driver - Driver structure for a particular PHY type
688  *
689  * @mdiodrv: Data common to all MDIO devices
690  * @phy_id: The result of reading the UID registers of this PHY
691  *   type, and ANDing them with the phy_id_mask.  This driver
692  *   only works for PHYs with IDs which match this field
693  * @name: The friendly name of this PHY type
694  * @phy_id_mask: Defines the important bits of the phy_id
695  * @features: A mandatory list of features (speed, duplex, etc)
696  *   supported by this PHY
697  * @flags: A bitfield defining certain other features this PHY
698  *   supports (like interrupts)
699  * @driver_data: Static driver data
700  *
701  * All functions are optional. If config_aneg or read_status
702  * are not implemented, the phy core uses the genphy versions.
703  * Note that none of these functions should be called from
704  * interrupt time. The goal is for the bus read/write functions
705  * to be able to block when the bus transaction is happening,
706  * and be freed up by an interrupt (The MPC85xx has this ability,
707  * though it is not currently supported in the driver).
708  */
709 struct phy_driver {
710         struct mdio_driver_common mdiodrv;
711         u32 phy_id;
712         char *name;
713         u32 phy_id_mask;
714         const unsigned long * const features;
715         u32 flags;
716         const void *driver_data;
717
718         /**
719          * @soft_reset: Called to issue a PHY software reset
720          */
721         int (*soft_reset)(struct phy_device *phydev);
722
723         /**
724          * @config_init: Called to initialize the PHY,
725          * including after a reset
726          */
727         int (*config_init)(struct phy_device *phydev);
728
729         /**
730          * @probe: Called during discovery.  Used to set
731          * up device-specific structures, if any
732          */
733         int (*probe)(struct phy_device *phydev);
734
735         /**
736          * @get_features: Probe the hardware to determine what
737          * abilities it has.  Should only set phydev->supported.
738          */
739         int (*get_features)(struct phy_device *phydev);
740
741         /* PHY Power Management */
742         /** @suspend: Suspend the hardware, saving state if needed */
743         int (*suspend)(struct phy_device *phydev);
744         /** @resume: Resume the hardware, restoring state if needed */
745         int (*resume)(struct phy_device *phydev);
746
747         /**
748          * @config_aneg: Configures the advertisement and resets
749          * autonegotiation if phydev->autoneg is on,
750          * forces the speed to the current settings in phydev
751          * if phydev->autoneg is off
752          */
753         int (*config_aneg)(struct phy_device *phydev);
754
755         /** @aneg_done: Determines the auto negotiation result */
756         int (*aneg_done)(struct phy_device *phydev);
757
758         /** @read_status: Determines the negotiated speed and duplex */
759         int (*read_status)(struct phy_device *phydev);
760
761         /**
762          * @config_intr: Enables or disables interrupts.
763          * It should also clear any pending interrupts prior to enabling the
764          * IRQs and after disabling them.
765          */
766         int (*config_intr)(struct phy_device *phydev);
767
768         /** @handle_interrupt: Override default interrupt handling */
769         irqreturn_t (*handle_interrupt)(struct phy_device *phydev);
770
771         /** @remove: Clears up any memory if needed */
772         void (*remove)(struct phy_device *phydev);
773
774         /**
775          * @match_phy_device: Returns true if this is a suitable
776          * driver for the given phydev.  If NULL, matching is based on
777          * phy_id and phy_id_mask.
778          */
779         int (*match_phy_device)(struct phy_device *phydev);
780
781         /**
782          * @set_wol: Some devices (e.g. qnap TS-119P II) require PHY
783          * register changes to enable Wake on LAN, so set_wol is
784          * provided to be called in the ethernet driver's set_wol
785          * function.
786          */
787         int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
788
789         /**
790          * @get_wol: See set_wol, but for checking whether Wake on LAN
791          * is enabled.
792          */
793         void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
794
795         /**
796          * @link_change_notify: Called to inform a PHY device driver
797          * when the core is about to change the link state. This
798          * callback is supposed to be used as fixup hook for drivers
799          * that need to take action when the link state
800          * changes. Drivers are by no means allowed to mess with the
801          * PHY device structure in their implementations.
802          */
803         void (*link_change_notify)(struct phy_device *dev);
804
805         /**
806          * @read_mmd: PHY specific driver override for reading a MMD
807          * register.  This function is optional for PHY specific
808          * drivers.  When not provided, the default MMD read function
809          * will be used by phy_read_mmd(), which will use either a
810          * direct read for Clause 45 PHYs or an indirect read for
811          * Clause 22 PHYs.  devnum is the MMD device number within the
812          * PHY device, regnum is the register within the selected MMD
813          * device.
814          */
815         int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
816
817         /**
818          * @write_mmd: PHY specific driver override for writing a MMD
819          * register.  This function is optional for PHY specific
820          * drivers.  When not provided, the default MMD write function
821          * will be used by phy_write_mmd(), which will use either a
822          * direct write for Clause 45 PHYs, or an indirect write for
823          * Clause 22 PHYs.  devnum is the MMD device number within the
824          * PHY device, regnum is the register within the selected MMD
825          * device.  val is the value to be written.
826          */
827         int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
828                          u16 val);
829
830         /** @read_page: Return the current PHY register page number */
831         int (*read_page)(struct phy_device *dev);
832         /** @write_page: Set the current PHY register page number */
833         int (*write_page)(struct phy_device *dev, int page);
834
835         /**
836          * @module_info: Get the size and type of the eeprom contained
837          * within a plug-in module
838          */
839         int (*module_info)(struct phy_device *dev,
840                            struct ethtool_modinfo *modinfo);
841
842         /**
843          * @module_eeprom: Get the eeprom information from the plug-in
844          * module
845          */
846         int (*module_eeprom)(struct phy_device *dev,
847                              struct ethtool_eeprom *ee, u8 *data);
848
849         /** @cable_test_start: Start a cable test */
850         int (*cable_test_start)(struct phy_device *dev);
851
852         /**  @cable_test_tdr_start: Start a raw TDR cable test */
853         int (*cable_test_tdr_start)(struct phy_device *dev,
854                                     const struct phy_tdr_config *config);
855
856         /**
857          * @cable_test_get_status: Once per second, or on interrupt,
858          * request the status of the test.
859          */
860         int (*cable_test_get_status)(struct phy_device *dev, bool *finished);
861
862         /* Get statistics from the PHY using ethtool */
863         /** @get_sset_count: Number of statistic counters */
864         int (*get_sset_count)(struct phy_device *dev);
865         /** @get_strings: Names of the statistic counters */
866         void (*get_strings)(struct phy_device *dev, u8 *data);
867         /** @get_stats: Return the statistic counter values */
868         void (*get_stats)(struct phy_device *dev,
869                           struct ethtool_stats *stats, u64 *data);
870
871         /* Get and Set PHY tunables */
872         /** @get_tunable: Return the value of a tunable */
873         int (*get_tunable)(struct phy_device *dev,
874                            struct ethtool_tunable *tuna, void *data);
875         /** @set_tunable: Set the value of a tunable */
876         int (*set_tunable)(struct phy_device *dev,
877                             struct ethtool_tunable *tuna,
878                             const void *data);
879         /** @set_loopback: Set the loopback mood of the PHY */
880         int (*set_loopback)(struct phy_device *dev, bool enable);
881         /** @get_sqi: Get the signal quality indication */
882         int (*get_sqi)(struct phy_device *dev);
883         /** @get_sqi_max: Get the maximum signal quality indication */
884         int (*get_sqi_max)(struct phy_device *dev);
885 };
886 #define to_phy_driver(d) container_of(to_mdio_common_driver(d),         \
887                                       struct phy_driver, mdiodrv)
888
889 #define PHY_ANY_ID "MATCH ANY PHY"
890 #define PHY_ANY_UID 0xffffffff
891
892 #define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 0)
893 #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
894 #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
895
896 /* A Structure for boards to register fixups with the PHY Lib */
897 struct phy_fixup {
898         struct list_head list;
899         char bus_id[MII_BUS_ID_SIZE + 3];
900         u32 phy_uid;
901         u32 phy_uid_mask;
902         int (*run)(struct phy_device *phydev);
903 };
904
905 const char *phy_speed_to_str(int speed);
906 const char *phy_duplex_to_str(unsigned int duplex);
907
908 /* A structure for mapping a particular speed and duplex
909  * combination to a particular SUPPORTED and ADVERTISED value
910  */
911 struct phy_setting {
912         u32 speed;
913         u8 duplex;
914         u8 bit;
915 };
916
917 const struct phy_setting *
918 phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
919                    bool exact);
920 size_t phy_speeds(unsigned int *speeds, size_t size,
921                   unsigned long *mask);
922 void of_set_phy_supported(struct phy_device *phydev);
923 void of_set_phy_eee_broken(struct phy_device *phydev);
924 int phy_speed_down_core(struct phy_device *phydev);
925
926 /**
927  * phy_is_started - Convenience function to check whether PHY is started
928  * @phydev: The phy_device struct
929  */
930 static inline bool phy_is_started(struct phy_device *phydev)
931 {
932         return phydev->state >= PHY_UP;
933 }
934
935 void phy_resolve_aneg_pause(struct phy_device *phydev);
936 void phy_resolve_aneg_linkmode(struct phy_device *phydev);
937 void phy_check_downshift(struct phy_device *phydev);
938
939 /**
940  * phy_read - Convenience function for reading a given PHY register
941  * @phydev: the phy_device struct
942  * @regnum: register number to read
943  *
944  * NOTE: MUST NOT be called from interrupt context,
945  * because the bus read/write functions may wait for an interrupt
946  * to conclude the operation.
947  */
948 static inline int phy_read(struct phy_device *phydev, u32 regnum)
949 {
950         return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
951 }
952
953 #define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
954                                 timeout_us, sleep_before_read) \
955 ({ \
956         int __ret = read_poll_timeout(phy_read, val, (cond) || val < 0, \
957                 sleep_us, timeout_us, sleep_before_read, phydev, regnum); \
958         if (val <  0) \
959                 __ret = val; \
960         if (__ret) \
961                 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
962         __ret; \
963 })
964
965
966 /**
967  * __phy_read - convenience function for reading a given PHY register
968  * @phydev: the phy_device struct
969  * @regnum: register number to read
970  *
971  * The caller must have taken the MDIO bus lock.
972  */
973 static inline int __phy_read(struct phy_device *phydev, u32 regnum)
974 {
975         return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
976 }
977
978 /**
979  * phy_write - Convenience function for writing a given PHY register
980  * @phydev: the phy_device struct
981  * @regnum: register number to write
982  * @val: value to write to @regnum
983  *
984  * NOTE: MUST NOT be called from interrupt context,
985  * because the bus read/write functions may wait for an interrupt
986  * to conclude the operation.
987  */
988 static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
989 {
990         return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
991 }
992
993 /**
994  * __phy_write - Convenience function for writing a given PHY register
995  * @phydev: the phy_device struct
996  * @regnum: register number to write
997  * @val: value to write to @regnum
998  *
999  * The caller must have taken the MDIO bus lock.
1000  */
1001 static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
1002 {
1003         return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum,
1004                                val);
1005 }
1006
1007 /**
1008  * __phy_modify_changed() - Convenience function for modifying a PHY register
1009  * @phydev: a pointer to a &struct phy_device
1010  * @regnum: register number
1011  * @mask: bit mask of bits to clear
1012  * @set: bit mask of bits to set
1013  *
1014  * Unlocked helper function which allows a PHY register to be modified as
1015  * new register value = (old register value & ~mask) | set
1016  *
1017  * Returns negative errno, 0 if there was no change, and 1 in case of change
1018  */
1019 static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
1020                                        u16 mask, u16 set)
1021 {
1022         return __mdiobus_modify_changed(phydev->mdio.bus, phydev->mdio.addr,
1023                                         regnum, mask, set);
1024 }
1025
1026 /*
1027  * phy_read_mmd - Convenience function for reading a register
1028  * from an MMD on a given PHY.
1029  */
1030 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1031
1032 /**
1033  * phy_read_mmd_poll_timeout - Periodically poll a PHY register until a
1034  *                             condition is met or a timeout occurs
1035  *
1036  * @phydev: The phy_device struct
1037  * @devaddr: The MMD to read from
1038  * @regnum: The register on the MMD to read
1039  * @val: Variable to read the register into
1040  * @cond: Break condition (usually involving @val)
1041  * @sleep_us: Maximum time to sleep between reads in us (0
1042  *            tight-loops).  Should be less than ~20ms since usleep_range
1043  *            is used (see Documentation/timers/timers-howto.rst).
1044  * @timeout_us: Timeout in us, 0 means never timeout
1045  * @sleep_before_read: if it is true, sleep @sleep_us before read.
1046  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
1047  * case, the last read value at @args is stored in @val. Must not
1048  * be called from atomic context if sleep_us or timeout_us are used.
1049  */
1050 #define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
1051                                   sleep_us, timeout_us, sleep_before_read) \
1052 ({ \
1053         int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
1054                                   sleep_us, timeout_us, sleep_before_read, \
1055                                   phydev, devaddr, regnum); \
1056         if (val <  0) \
1057                 __ret = val; \
1058         if (__ret) \
1059                 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
1060         __ret; \
1061 })
1062
1063 /*
1064  * __phy_read_mmd - Convenience function for reading a register
1065  * from an MMD on a given PHY.
1066  */
1067 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1068
1069 /*
1070  * phy_write_mmd - Convenience function for writing a register
1071  * on an MMD on a given PHY.
1072  */
1073 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1074
1075 /*
1076  * __phy_write_mmd - Convenience function for writing a register
1077  * on an MMD on a given PHY.
1078  */
1079 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1080
1081 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1082                          u16 set);
1083 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1084                        u16 set);
1085 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1086 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1087
1088 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1089                              u16 mask, u16 set);
1090 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1091                            u16 mask, u16 set);
1092 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1093                      u16 mask, u16 set);
1094 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1095                    u16 mask, u16 set);
1096
1097 /**
1098  * __phy_set_bits - Convenience function for setting bits in a PHY register
1099  * @phydev: the phy_device struct
1100  * @regnum: register number to write
1101  * @val: bits to set
1102  *
1103  * The caller must have taken the MDIO bus lock.
1104  */
1105 static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1106 {
1107         return __phy_modify(phydev, regnum, 0, val);
1108 }
1109
1110 /**
1111  * __phy_clear_bits - Convenience function for clearing bits in a PHY register
1112  * @phydev: the phy_device struct
1113  * @regnum: register number to write
1114  * @val: bits to clear
1115  *
1116  * The caller must have taken the MDIO bus lock.
1117  */
1118 static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum,
1119                                    u16 val)
1120 {
1121         return __phy_modify(phydev, regnum, val, 0);
1122 }
1123
1124 /**
1125  * phy_set_bits - Convenience function for setting bits in a PHY register
1126  * @phydev: the phy_device struct
1127  * @regnum: register number to write
1128  * @val: bits to set
1129  */
1130 static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1131 {
1132         return phy_modify(phydev, regnum, 0, val);
1133 }
1134
1135 /**
1136  * phy_clear_bits - Convenience function for clearing bits in a PHY register
1137  * @phydev: the phy_device struct
1138  * @regnum: register number to write
1139  * @val: bits to clear
1140  */
1141 static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val)
1142 {
1143         return phy_modify(phydev, regnum, val, 0);
1144 }
1145
1146 /**
1147  * __phy_set_bits_mmd - Convenience function for setting bits in a register
1148  * on MMD
1149  * @phydev: the phy_device struct
1150  * @devad: the MMD containing register to modify
1151  * @regnum: register number to modify
1152  * @val: bits to set
1153  *
1154  * The caller must have taken the MDIO bus lock.
1155  */
1156 static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
1157                 u32 regnum, u16 val)
1158 {
1159         return __phy_modify_mmd(phydev, devad, regnum, 0, val);
1160 }
1161
1162 /**
1163  * __phy_clear_bits_mmd - Convenience function for clearing bits in a register
1164  * on MMD
1165  * @phydev: the phy_device struct
1166  * @devad: the MMD containing register to modify
1167  * @regnum: register number to modify
1168  * @val: bits to clear
1169  *
1170  * The caller must have taken the MDIO bus lock.
1171  */
1172 static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1173                 u32 regnum, u16 val)
1174 {
1175         return __phy_modify_mmd(phydev, devad, regnum, val, 0);
1176 }
1177
1178 /**
1179  * phy_set_bits_mmd - Convenience function for setting bits in a register
1180  * on MMD
1181  * @phydev: the phy_device struct
1182  * @devad: the MMD containing register to modify
1183  * @regnum: register number to modify
1184  * @val: bits to set
1185  */
1186 static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
1187                 u32 regnum, u16 val)
1188 {
1189         return phy_modify_mmd(phydev, devad, regnum, 0, val);
1190 }
1191
1192 /**
1193  * phy_clear_bits_mmd - Convenience function for clearing bits in a register
1194  * on MMD
1195  * @phydev: the phy_device struct
1196  * @devad: the MMD containing register to modify
1197  * @regnum: register number to modify
1198  * @val: bits to clear
1199  */
1200 static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1201                 u32 regnum, u16 val)
1202 {
1203         return phy_modify_mmd(phydev, devad, regnum, val, 0);
1204 }
1205
1206 /**
1207  * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
1208  * @phydev: the phy_device struct
1209  *
1210  * NOTE: must be kept in sync with addition/removal of PHY_POLL and
1211  * PHY_MAC_INTERRUPT
1212  */
1213 static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
1214 {
1215         return phydev->irq != PHY_POLL && phydev->irq != PHY_MAC_INTERRUPT;
1216 }
1217
1218 /**
1219  * phy_polling_mode - Convenience function for testing whether polling is
1220  * used to detect PHY status changes
1221  * @phydev: the phy_device struct
1222  */
1223 static inline bool phy_polling_mode(struct phy_device *phydev)
1224 {
1225         if (phydev->state == PHY_CABLETEST)
1226                 if (phydev->drv->flags & PHY_POLL_CABLE_TEST)
1227                         return true;
1228
1229         return phydev->irq == PHY_POLL;
1230 }
1231
1232 /**
1233  * phy_has_hwtstamp - Tests whether a PHY time stamp configuration.
1234  * @phydev: the phy_device struct
1235  */
1236 static inline bool phy_has_hwtstamp(struct phy_device *phydev)
1237 {
1238         return phydev && phydev->mii_ts && phydev->mii_ts->hwtstamp;
1239 }
1240
1241 /**
1242  * phy_has_rxtstamp - Tests whether a PHY supports receive time stamping.
1243  * @phydev: the phy_device struct
1244  */
1245 static inline bool phy_has_rxtstamp(struct phy_device *phydev)
1246 {
1247         return phydev && phydev->mii_ts && phydev->mii_ts->rxtstamp;
1248 }
1249
1250 /**
1251  * phy_has_tsinfo - Tests whether a PHY reports time stamping and/or
1252  * PTP hardware clock capabilities.
1253  * @phydev: the phy_device struct
1254  */
1255 static inline bool phy_has_tsinfo(struct phy_device *phydev)
1256 {
1257         return phydev && phydev->mii_ts && phydev->mii_ts->ts_info;
1258 }
1259
1260 /**
1261  * phy_has_txtstamp - Tests whether a PHY supports transmit time stamping.
1262  * @phydev: the phy_device struct
1263  */
1264 static inline bool phy_has_txtstamp(struct phy_device *phydev)
1265 {
1266         return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp;
1267 }
1268
1269 static inline int phy_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1270 {
1271         return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
1272 }
1273
1274 static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb,
1275                                 int type)
1276 {
1277         return phydev->mii_ts->rxtstamp(phydev->mii_ts, skb, type);
1278 }
1279
1280 static inline int phy_ts_info(struct phy_device *phydev,
1281                               struct ethtool_ts_info *tsinfo)
1282 {
1283         return phydev->mii_ts->ts_info(phydev->mii_ts, tsinfo);
1284 }
1285
1286 static inline void phy_txtstamp(struct phy_device *phydev, struct sk_buff *skb,
1287                                 int type)
1288 {
1289         phydev->mii_ts->txtstamp(phydev->mii_ts, skb, type);
1290 }
1291
1292 /**
1293  * phy_is_internal - Convenience function for testing if a PHY is internal
1294  * @phydev: the phy_device struct
1295  */
1296 static inline bool phy_is_internal(struct phy_device *phydev)
1297 {
1298         return phydev->is_internal;
1299 }
1300
1301 /**
1302  * phy_on_sfp - Convenience function for testing if a PHY is on an SFP module
1303  * @phydev: the phy_device struct
1304  */
1305 static inline bool phy_on_sfp(struct phy_device *phydev)
1306 {
1307         return phydev->is_on_sfp_module;
1308 }
1309
1310 /**
1311  * phy_interface_mode_is_rgmii - Convenience function for testing if a
1312  * PHY interface mode is RGMII (all variants)
1313  * @mode: the &phy_interface_t enum
1314  */
1315 static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
1316 {
1317         return mode >= PHY_INTERFACE_MODE_RGMII &&
1318                 mode <= PHY_INTERFACE_MODE_RGMII_TXID;
1319 };
1320
1321 /**
1322  * phy_interface_mode_is_8023z() - does the PHY interface mode use 802.3z
1323  *   negotiation
1324  * @mode: one of &enum phy_interface_t
1325  *
1326  * Returns true if the PHY interface mode uses the 16-bit negotiation
1327  * word as defined in 802.3z. (See 802.3-2015 37.2.1 Config_Reg encoding)
1328  */
1329 static inline bool phy_interface_mode_is_8023z(phy_interface_t mode)
1330 {
1331         return mode == PHY_INTERFACE_MODE_1000BASEX ||
1332                mode == PHY_INTERFACE_MODE_2500BASEX;
1333 }
1334
1335 /**
1336  * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
1337  * is RGMII (all variants)
1338  * @phydev: the phy_device struct
1339  */
1340 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
1341 {
1342         return phy_interface_mode_is_rgmii(phydev->interface);
1343 };
1344
1345 /**
1346  * phy_is_pseudo_fixed_link - Convenience function for testing if this
1347  * PHY is the CPU port facing side of an Ethernet switch, or similar.
1348  * @phydev: the phy_device struct
1349  */
1350 static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
1351 {
1352         return phydev->is_pseudo_fixed_link;
1353 }
1354
1355 int phy_save_page(struct phy_device *phydev);
1356 int phy_select_page(struct phy_device *phydev, int page);
1357 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
1358 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
1359 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
1360 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
1361                              u16 mask, u16 set);
1362 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
1363                      u16 mask, u16 set);
1364
1365 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
1366                                      bool is_c45,
1367                                      struct phy_c45_device_ids *c45_ids);
1368 #if IS_ENABLED(CONFIG_PHYLIB)
1369 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
1370 int phy_device_register(struct phy_device *phy);
1371 void phy_device_free(struct phy_device *phydev);
1372 #else
1373 static inline
1374 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
1375 {
1376         return NULL;
1377 }
1378
1379 static inline int phy_device_register(struct phy_device *phy)
1380 {
1381         return 0;
1382 }
1383
1384 static inline void phy_device_free(struct phy_device *phydev) { }
1385 #endif /* CONFIG_PHYLIB */
1386 void phy_device_remove(struct phy_device *phydev);
1387 int phy_init_hw(struct phy_device *phydev);
1388 int phy_suspend(struct phy_device *phydev);
1389 int phy_resume(struct phy_device *phydev);
1390 int __phy_resume(struct phy_device *phydev);
1391 int phy_loopback(struct phy_device *phydev, bool enable);
1392 void phy_sfp_attach(void *upstream, struct sfp_bus *bus);
1393 void phy_sfp_detach(void *upstream, struct sfp_bus *bus);
1394 int phy_sfp_probe(struct phy_device *phydev,
1395                   const struct sfp_upstream_ops *ops);
1396 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1397                               phy_interface_t interface);
1398 struct phy_device *phy_find_first(struct mii_bus *bus);
1399 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1400                       u32 flags, phy_interface_t interface);
1401 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1402                        void (*handler)(struct net_device *),
1403                        phy_interface_t interface);
1404 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1405                                void (*handler)(struct net_device *),
1406                                phy_interface_t interface);
1407 void phy_disconnect(struct phy_device *phydev);
1408 void phy_detach(struct phy_device *phydev);
1409 void phy_start(struct phy_device *phydev);
1410 void phy_stop(struct phy_device *phydev);
1411 int phy_start_aneg(struct phy_device *phydev);
1412 int phy_aneg_done(struct phy_device *phydev);
1413 int phy_speed_down(struct phy_device *phydev, bool sync);
1414 int phy_speed_up(struct phy_device *phydev);
1415
1416 int phy_restart_aneg(struct phy_device *phydev);
1417 int phy_reset_after_clk_enable(struct phy_device *phydev);
1418
1419 #if IS_ENABLED(CONFIG_PHYLIB)
1420 int phy_start_cable_test(struct phy_device *phydev,
1421                          struct netlink_ext_ack *extack);
1422 int phy_start_cable_test_tdr(struct phy_device *phydev,
1423                              struct netlink_ext_ack *extack,
1424                              const struct phy_tdr_config *config);
1425 #else
1426 static inline
1427 int phy_start_cable_test(struct phy_device *phydev,
1428                          struct netlink_ext_ack *extack)
1429 {
1430         NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1431         return -EOPNOTSUPP;
1432 }
1433 static inline
1434 int phy_start_cable_test_tdr(struct phy_device *phydev,
1435                              struct netlink_ext_ack *extack,
1436                              const struct phy_tdr_config *config)
1437 {
1438         NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1439         return -EOPNOTSUPP;
1440 }
1441 #endif
1442
1443 int phy_cable_test_result(struct phy_device *phydev, u8 pair, u16 result);
1444 int phy_cable_test_fault_length(struct phy_device *phydev, u8 pair,
1445                                 u16 cm);
1446
1447 static inline void phy_device_reset(struct phy_device *phydev, int value)
1448 {
1449         mdio_device_reset(&phydev->mdio, value);
1450 }
1451
1452 #define phydev_err(_phydev, format, args...)    \
1453         dev_err(&_phydev->mdio.dev, format, ##args)
1454
1455 #define phydev_info(_phydev, format, args...)   \
1456         dev_info(&_phydev->mdio.dev, format, ##args)
1457
1458 #define phydev_warn(_phydev, format, args...)   \
1459         dev_warn(&_phydev->mdio.dev, format, ##args)
1460
1461 #define phydev_dbg(_phydev, format, args...)    \
1462         dev_dbg(&_phydev->mdio.dev, format, ##args)
1463
1464 static inline const char *phydev_name(const struct phy_device *phydev)
1465 {
1466         return dev_name(&phydev->mdio.dev);
1467 }
1468
1469 static inline void phy_lock_mdio_bus(struct phy_device *phydev)
1470 {
1471         mutex_lock(&phydev->mdio.bus->mdio_lock);
1472 }
1473
1474 static inline void phy_unlock_mdio_bus(struct phy_device *phydev)
1475 {
1476         mutex_unlock(&phydev->mdio.bus->mdio_lock);
1477 }
1478
1479 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1480         __printf(2, 3);
1481 char *phy_attached_info_irq(struct phy_device *phydev)
1482         __malloc;
1483 void phy_attached_info(struct phy_device *phydev);
1484
1485 /* Clause 22 PHY */
1486 int genphy_read_abilities(struct phy_device *phydev);
1487 int genphy_setup_forced(struct phy_device *phydev);
1488 int genphy_restart_aneg(struct phy_device *phydev);
1489 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1490 int genphy_config_eee_advert(struct phy_device *phydev);
1491 int __genphy_config_aneg(struct phy_device *phydev, bool changed);
1492 int genphy_aneg_done(struct phy_device *phydev);
1493 int genphy_update_link(struct phy_device *phydev);
1494 int genphy_read_lpa(struct phy_device *phydev);
1495 int genphy_read_status_fixed(struct phy_device *phydev);
1496 int genphy_read_status(struct phy_device *phydev);
1497 int genphy_suspend(struct phy_device *phydev);
1498 int genphy_resume(struct phy_device *phydev);
1499 int genphy_loopback(struct phy_device *phydev, bool enable);
1500 int genphy_soft_reset(struct phy_device *phydev);
1501 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev);
1502
1503 static inline int genphy_config_aneg(struct phy_device *phydev)
1504 {
1505         return __genphy_config_aneg(phydev, false);
1506 }
1507
1508 static inline int genphy_no_config_intr(struct phy_device *phydev)
1509 {
1510         return 0;
1511 }
1512 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
1513                                 u16 regnum);
1514 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1515                                  u16 regnum, u16 val);
1516
1517 /* Clause 37 */
1518 int genphy_c37_config_aneg(struct phy_device *phydev);
1519 int genphy_c37_read_status(struct phy_device *phydev);
1520
1521 /* Clause 45 PHY */
1522 int genphy_c45_restart_aneg(struct phy_device *phydev);
1523 int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1524 int genphy_c45_aneg_done(struct phy_device *phydev);
1525 int genphy_c45_read_link(struct phy_device *phydev);
1526 int genphy_c45_read_lpa(struct phy_device *phydev);
1527 int genphy_c45_read_pma(struct phy_device *phydev);
1528 int genphy_c45_pma_setup_forced(struct phy_device *phydev);
1529 int genphy_c45_an_config_aneg(struct phy_device *phydev);
1530 int genphy_c45_an_disable_aneg(struct phy_device *phydev);
1531 int genphy_c45_read_mdix(struct phy_device *phydev);
1532 int genphy_c45_pma_read_abilities(struct phy_device *phydev);
1533 int genphy_c45_read_status(struct phy_device *phydev);
1534 int genphy_c45_config_aneg(struct phy_device *phydev);
1535
1536 /* Generic C45 PHY driver */
1537 extern struct phy_driver genphy_c45_driver;
1538
1539 /* The gen10g_* functions are the old Clause 45 stub */
1540 int gen10g_config_aneg(struct phy_device *phydev);
1541
1542 static inline int phy_read_status(struct phy_device *phydev)
1543 {
1544         if (!phydev->drv)
1545                 return -EIO;
1546
1547         if (phydev->drv->read_status)
1548                 return phydev->drv->read_status(phydev);
1549         else
1550                 return genphy_read_status(phydev);
1551 }
1552
1553 void phy_driver_unregister(struct phy_driver *drv);
1554 void phy_drivers_unregister(struct phy_driver *drv, int n);
1555 int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
1556 int phy_drivers_register(struct phy_driver *new_driver, int n,
1557                          struct module *owner);
1558 void phy_error(struct phy_device *phydev);
1559 void phy_state_machine(struct work_struct *work);
1560 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
1561 void phy_trigger_machine(struct phy_device *phydev);
1562 void phy_mac_interrupt(struct phy_device *phydev);
1563 void phy_start_machine(struct phy_device *phydev);
1564 void phy_stop_machine(struct phy_device *phydev);
1565 void phy_ethtool_ksettings_get(struct phy_device *phydev,
1566                                struct ethtool_link_ksettings *cmd);
1567 int phy_ethtool_ksettings_set(struct phy_device *phydev,
1568                               const struct ethtool_link_ksettings *cmd);
1569 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
1570 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
1571 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd);
1572 int phy_disable_interrupts(struct phy_device *phydev);
1573 void phy_request_interrupt(struct phy_device *phydev);
1574 void phy_free_interrupt(struct phy_device *phydev);
1575 void phy_print_status(struct phy_device *phydev);
1576 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
1577 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
1578 void phy_advertise_supported(struct phy_device *phydev);
1579 void phy_support_sym_pause(struct phy_device *phydev);
1580 void phy_support_asym_pause(struct phy_device *phydev);
1581 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
1582                        bool autoneg);
1583 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
1584 bool phy_validate_pause(struct phy_device *phydev,
1585                         struct ethtool_pauseparam *pp);
1586 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause);
1587
1588 s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
1589                            const int *delay_values, int size, bool is_rx);
1590
1591 void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv,
1592                        bool *tx_pause, bool *rx_pause);
1593
1594 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
1595                        int (*run)(struct phy_device *));
1596 int phy_register_fixup_for_id(const char *bus_id,
1597                               int (*run)(struct phy_device *));
1598 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
1599                                int (*run)(struct phy_device *));
1600
1601 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
1602 int phy_unregister_fixup_for_id(const char *bus_id);
1603 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
1604
1605 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
1606 int phy_get_eee_err(struct phy_device *phydev);
1607 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
1608 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
1609 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
1610 void phy_ethtool_get_wol(struct phy_device *phydev,
1611                          struct ethtool_wolinfo *wol);
1612 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1613                                    struct ethtool_link_ksettings *cmd);
1614 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1615                                    const struct ethtool_link_ksettings *cmd);
1616 int phy_ethtool_nway_reset(struct net_device *ndev);
1617 int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size);
1618 void phy_package_leave(struct phy_device *phydev);
1619 int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
1620                           int addr, size_t priv_size);
1621
1622 #if IS_ENABLED(CONFIG_PHYLIB)
1623 int __init mdio_bus_init(void);
1624 void mdio_bus_exit(void);
1625 #endif
1626
1627 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data);
1628 int phy_ethtool_get_sset_count(struct phy_device *phydev);
1629 int phy_ethtool_get_stats(struct phy_device *phydev,
1630                           struct ethtool_stats *stats, u64 *data);
1631
1632 static inline int phy_package_read(struct phy_device *phydev, u32 regnum)
1633 {
1634         struct phy_package_shared *shared = phydev->shared;
1635
1636         if (!shared)
1637                 return -EIO;
1638
1639         return mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
1640 }
1641
1642 static inline int __phy_package_read(struct phy_device *phydev, u32 regnum)
1643 {
1644         struct phy_package_shared *shared = phydev->shared;
1645
1646         if (!shared)
1647                 return -EIO;
1648
1649         return __mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
1650 }
1651
1652 static inline int phy_package_write(struct phy_device *phydev,
1653                                     u32 regnum, u16 val)
1654 {
1655         struct phy_package_shared *shared = phydev->shared;
1656
1657         if (!shared)
1658                 return -EIO;
1659
1660         return mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
1661 }
1662
1663 static inline int __phy_package_write(struct phy_device *phydev,
1664                                       u32 regnum, u16 val)
1665 {
1666         struct phy_package_shared *shared = phydev->shared;
1667
1668         if (!shared)
1669                 return -EIO;
1670
1671         return __mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
1672 }
1673
1674 static inline bool __phy_package_set_once(struct phy_device *phydev,
1675                                           unsigned int b)
1676 {
1677         struct phy_package_shared *shared = phydev->shared;
1678
1679         if (!shared)
1680                 return false;
1681
1682         return !test_and_set_bit(b, &shared->flags);
1683 }
1684
1685 static inline bool phy_package_init_once(struct phy_device *phydev)
1686 {
1687         return __phy_package_set_once(phydev, PHY_SHARED_F_INIT_DONE);
1688 }
1689
1690 static inline bool phy_package_probe_once(struct phy_device *phydev)
1691 {
1692         return __phy_package_set_once(phydev, PHY_SHARED_F_PROBE_DONE);
1693 }
1694
1695 extern struct bus_type mdio_bus_type;
1696
1697 struct mdio_board_info {
1698         const char      *bus_id;
1699         char            modalias[MDIO_NAME_SIZE];
1700         int             mdio_addr;
1701         const void      *platform_data;
1702 };
1703
1704 #if IS_ENABLED(CONFIG_MDIO_DEVICE)
1705 int mdiobus_register_board_info(const struct mdio_board_info *info,
1706                                 unsigned int n);
1707 #else
1708 static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
1709                                               unsigned int n)
1710 {
1711         return 0;
1712 }
1713 #endif
1714
1715
1716 /**
1717  * phy_module_driver() - Helper macro for registering PHY drivers
1718  * @__phy_drivers: array of PHY drivers to register
1719  * @__count: Numbers of members in array
1720  *
1721  * Helper macro for PHY drivers which do not do anything special in module
1722  * init/exit. Each module may only use this macro once, and calling it
1723  * replaces module_init() and module_exit().
1724  */
1725 #define phy_module_driver(__phy_drivers, __count)                       \
1726 static int __init phy_module_init(void)                                 \
1727 {                                                                       \
1728         return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
1729 }                                                                       \
1730 module_init(phy_module_init);                                           \
1731 static void __exit phy_module_exit(void)                                \
1732 {                                                                       \
1733         phy_drivers_unregister(__phy_drivers, __count);                 \
1734 }                                                                       \
1735 module_exit(phy_module_exit)
1736
1737 #define module_phy_driver(__phy_drivers)                                \
1738         phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
1739
1740 bool phy_driver_is_genphy(struct phy_device *phydev);
1741 bool phy_driver_is_genphy_10g(struct phy_device *phydev);
1742
1743 #endif /* __PHY_H */