xref: /linux-6.15/include/linux/phy.h (revision 3e64db35)
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  * @phyindex: Unique id across the phy's parent tree of phys to address the PHY
548  *	      from userspace, similar to ifindex. A zero index means the PHY
549  *	      wasn't assigned an id yet.
550  * @phy_id: UID for this device found during discovery
551  * @c45_ids: 802.3-c45 Device Identifiers if is_c45.
552  * @is_c45:  Set to true if this PHY uses clause 45 addressing.
553  * @is_internal: Set to true if this PHY is internal to a MAC.
554  * @is_pseudo_fixed_link: Set to true if this PHY is an Ethernet switch, etc.
555  * @is_gigabit_capable: Set to true if PHY supports 1000Mbps
556  * @has_fixups: Set to true if this PHY has fixups/quirks.
557  * @suspended: Set to true if this PHY has been suspended successfully.
558  * @suspended_by_mdio_bus: Set to true if this PHY was suspended by MDIO bus.
559  * @sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
560  * @loopback_enabled: Set true if this PHY has been loopbacked successfully.
561  * @downshifted_rate: Set true if link speed has been downshifted.
562  * @is_on_sfp_module: Set true if PHY is located on an SFP module.
563  * @mac_managed_pm: Set true if MAC driver takes of suspending/resuming PHY
564  * @wol_enabled: Set to true if the PHY or the attached MAC have Wake-on-LAN
565  * 		 enabled.
566  * @state: State of the PHY for management purposes
567  * @dev_flags: Device-specific flags used by the PHY driver.
568  *
569  *      - Bits [15:0] are free to use by the PHY driver to communicate
570  *        driver specific behavior.
571  *      - Bits [23:16] are currently reserved for future use.
572  *      - Bits [31:24] are reserved for defining generic
573  *        PHY driver behavior.
574  * @irq: IRQ number of the PHY's interrupt (-1 if none)
575  * @phy_timer: The timer for handling the state machine
576  * @phylink: Pointer to phylink instance for this PHY
577  * @sfp_bus_attached: Flag indicating whether the SFP bus has been attached
578  * @sfp_bus: SFP bus attached to this PHY's fiber port
579  * @attached_dev: The attached enet driver's device instance ptr
580  * @adjust_link: Callback for the enet controller to respond to changes: in the
581  *               link state.
582  * @phy_link_change: Callback for phylink for notification of link change
583  * @macsec_ops: MACsec offloading ops.
584  *
585  * @speed: Current link speed
586  * @duplex: Current duplex
587  * @port: Current port
588  * @pause: Current pause
589  * @asym_pause: Current asymmetric pause
590  * @supported: Combined MAC/PHY supported linkmodes
591  * @advertising: Currently advertised linkmodes
592  * @adv_old: Saved advertised while power saving for WoL
593  * @supported_eee: supported PHY EEE linkmodes
594  * @advertising_eee: Currently advertised EEE linkmodes
595  * @eee_enabled: Flag indicating whether the EEE feature is enabled
596  * @lp_advertising: Current link partner advertised linkmodes
597  * @host_interfaces: PHY interface modes supported by host
598  * @eee_broken_modes: Energy efficient ethernet modes which should be prohibited
599  * @autoneg: Flag autoneg being used
600  * @rate_matching: Current rate matching mode
601  * @link: Current link state
602  * @autoneg_complete: Flag auto negotiation of the link has completed
603  * @mdix: Current crossover
604  * @mdix_ctrl: User setting of crossover
605  * @pma_extable: Cached value of PMA/PMD Extended Abilities Register
606  * @interrupts: Flag interrupts have been enabled
607  * @irq_suspended: Flag indicating PHY is suspended and therefore interrupt
608  *                 handling shall be postponed until PHY has resumed
609  * @irq_rerun: Flag indicating interrupts occurred while PHY was suspended,
610  *             requiring a rerun of the interrupt handler after resume
611  * @interface: enum phy_interface_t value
612  * @possible_interfaces: bitmap if interface modes that the attached PHY
613  *			 will switch between depending on media speed.
614  * @skb: Netlink message for cable diagnostics
615  * @nest: Netlink nest used for cable diagnostics
616  * @ehdr: nNtlink header for cable diagnostics
617  * @phy_led_triggers: Array of LED triggers
618  * @phy_num_led_triggers: Number of triggers in @phy_led_triggers
619  * @led_link_trigger: LED trigger for link up/down
620  * @last_triggered: last LED trigger for link speed
621  * @leds: list of PHY LED structures
622  * @master_slave_set: User requested master/slave configuration
623  * @master_slave_get: Current master/slave advertisement
624  * @master_slave_state: Current master/slave configuration
625  * @mii_ts: Pointer to time stamper callbacks
626  * @psec: Pointer to Power Sourcing Equipment control struct
627  * @lock:  Mutex for serialization access to PHY
628  * @state_queue: Work queue for state machine
629  * @link_down_events: Number of times link was lost
630  * @shared: Pointer to private data shared by phys in one package
631  * @priv: Pointer to driver private data
632  *
633  * interrupts currently only supports enabled or disabled,
634  * but could be changed in the future to support enabling
635  * and disabling specific interrupts
636  *
637  * Contains some infrastructure for polling and interrupt
638  * handling, as well as handling shifts in PHY hardware state
639  */
640 struct phy_device {
641 	struct mdio_device mdio;
642 
643 	/* Information about the PHY type */
644 	/* And management functions */
645 	struct phy_driver *drv;
646 
647 	struct device_link *devlink;
648 
649 	u32 phyindex;
650 	u32 phy_id;
651 
652 	struct phy_c45_device_ids c45_ids;
653 	unsigned is_c45:1;
654 	unsigned is_internal:1;
655 	unsigned is_pseudo_fixed_link:1;
656 	unsigned is_gigabit_capable:1;
657 	unsigned has_fixups:1;
658 	unsigned suspended:1;
659 	unsigned suspended_by_mdio_bus:1;
660 	unsigned sysfs_links:1;
661 	unsigned loopback_enabled:1;
662 	unsigned downshifted_rate:1;
663 	unsigned is_on_sfp_module:1;
664 	unsigned mac_managed_pm:1;
665 	unsigned wol_enabled:1;
666 
667 	unsigned autoneg:1;
668 	/* The most recently read link state */
669 	unsigned link:1;
670 	unsigned autoneg_complete:1;
671 
672 	/* Interrupts are enabled */
673 	unsigned interrupts:1;
674 	unsigned irq_suspended:1;
675 	unsigned irq_rerun:1;
676 
677 	int rate_matching;
678 
679 	enum phy_state state;
680 
681 	u32 dev_flags;
682 
683 	phy_interface_t interface;
684 	DECLARE_PHY_INTERFACE_MASK(possible_interfaces);
685 
686 	/*
687 	 * forced speed & duplex (no autoneg)
688 	 * partner speed & duplex & pause (autoneg)
689 	 */
690 	int speed;
691 	int duplex;
692 	int port;
693 	int pause;
694 	int asym_pause;
695 	u8 master_slave_get;
696 	u8 master_slave_set;
697 	u8 master_slave_state;
698 
699 	/* Union of PHY and Attached devices' supported link modes */
700 	/* See ethtool.h for more info */
701 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
702 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
703 	__ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
704 	/* used with phy_speed_down */
705 	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
706 	/* used for eee validation */
707 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported_eee);
708 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising_eee);
709 	bool eee_enabled;
710 
711 	/* Host supported PHY interface types. Should be ignored if empty. */
712 	DECLARE_PHY_INTERFACE_MASK(host_interfaces);
713 
714 	/* Energy efficient ethernet modes which should be prohibited */
715 	u32 eee_broken_modes;
716 
717 #ifdef CONFIG_LED_TRIGGER_PHY
718 	struct phy_led_trigger *phy_led_triggers;
719 	unsigned int phy_num_led_triggers;
720 	struct phy_led_trigger *last_triggered;
721 
722 	struct phy_led_trigger *led_link_trigger;
723 #endif
724 	struct list_head leds;
725 
726 	/*
727 	 * Interrupt number for this PHY
728 	 * -1 means no interrupt
729 	 */
730 	int irq;
731 
732 	/* private data pointer */
733 	/* For use by PHYs to maintain extra state */
734 	void *priv;
735 
736 	/* shared data pointer */
737 	/* For use by PHYs inside the same package that need a shared state. */
738 	struct phy_package_shared *shared;
739 
740 	/* Reporting cable test results */
741 	struct sk_buff *skb;
742 	void *ehdr;
743 	struct nlattr *nest;
744 
745 	/* Interrupt and Polling infrastructure */
746 	struct delayed_work state_queue;
747 
748 	struct mutex lock;
749 
750 	/* This may be modified under the rtnl lock */
751 	bool sfp_bus_attached;
752 	struct sfp_bus *sfp_bus;
753 	struct phylink *phylink;
754 	struct net_device *attached_dev;
755 	struct mii_timestamper *mii_ts;
756 	struct pse_control *psec;
757 
758 	u8 mdix;
759 	u8 mdix_ctrl;
760 
761 	int pma_extable;
762 
763 	unsigned int link_down_events;
764 
765 	void (*phy_link_change)(struct phy_device *phydev, bool up);
766 	void (*adjust_link)(struct net_device *dev);
767 
768 #if IS_ENABLED(CONFIG_MACSEC)
769 	/* MACsec management functions */
770 	const struct macsec_ops *macsec_ops;
771 #endif
772 };
773 
774 /* Generic phy_device::dev_flags */
775 #define PHY_F_NO_IRQ		0x80000000
776 
777 static inline struct phy_device *to_phy_device(const struct device *dev)
778 {
779 	return container_of(to_mdio_device(dev), struct phy_device, mdio);
780 }
781 
782 /**
783  * struct phy_tdr_config - Configuration of a TDR raw test
784  *
785  * @first: Distance for first data collection point
786  * @last: Distance for last data collection point
787  * @step: Step between data collection points
788  * @pair: Bitmap of cable pairs to collect data for
789  *
790  * A structure containing possible configuration parameters
791  * for a TDR cable test. The driver does not need to implement
792  * all the parameters, but should report what is actually used.
793  * All distances are in centimeters.
794  */
795 struct phy_tdr_config {
796 	u32 first;
797 	u32 last;
798 	u32 step;
799 	s8 pair;
800 };
801 #define PHY_PAIR_ALL -1
802 
803 /**
804  * struct phy_plca_cfg - Configuration of the PLCA (Physical Layer Collision
805  * Avoidance) Reconciliation Sublayer.
806  *
807  * @version: read-only PLCA register map version. -1 = not available. Ignored
808  *   when setting the configuration. Format is the same as reported by the PLCA
809  *   IDVER register (31.CA00). -1 = not available.
810  * @enabled: PLCA configured mode (enabled/disabled). -1 = not available / don't
811  *   set. 0 = disabled, anything else = enabled.
812  * @node_id: the PLCA local node identifier. -1 = not available / don't set.
813  *   Allowed values [0 .. 254]. 255 = node disabled.
814  * @node_cnt: the PLCA node count (maximum number of nodes having a TO). Only
815  *   meaningful for the coordinator (node_id = 0). -1 = not available / don't
816  *   set. Allowed values [1 .. 255].
817  * @to_tmr: The value of the PLCA to_timer in bit-times, which determines the
818  *   PLCA transmit opportunity window opening. See IEEE802.3 Clause 148 for
819  *   more details. The to_timer shall be set equal over all nodes.
820  *   -1 = not available / don't set. Allowed values [0 .. 255].
821  * @burst_cnt: controls how many additional frames a node is allowed to send in
822  *   single transmit opportunity (TO). The default value of 0 means that the
823  *   node is allowed exactly one frame per TO. A value of 1 allows two frames
824  *   per TO, and so on. -1 = not available / don't set.
825  *   Allowed values [0 .. 255].
826  * @burst_tmr: controls how many bit times to wait for the MAC to send a new
827  *   frame before interrupting the burst. This value should be set to a value
828  *   greater than the MAC inter-packet gap (which is typically 96 bits).
829  *   -1 = not available / don't set. Allowed values [0 .. 255].
830  *
831  * A structure containing configuration parameters for setting/getting the PLCA
832  * RS configuration. The driver does not need to implement all the parameters,
833  * but should report what is actually used.
834  */
835 struct phy_plca_cfg {
836 	int version;
837 	int enabled;
838 	int node_id;
839 	int node_cnt;
840 	int to_tmr;
841 	int burst_cnt;
842 	int burst_tmr;
843 };
844 
845 /**
846  * struct phy_plca_status - Status of the PLCA (Physical Layer Collision
847  * Avoidance) Reconciliation Sublayer.
848  *
849  * @pst: The PLCA status as reported by the PST bit in the PLCA STATUS
850  *	register(31.CA03), indicating BEACON activity.
851  *
852  * A structure containing status information of the PLCA RS configuration.
853  * The driver does not need to implement all the parameters, but should report
854  * what is actually used.
855  */
856 struct phy_plca_status {
857 	bool pst;
858 };
859 
860 /**
861  * struct phy_led: An LED driven by the PHY
862  *
863  * @list: List of LEDs
864  * @phydev: PHY this LED is attached to
865  * @led_cdev: Standard LED class structure
866  * @index: Number of the LED
867  */
868 struct phy_led {
869 	struct list_head list;
870 	struct phy_device *phydev;
871 	struct led_classdev led_cdev;
872 	u8 index;
873 };
874 
875 #define to_phy_led(d) container_of(d, struct phy_led, led_cdev)
876 
877 /**
878  * struct phy_driver - Driver structure for a particular PHY type
879  *
880  * @mdiodrv: Data common to all MDIO devices
881  * @phy_id: The result of reading the UID registers of this PHY
882  *   type, and ANDing them with the phy_id_mask.  This driver
883  *   only works for PHYs with IDs which match this field
884  * @name: The friendly name of this PHY type
885  * @phy_id_mask: Defines the important bits of the phy_id
886  * @features: A mandatory list of features (speed, duplex, etc)
887  *   supported by this PHY
888  * @flags: A bitfield defining certain other features this PHY
889  *   supports (like interrupts)
890  * @driver_data: Static driver data
891  *
892  * All functions are optional. If config_aneg or read_status
893  * are not implemented, the phy core uses the genphy versions.
894  * Note that none of these functions should be called from
895  * interrupt time. The goal is for the bus read/write functions
896  * to be able to block when the bus transaction is happening,
897  * and be freed up by an interrupt (The MPC85xx has this ability,
898  * though it is not currently supported in the driver).
899  */
900 struct phy_driver {
901 	struct mdio_driver_common mdiodrv;
902 	u32 phy_id;
903 	char *name;
904 	u32 phy_id_mask;
905 	const unsigned long * const features;
906 	u32 flags;
907 	const void *driver_data;
908 
909 	/**
910 	 * @soft_reset: Called to issue a PHY software reset
911 	 */
912 	int (*soft_reset)(struct phy_device *phydev);
913 
914 	/**
915 	 * @config_init: Called to initialize the PHY,
916 	 * including after a reset
917 	 */
918 	int (*config_init)(struct phy_device *phydev);
919 
920 	/**
921 	 * @probe: Called during discovery.  Used to set
922 	 * up device-specific structures, if any
923 	 */
924 	int (*probe)(struct phy_device *phydev);
925 
926 	/**
927 	 * @get_features: Probe the hardware to determine what
928 	 * abilities it has.  Should only set phydev->supported.
929 	 */
930 	int (*get_features)(struct phy_device *phydev);
931 
932 	/**
933 	 * @get_rate_matching: Get the supported type of rate matching for a
934 	 * particular phy interface. This is used by phy consumers to determine
935 	 * whether to advertise lower-speed modes for that interface. It is
936 	 * assumed that if a rate matching mode is supported on an interface,
937 	 * then that interface's rate can be adapted to all slower link speeds
938 	 * supported by the phy. If the interface is not supported, this should
939 	 * return %RATE_MATCH_NONE.
940 	 */
941 	int (*get_rate_matching)(struct phy_device *phydev,
942 				   phy_interface_t iface);
943 
944 	/* PHY Power Management */
945 	/** @suspend: Suspend the hardware, saving state if needed */
946 	int (*suspend)(struct phy_device *phydev);
947 	/** @resume: Resume the hardware, restoring state if needed */
948 	int (*resume)(struct phy_device *phydev);
949 
950 	/**
951 	 * @config_aneg: Configures the advertisement and resets
952 	 * autonegotiation if phydev->autoneg is on,
953 	 * forces the speed to the current settings in phydev
954 	 * if phydev->autoneg is off
955 	 */
956 	int (*config_aneg)(struct phy_device *phydev);
957 
958 	/** @aneg_done: Determines the auto negotiation result */
959 	int (*aneg_done)(struct phy_device *phydev);
960 
961 	/** @read_status: Determines the negotiated speed and duplex */
962 	int (*read_status)(struct phy_device *phydev);
963 
964 	/**
965 	 * @config_intr: Enables or disables interrupts.
966 	 * It should also clear any pending interrupts prior to enabling the
967 	 * IRQs and after disabling them.
968 	 */
969 	int (*config_intr)(struct phy_device *phydev);
970 
971 	/** @handle_interrupt: Override default interrupt handling */
972 	irqreturn_t (*handle_interrupt)(struct phy_device *phydev);
973 
974 	/** @remove: Clears up any memory if needed */
975 	void (*remove)(struct phy_device *phydev);
976 
977 	/**
978 	 * @match_phy_device: Returns true if this is a suitable
979 	 * driver for the given phydev.	 If NULL, matching is based on
980 	 * phy_id and phy_id_mask.
981 	 */
982 	int (*match_phy_device)(struct phy_device *phydev);
983 
984 	/**
985 	 * @set_wol: Some devices (e.g. qnap TS-119P II) require PHY
986 	 * register changes to enable Wake on LAN, so set_wol is
987 	 * provided to be called in the ethernet driver's set_wol
988 	 * function.
989 	 */
990 	int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
991 
992 	/**
993 	 * @get_wol: See set_wol, but for checking whether Wake on LAN
994 	 * is enabled.
995 	 */
996 	void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
997 
998 	/**
999 	 * @link_change_notify: Called to inform a PHY device driver
1000 	 * when the core is about to change the link state. This
1001 	 * callback is supposed to be used as fixup hook for drivers
1002 	 * that need to take action when the link state
1003 	 * changes. Drivers are by no means allowed to mess with the
1004 	 * PHY device structure in their implementations.
1005 	 */
1006 	void (*link_change_notify)(struct phy_device *dev);
1007 
1008 	/**
1009 	 * @read_mmd: PHY specific driver override for reading a MMD
1010 	 * register.  This function is optional for PHY specific
1011 	 * drivers.  When not provided, the default MMD read function
1012 	 * will be used by phy_read_mmd(), which will use either a
1013 	 * direct read for Clause 45 PHYs or an indirect read for
1014 	 * Clause 22 PHYs.  devnum is the MMD device number within the
1015 	 * PHY device, regnum is the register within the selected MMD
1016 	 * device.
1017 	 */
1018 	int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
1019 
1020 	/**
1021 	 * @write_mmd: PHY specific driver override for writing a MMD
1022 	 * register.  This function is optional for PHY specific
1023 	 * drivers.  When not provided, the default MMD write function
1024 	 * will be used by phy_write_mmd(), which will use either a
1025 	 * direct write for Clause 45 PHYs, or an indirect write for
1026 	 * Clause 22 PHYs.  devnum is the MMD device number within the
1027 	 * PHY device, regnum is the register within the selected MMD
1028 	 * device.  val is the value to be written.
1029 	 */
1030 	int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
1031 			 u16 val);
1032 
1033 	/** @read_page: Return the current PHY register page number */
1034 	int (*read_page)(struct phy_device *dev);
1035 	/** @write_page: Set the current PHY register page number */
1036 	int (*write_page)(struct phy_device *dev, int page);
1037 
1038 	/**
1039 	 * @module_info: Get the size and type of the eeprom contained
1040 	 * within a plug-in module
1041 	 */
1042 	int (*module_info)(struct phy_device *dev,
1043 			   struct ethtool_modinfo *modinfo);
1044 
1045 	/**
1046 	 * @module_eeprom: Get the eeprom information from the plug-in
1047 	 * module
1048 	 */
1049 	int (*module_eeprom)(struct phy_device *dev,
1050 			     struct ethtool_eeprom *ee, u8 *data);
1051 
1052 	/** @cable_test_start: Start a cable test */
1053 	int (*cable_test_start)(struct phy_device *dev);
1054 
1055 	/**  @cable_test_tdr_start: Start a raw TDR cable test */
1056 	int (*cable_test_tdr_start)(struct phy_device *dev,
1057 				    const struct phy_tdr_config *config);
1058 
1059 	/**
1060 	 * @cable_test_get_status: Once per second, or on interrupt,
1061 	 * request the status of the test.
1062 	 */
1063 	int (*cable_test_get_status)(struct phy_device *dev, bool *finished);
1064 
1065 	/* Get statistics from the PHY using ethtool */
1066 	/** @get_sset_count: Number of statistic counters */
1067 	int (*get_sset_count)(struct phy_device *dev);
1068 	/** @get_strings: Names of the statistic counters */
1069 	void (*get_strings)(struct phy_device *dev, u8 *data);
1070 	/** @get_stats: Return the statistic counter values */
1071 	void (*get_stats)(struct phy_device *dev,
1072 			  struct ethtool_stats *stats, u64 *data);
1073 
1074 	/* Get and Set PHY tunables */
1075 	/** @get_tunable: Return the value of a tunable */
1076 	int (*get_tunable)(struct phy_device *dev,
1077 			   struct ethtool_tunable *tuna, void *data);
1078 	/** @set_tunable: Set the value of a tunable */
1079 	int (*set_tunable)(struct phy_device *dev,
1080 			    struct ethtool_tunable *tuna,
1081 			    const void *data);
1082 	/** @set_loopback: Set the loopback mood of the PHY */
1083 	int (*set_loopback)(struct phy_device *dev, bool enable);
1084 	/** @get_sqi: Get the signal quality indication */
1085 	int (*get_sqi)(struct phy_device *dev);
1086 	/** @get_sqi_max: Get the maximum signal quality indication */
1087 	int (*get_sqi_max)(struct phy_device *dev);
1088 
1089 	/* PLCA RS interface */
1090 	/** @get_plca_cfg: Return the current PLCA configuration */
1091 	int (*get_plca_cfg)(struct phy_device *dev,
1092 			    struct phy_plca_cfg *plca_cfg);
1093 	/** @set_plca_cfg: Set the PLCA configuration */
1094 	int (*set_plca_cfg)(struct phy_device *dev,
1095 			    const struct phy_plca_cfg *plca_cfg);
1096 	/** @get_plca_status: Return the current PLCA status info */
1097 	int (*get_plca_status)(struct phy_device *dev,
1098 			       struct phy_plca_status *plca_st);
1099 
1100 	/**
1101 	 * @led_brightness_set: Set a PHY LED brightness. Index
1102 	 * indicates which of the PHYs led should be set. Value
1103 	 * follows the standard LED class meaning, e.g. LED_OFF,
1104 	 * LED_HALF, LED_FULL.
1105 	 */
1106 	int (*led_brightness_set)(struct phy_device *dev,
1107 				  u8 index, enum led_brightness value);
1108 
1109 	/**
1110 	 * @led_blink_set: Set a PHY LED brightness.  Index indicates
1111 	 * which of the PHYs led should be configured to blink. Delays
1112 	 * are in milliseconds and if both are zero then a sensible
1113 	 * default should be chosen.  The call should adjust the
1114 	 * timings in that case and if it can't match the values
1115 	 * specified exactly.
1116 	 */
1117 	int (*led_blink_set)(struct phy_device *dev, u8 index,
1118 			     unsigned long *delay_on,
1119 			     unsigned long *delay_off);
1120 	/**
1121 	 * @led_hw_is_supported: Can the HW support the given rules.
1122 	 * @dev: PHY device which has the LED
1123 	 * @index: Which LED of the PHY device
1124 	 * @rules The core is interested in these rules
1125 	 *
1126 	 * Return 0 if yes,  -EOPNOTSUPP if not, or an error code.
1127 	 */
1128 	int (*led_hw_is_supported)(struct phy_device *dev, u8 index,
1129 				   unsigned long rules);
1130 	/**
1131 	 * @led_hw_control_set: Set the HW to control the LED
1132 	 * @dev: PHY device which has the LED
1133 	 * @index: Which LED of the PHY device
1134 	 * @rules The rules used to control the LED
1135 	 *
1136 	 * Returns 0, or a an error code.
1137 	 */
1138 	int (*led_hw_control_set)(struct phy_device *dev, u8 index,
1139 				  unsigned long rules);
1140 	/**
1141 	 * @led_hw_control_get: Get how the HW is controlling the LED
1142 	 * @dev: PHY device which has the LED
1143 	 * @index: Which LED of the PHY device
1144 	 * @rules Pointer to the rules used to control the LED
1145 	 *
1146 	 * Set *@rules to how the HW is currently blinking. Returns 0
1147 	 * on success, or a error code if the current blinking cannot
1148 	 * be represented in rules, or some other error happens.
1149 	 */
1150 	int (*led_hw_control_get)(struct phy_device *dev, u8 index,
1151 				  unsigned long *rules);
1152 
1153 };
1154 #define to_phy_driver(d) container_of(to_mdio_common_driver(d),		\
1155 				      struct phy_driver, mdiodrv)
1156 
1157 #define PHY_ANY_ID "MATCH ANY PHY"
1158 #define PHY_ANY_UID 0xffffffff
1159 
1160 #define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 0)
1161 #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
1162 #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
1163 
1164 /**
1165  * phy_id_compare - compare @id1 with @id2 taking account of @mask
1166  * @id1: first PHY ID
1167  * @id2: second PHY ID
1168  * @mask: the PHY ID mask, set bits are significant in matching
1169  *
1170  * Return true if the bits from @id1 and @id2 specified by @mask match.
1171  * This uses an equivalent test to (@id & @mask) == (@phy_id & @mask).
1172  */
1173 static inline bool phy_id_compare(u32 id1, u32 id2, u32 mask)
1174 {
1175 	return !((id1 ^ id2) & mask);
1176 }
1177 
1178 /**
1179  * phydev_id_compare - compare @id with the PHY's Clause 22 ID
1180  * @phydev: the PHY device
1181  * @id: the PHY ID to be matched
1182  *
1183  * Compare the @phydev clause 22 ID with the provided @id and return true or
1184  * false depending whether it matches, using the bound driver mask. The
1185  * @phydev must be bound to a driver.
1186  */
1187 static inline bool phydev_id_compare(struct phy_device *phydev, u32 id)
1188 {
1189 	return phy_id_compare(id, phydev->phy_id, phydev->drv->phy_id_mask);
1190 }
1191 
1192 /* A Structure for boards to register fixups with the PHY Lib */
1193 struct phy_fixup {
1194 	struct list_head list;
1195 	char bus_id[MII_BUS_ID_SIZE + 3];
1196 	u32 phy_uid;
1197 	u32 phy_uid_mask;
1198 	int (*run)(struct phy_device *phydev);
1199 };
1200 
1201 const char *phy_speed_to_str(int speed);
1202 const char *phy_duplex_to_str(unsigned int duplex);
1203 const char *phy_rate_matching_to_str(int rate_matching);
1204 
1205 int phy_interface_num_ports(phy_interface_t interface);
1206 
1207 /* A structure for mapping a particular speed and duplex
1208  * combination to a particular SUPPORTED and ADVERTISED value
1209  */
1210 struct phy_setting {
1211 	u32 speed;
1212 	u8 duplex;
1213 	u8 bit;
1214 };
1215 
1216 const struct phy_setting *
1217 phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
1218 		   bool exact);
1219 size_t phy_speeds(unsigned int *speeds, size_t size,
1220 		  unsigned long *mask);
1221 void of_set_phy_supported(struct phy_device *phydev);
1222 void of_set_phy_eee_broken(struct phy_device *phydev);
1223 int phy_speed_down_core(struct phy_device *phydev);
1224 
1225 /**
1226  * phy_is_started - Convenience function to check whether PHY is started
1227  * @phydev: The phy_device struct
1228  */
1229 static inline bool phy_is_started(struct phy_device *phydev)
1230 {
1231 	return phydev->state >= PHY_UP;
1232 }
1233 
1234 void phy_resolve_aneg_pause(struct phy_device *phydev);
1235 void phy_resolve_aneg_linkmode(struct phy_device *phydev);
1236 void phy_check_downshift(struct phy_device *phydev);
1237 
1238 /**
1239  * phy_read - Convenience function for reading a given PHY register
1240  * @phydev: the phy_device struct
1241  * @regnum: register number to read
1242  *
1243  * NOTE: MUST NOT be called from interrupt context,
1244  * because the bus read/write functions may wait for an interrupt
1245  * to conclude the operation.
1246  */
1247 static inline int phy_read(struct phy_device *phydev, u32 regnum)
1248 {
1249 	return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
1250 }
1251 
1252 #define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
1253 				timeout_us, sleep_before_read) \
1254 ({ \
1255 	int __ret, __val; \
1256 	__ret = read_poll_timeout(__val = phy_read, val, \
1257 				  __val < 0 || (cond), \
1258 		sleep_us, timeout_us, sleep_before_read, phydev, regnum); \
1259 	if (__val < 0) \
1260 		__ret = __val; \
1261 	if (__ret) \
1262 		phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
1263 	__ret; \
1264 })
1265 
1266 /**
1267  * __phy_read - convenience function for reading a given PHY register
1268  * @phydev: the phy_device struct
1269  * @regnum: register number to read
1270  *
1271  * The caller must have taken the MDIO bus lock.
1272  */
1273 static inline int __phy_read(struct phy_device *phydev, u32 regnum)
1274 {
1275 	return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
1276 }
1277 
1278 /**
1279  * phy_write - Convenience function for writing a given PHY register
1280  * @phydev: the phy_device struct
1281  * @regnum: register number to write
1282  * @val: value to write to @regnum
1283  *
1284  * NOTE: MUST NOT be called from interrupt context,
1285  * because the bus read/write functions may wait for an interrupt
1286  * to conclude the operation.
1287  */
1288 static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
1289 {
1290 	return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
1291 }
1292 
1293 /**
1294  * __phy_write - Convenience function for writing a given PHY register
1295  * @phydev: the phy_device struct
1296  * @regnum: register number to write
1297  * @val: value to write to @regnum
1298  *
1299  * The caller must have taken the MDIO bus lock.
1300  */
1301 static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
1302 {
1303 	return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum,
1304 			       val);
1305 }
1306 
1307 /**
1308  * __phy_modify_changed() - Convenience function for modifying a PHY register
1309  * @phydev: a pointer to a &struct phy_device
1310  * @regnum: register number
1311  * @mask: bit mask of bits to clear
1312  * @set: bit mask of bits to set
1313  *
1314  * Unlocked helper function which allows a PHY register to be modified as
1315  * new register value = (old register value & ~mask) | set
1316  *
1317  * Returns negative errno, 0 if there was no change, and 1 in case of change
1318  */
1319 static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
1320 				       u16 mask, u16 set)
1321 {
1322 	return __mdiobus_modify_changed(phydev->mdio.bus, phydev->mdio.addr,
1323 					regnum, mask, set);
1324 }
1325 
1326 /*
1327  * phy_read_mmd - Convenience function for reading a register
1328  * from an MMD on a given PHY.
1329  */
1330 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1331 
1332 /**
1333  * phy_read_mmd_poll_timeout - Periodically poll a PHY register until a
1334  *                             condition is met or a timeout occurs
1335  *
1336  * @phydev: The phy_device struct
1337  * @devaddr: The MMD to read from
1338  * @regnum: The register on the MMD to read
1339  * @val: Variable to read the register into
1340  * @cond: Break condition (usually involving @val)
1341  * @sleep_us: Maximum time to sleep between reads in us (0
1342  *            tight-loops).  Should be less than ~20ms since usleep_range
1343  *            is used (see Documentation/timers/timers-howto.rst).
1344  * @timeout_us: Timeout in us, 0 means never timeout
1345  * @sleep_before_read: if it is true, sleep @sleep_us before read.
1346  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
1347  * case, the last read value at @args is stored in @val. Must not
1348  * be called from atomic context if sleep_us or timeout_us are used.
1349  */
1350 #define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
1351 				  sleep_us, timeout_us, sleep_before_read) \
1352 ({ \
1353 	int __ret, __val; \
1354 	__ret = read_poll_timeout(__val = phy_read_mmd, val, \
1355 				  __val < 0 || (cond), \
1356 				  sleep_us, timeout_us, sleep_before_read, \
1357 				  phydev, devaddr, regnum); \
1358 	if (__val < 0) \
1359 		__ret = __val; \
1360 	if (__ret) \
1361 		phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
1362 	__ret; \
1363 })
1364 
1365 /*
1366  * __phy_read_mmd - Convenience function for reading a register
1367  * from an MMD on a given PHY.
1368  */
1369 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1370 
1371 /*
1372  * phy_write_mmd - Convenience function for writing a register
1373  * on an MMD on a given PHY.
1374  */
1375 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1376 
1377 /*
1378  * __phy_write_mmd - Convenience function for writing a register
1379  * on an MMD on a given PHY.
1380  */
1381 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1382 
1383 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1384 			 u16 set);
1385 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1386 		       u16 set);
1387 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1388 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1389 
1390 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1391 			     u16 mask, u16 set);
1392 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1393 			   u16 mask, u16 set);
1394 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1395 		     u16 mask, u16 set);
1396 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1397 		   u16 mask, u16 set);
1398 
1399 /**
1400  * __phy_set_bits - Convenience function for setting bits in a PHY register
1401  * @phydev: the phy_device struct
1402  * @regnum: register number to write
1403  * @val: bits to set
1404  *
1405  * The caller must have taken the MDIO bus lock.
1406  */
1407 static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1408 {
1409 	return __phy_modify(phydev, regnum, 0, val);
1410 }
1411 
1412 /**
1413  * __phy_clear_bits - Convenience function for clearing bits in a PHY register
1414  * @phydev: the phy_device struct
1415  * @regnum: register number to write
1416  * @val: bits to clear
1417  *
1418  * The caller must have taken the MDIO bus lock.
1419  */
1420 static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum,
1421 				   u16 val)
1422 {
1423 	return __phy_modify(phydev, regnum, val, 0);
1424 }
1425 
1426 /**
1427  * phy_set_bits - Convenience function for setting bits in a PHY register
1428  * @phydev: the phy_device struct
1429  * @regnum: register number to write
1430  * @val: bits to set
1431  */
1432 static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1433 {
1434 	return phy_modify(phydev, regnum, 0, val);
1435 }
1436 
1437 /**
1438  * phy_clear_bits - Convenience function for clearing bits in a PHY register
1439  * @phydev: the phy_device struct
1440  * @regnum: register number to write
1441  * @val: bits to clear
1442  */
1443 static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val)
1444 {
1445 	return phy_modify(phydev, regnum, val, 0);
1446 }
1447 
1448 /**
1449  * __phy_set_bits_mmd - Convenience function for setting bits in a register
1450  * on MMD
1451  * @phydev: the phy_device struct
1452  * @devad: the MMD containing register to modify
1453  * @regnum: register number to modify
1454  * @val: bits to set
1455  *
1456  * The caller must have taken the MDIO bus lock.
1457  */
1458 static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
1459 		u32 regnum, u16 val)
1460 {
1461 	return __phy_modify_mmd(phydev, devad, regnum, 0, val);
1462 }
1463 
1464 /**
1465  * __phy_clear_bits_mmd - Convenience function for clearing bits in a register
1466  * on MMD
1467  * @phydev: the phy_device struct
1468  * @devad: the MMD containing register to modify
1469  * @regnum: register number to modify
1470  * @val: bits to clear
1471  *
1472  * The caller must have taken the MDIO bus lock.
1473  */
1474 static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1475 		u32 regnum, u16 val)
1476 {
1477 	return __phy_modify_mmd(phydev, devad, regnum, val, 0);
1478 }
1479 
1480 /**
1481  * phy_set_bits_mmd - Convenience function for setting bits in a register
1482  * on MMD
1483  * @phydev: the phy_device struct
1484  * @devad: the MMD containing register to modify
1485  * @regnum: register number to modify
1486  * @val: bits to set
1487  */
1488 static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
1489 		u32 regnum, u16 val)
1490 {
1491 	return phy_modify_mmd(phydev, devad, regnum, 0, val);
1492 }
1493 
1494 /**
1495  * phy_clear_bits_mmd - Convenience function for clearing bits in a register
1496  * on MMD
1497  * @phydev: the phy_device struct
1498  * @devad: the MMD containing register to modify
1499  * @regnum: register number to modify
1500  * @val: bits to clear
1501  */
1502 static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1503 		u32 regnum, u16 val)
1504 {
1505 	return phy_modify_mmd(phydev, devad, regnum, val, 0);
1506 }
1507 
1508 /**
1509  * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
1510  * @phydev: the phy_device struct
1511  *
1512  * NOTE: must be kept in sync with addition/removal of PHY_POLL and
1513  * PHY_MAC_INTERRUPT
1514  */
1515 static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
1516 {
1517 	return phydev->irq != PHY_POLL && phydev->irq != PHY_MAC_INTERRUPT;
1518 }
1519 
1520 /**
1521  * phy_polling_mode - Convenience function for testing whether polling is
1522  * used to detect PHY status changes
1523  * @phydev: the phy_device struct
1524  */
1525 static inline bool phy_polling_mode(struct phy_device *phydev)
1526 {
1527 	if (phydev->state == PHY_CABLETEST)
1528 		if (phydev->drv->flags & PHY_POLL_CABLE_TEST)
1529 			return true;
1530 
1531 	return phydev->irq == PHY_POLL;
1532 }
1533 
1534 /**
1535  * phy_has_hwtstamp - Tests whether a PHY time stamp configuration.
1536  * @phydev: the phy_device struct
1537  */
1538 static inline bool phy_has_hwtstamp(struct phy_device *phydev)
1539 {
1540 	return phydev && phydev->mii_ts && phydev->mii_ts->hwtstamp;
1541 }
1542 
1543 /**
1544  * phy_has_rxtstamp - Tests whether a PHY supports receive time stamping.
1545  * @phydev: the phy_device struct
1546  */
1547 static inline bool phy_has_rxtstamp(struct phy_device *phydev)
1548 {
1549 	return phydev && phydev->mii_ts && phydev->mii_ts->rxtstamp;
1550 }
1551 
1552 /**
1553  * phy_has_tsinfo - Tests whether a PHY reports time stamping and/or
1554  * PTP hardware clock capabilities.
1555  * @phydev: the phy_device struct
1556  */
1557 static inline bool phy_has_tsinfo(struct phy_device *phydev)
1558 {
1559 	return phydev && phydev->mii_ts && phydev->mii_ts->ts_info;
1560 }
1561 
1562 /**
1563  * phy_has_txtstamp - Tests whether a PHY supports transmit time stamping.
1564  * @phydev: the phy_device struct
1565  */
1566 static inline bool phy_has_txtstamp(struct phy_device *phydev)
1567 {
1568 	return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp;
1569 }
1570 
1571 static inline int phy_hwtstamp(struct phy_device *phydev,
1572 			       struct kernel_hwtstamp_config *cfg,
1573 			       struct netlink_ext_ack *extack)
1574 {
1575 	return phydev->mii_ts->hwtstamp(phydev->mii_ts, cfg, extack);
1576 }
1577 
1578 static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb,
1579 				int type)
1580 {
1581 	return phydev->mii_ts->rxtstamp(phydev->mii_ts, skb, type);
1582 }
1583 
1584 static inline int phy_ts_info(struct phy_device *phydev,
1585 			      struct ethtool_ts_info *tsinfo)
1586 {
1587 	return phydev->mii_ts->ts_info(phydev->mii_ts, tsinfo);
1588 }
1589 
1590 static inline void phy_txtstamp(struct phy_device *phydev, struct sk_buff *skb,
1591 				int type)
1592 {
1593 	phydev->mii_ts->txtstamp(phydev->mii_ts, skb, type);
1594 }
1595 
1596 /**
1597  * phy_is_internal - Convenience function for testing if a PHY is internal
1598  * @phydev: the phy_device struct
1599  */
1600 static inline bool phy_is_internal(struct phy_device *phydev)
1601 {
1602 	return phydev->is_internal;
1603 }
1604 
1605 /**
1606  * phy_on_sfp - Convenience function for testing if a PHY is on an SFP module
1607  * @phydev: the phy_device struct
1608  */
1609 static inline bool phy_on_sfp(struct phy_device *phydev)
1610 {
1611 	return phydev->is_on_sfp_module;
1612 }
1613 
1614 /**
1615  * phy_interface_mode_is_rgmii - Convenience function for testing if a
1616  * PHY interface mode is RGMII (all variants)
1617  * @mode: the &phy_interface_t enum
1618  */
1619 static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
1620 {
1621 	return mode >= PHY_INTERFACE_MODE_RGMII &&
1622 		mode <= PHY_INTERFACE_MODE_RGMII_TXID;
1623 };
1624 
1625 /**
1626  * phy_interface_mode_is_8023z() - does the PHY interface mode use 802.3z
1627  *   negotiation
1628  * @mode: one of &enum phy_interface_t
1629  *
1630  * Returns true if the PHY interface mode uses the 16-bit negotiation
1631  * word as defined in 802.3z. (See 802.3-2015 37.2.1 Config_Reg encoding)
1632  */
1633 static inline bool phy_interface_mode_is_8023z(phy_interface_t mode)
1634 {
1635 	return mode == PHY_INTERFACE_MODE_1000BASEX ||
1636 	       mode == PHY_INTERFACE_MODE_2500BASEX;
1637 }
1638 
1639 /**
1640  * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
1641  * is RGMII (all variants)
1642  * @phydev: the phy_device struct
1643  */
1644 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
1645 {
1646 	return phy_interface_mode_is_rgmii(phydev->interface);
1647 };
1648 
1649 /**
1650  * phy_is_pseudo_fixed_link - Convenience function for testing if this
1651  * PHY is the CPU port facing side of an Ethernet switch, or similar.
1652  * @phydev: the phy_device struct
1653  */
1654 static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
1655 {
1656 	return phydev->is_pseudo_fixed_link;
1657 }
1658 
1659 int phy_save_page(struct phy_device *phydev);
1660 int phy_select_page(struct phy_device *phydev, int page);
1661 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
1662 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
1663 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
1664 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
1665 			     u16 mask, u16 set);
1666 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
1667 		     u16 mask, u16 set);
1668 
1669 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
1670 				     bool is_c45,
1671 				     struct phy_c45_device_ids *c45_ids);
1672 #if IS_ENABLED(CONFIG_PHYLIB)
1673 int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id);
1674 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode);
1675 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode);
1676 struct phy_device *device_phy_find_device(struct device *dev);
1677 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode);
1678 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
1679 int phy_device_register(struct phy_device *phy);
1680 void phy_device_free(struct phy_device *phydev);
1681 #else
1682 static inline int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id)
1683 {
1684 	return 0;
1685 }
1686 static inline
1687 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
1688 {
1689 	return 0;
1690 }
1691 
1692 static inline
1693 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
1694 {
1695 	return NULL;
1696 }
1697 
1698 static inline struct phy_device *device_phy_find_device(struct device *dev)
1699 {
1700 	return NULL;
1701 }
1702 
1703 static inline
1704 struct fwnode_handle *fwnode_get_phy_node(struct fwnode_handle *fwnode)
1705 {
1706 	return NULL;
1707 }
1708 
1709 static inline
1710 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
1711 {
1712 	return NULL;
1713 }
1714 
1715 static inline int phy_device_register(struct phy_device *phy)
1716 {
1717 	return 0;
1718 }
1719 
1720 static inline void phy_device_free(struct phy_device *phydev) { }
1721 #endif /* CONFIG_PHYLIB */
1722 void phy_device_remove(struct phy_device *phydev);
1723 int phy_get_c45_ids(struct phy_device *phydev);
1724 int phy_init_hw(struct phy_device *phydev);
1725 int phy_suspend(struct phy_device *phydev);
1726 int phy_resume(struct phy_device *phydev);
1727 int __phy_resume(struct phy_device *phydev);
1728 int phy_loopback(struct phy_device *phydev, bool enable);
1729 int phy_sfp_connect_phy(void *upstream, struct phy_device *phy);
1730 void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy);
1731 void phy_sfp_attach(void *upstream, struct sfp_bus *bus);
1732 void phy_sfp_detach(void *upstream, struct sfp_bus *bus);
1733 int phy_sfp_probe(struct phy_device *phydev,
1734 	          const struct sfp_upstream_ops *ops);
1735 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1736 			      phy_interface_t interface);
1737 struct phy_device *phy_find_first(struct mii_bus *bus);
1738 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1739 		      u32 flags, phy_interface_t interface);
1740 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1741 		       void (*handler)(struct net_device *),
1742 		       phy_interface_t interface);
1743 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1744 			       void (*handler)(struct net_device *),
1745 			       phy_interface_t interface);
1746 void phy_disconnect(struct phy_device *phydev);
1747 void phy_detach(struct phy_device *phydev);
1748 void phy_start(struct phy_device *phydev);
1749 void phy_stop(struct phy_device *phydev);
1750 int phy_config_aneg(struct phy_device *phydev);
1751 int _phy_start_aneg(struct phy_device *phydev);
1752 int phy_start_aneg(struct phy_device *phydev);
1753 int phy_aneg_done(struct phy_device *phydev);
1754 int phy_speed_down(struct phy_device *phydev, bool sync);
1755 int phy_speed_up(struct phy_device *phydev);
1756 bool phy_check_valid(int speed, int duplex, unsigned long *features);
1757 
1758 int phy_restart_aneg(struct phy_device *phydev);
1759 int phy_reset_after_clk_enable(struct phy_device *phydev);
1760 
1761 #if IS_ENABLED(CONFIG_PHYLIB)
1762 int phy_start_cable_test(struct phy_device *phydev,
1763 			 struct netlink_ext_ack *extack);
1764 int phy_start_cable_test_tdr(struct phy_device *phydev,
1765 			     struct netlink_ext_ack *extack,
1766 			     const struct phy_tdr_config *config);
1767 #else
1768 static inline
1769 int phy_start_cable_test(struct phy_device *phydev,
1770 			 struct netlink_ext_ack *extack)
1771 {
1772 	NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1773 	return -EOPNOTSUPP;
1774 }
1775 static inline
1776 int phy_start_cable_test_tdr(struct phy_device *phydev,
1777 			     struct netlink_ext_ack *extack,
1778 			     const struct phy_tdr_config *config)
1779 {
1780 	NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1781 	return -EOPNOTSUPP;
1782 }
1783 #endif
1784 
1785 static inline void phy_device_reset(struct phy_device *phydev, int value)
1786 {
1787 	mdio_device_reset(&phydev->mdio, value);
1788 }
1789 
1790 #define phydev_err(_phydev, format, args...)	\
1791 	dev_err(&_phydev->mdio.dev, format, ##args)
1792 
1793 #define phydev_err_probe(_phydev, err, format, args...)	\
1794 	dev_err_probe(&_phydev->mdio.dev, err, format, ##args)
1795 
1796 #define phydev_info(_phydev, format, args...)	\
1797 	dev_info(&_phydev->mdio.dev, format, ##args)
1798 
1799 #define phydev_warn(_phydev, format, args...)	\
1800 	dev_warn(&_phydev->mdio.dev, format, ##args)
1801 
1802 #define phydev_dbg(_phydev, format, args...)	\
1803 	dev_dbg(&_phydev->mdio.dev, format, ##args)
1804 
1805 static inline const char *phydev_name(const struct phy_device *phydev)
1806 {
1807 	return dev_name(&phydev->mdio.dev);
1808 }
1809 
1810 static inline void phy_lock_mdio_bus(struct phy_device *phydev)
1811 {
1812 	mutex_lock(&phydev->mdio.bus->mdio_lock);
1813 }
1814 
1815 static inline void phy_unlock_mdio_bus(struct phy_device *phydev)
1816 {
1817 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
1818 }
1819 
1820 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1821 	__printf(2, 3);
1822 char *phy_attached_info_irq(struct phy_device *phydev)
1823 	__malloc;
1824 void phy_attached_info(struct phy_device *phydev);
1825 
1826 /* Clause 22 PHY */
1827 int genphy_read_abilities(struct phy_device *phydev);
1828 int genphy_setup_forced(struct phy_device *phydev);
1829 int genphy_restart_aneg(struct phy_device *phydev);
1830 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1831 int genphy_config_eee_advert(struct phy_device *phydev);
1832 int __genphy_config_aneg(struct phy_device *phydev, bool changed);
1833 int genphy_aneg_done(struct phy_device *phydev);
1834 int genphy_update_link(struct phy_device *phydev);
1835 int genphy_read_lpa(struct phy_device *phydev);
1836 int genphy_read_status_fixed(struct phy_device *phydev);
1837 int genphy_read_status(struct phy_device *phydev);
1838 int genphy_read_master_slave(struct phy_device *phydev);
1839 int genphy_suspend(struct phy_device *phydev);
1840 int genphy_resume(struct phy_device *phydev);
1841 int genphy_loopback(struct phy_device *phydev, bool enable);
1842 int genphy_soft_reset(struct phy_device *phydev);
1843 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev);
1844 
1845 static inline int genphy_config_aneg(struct phy_device *phydev)
1846 {
1847 	return __genphy_config_aneg(phydev, false);
1848 }
1849 
1850 static inline int genphy_no_config_intr(struct phy_device *phydev)
1851 {
1852 	return 0;
1853 }
1854 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
1855 				u16 regnum);
1856 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1857 				 u16 regnum, u16 val);
1858 
1859 /* Clause 37 */
1860 int genphy_c37_config_aneg(struct phy_device *phydev);
1861 int genphy_c37_read_status(struct phy_device *phydev);
1862 
1863 /* Clause 45 PHY */
1864 int genphy_c45_restart_aneg(struct phy_device *phydev);
1865 int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1866 int genphy_c45_aneg_done(struct phy_device *phydev);
1867 int genphy_c45_read_link(struct phy_device *phydev);
1868 int genphy_c45_read_lpa(struct phy_device *phydev);
1869 int genphy_c45_read_pma(struct phy_device *phydev);
1870 int genphy_c45_pma_setup_forced(struct phy_device *phydev);
1871 int genphy_c45_pma_baset1_setup_master_slave(struct phy_device *phydev);
1872 int genphy_c45_an_config_aneg(struct phy_device *phydev);
1873 int genphy_c45_an_disable_aneg(struct phy_device *phydev);
1874 int genphy_c45_read_mdix(struct phy_device *phydev);
1875 int genphy_c45_pma_read_abilities(struct phy_device *phydev);
1876 int genphy_c45_pma_read_ext_abilities(struct phy_device *phydev);
1877 int genphy_c45_pma_baset1_read_abilities(struct phy_device *phydev);
1878 int genphy_c45_read_eee_abilities(struct phy_device *phydev);
1879 int genphy_c45_pma_baset1_read_master_slave(struct phy_device *phydev);
1880 int genphy_c45_read_status(struct phy_device *phydev);
1881 int genphy_c45_baset1_read_status(struct phy_device *phydev);
1882 int genphy_c45_config_aneg(struct phy_device *phydev);
1883 int genphy_c45_loopback(struct phy_device *phydev, bool enable);
1884 int genphy_c45_pma_resume(struct phy_device *phydev);
1885 int genphy_c45_pma_suspend(struct phy_device *phydev);
1886 int genphy_c45_fast_retrain(struct phy_device *phydev, bool enable);
1887 int genphy_c45_plca_get_cfg(struct phy_device *phydev,
1888 			    struct phy_plca_cfg *plca_cfg);
1889 int genphy_c45_plca_set_cfg(struct phy_device *phydev,
1890 			    const struct phy_plca_cfg *plca_cfg);
1891 int genphy_c45_plca_get_status(struct phy_device *phydev,
1892 			       struct phy_plca_status *plca_st);
1893 int genphy_c45_eee_is_active(struct phy_device *phydev, unsigned long *adv,
1894 			     unsigned long *lp, bool *is_enabled);
1895 int genphy_c45_ethtool_get_eee(struct phy_device *phydev,
1896 			       struct ethtool_eee *data);
1897 int genphy_c45_ethtool_set_eee(struct phy_device *phydev,
1898 			       struct ethtool_eee *data);
1899 int genphy_c45_write_eee_adv(struct phy_device *phydev, unsigned long *adv);
1900 int genphy_c45_an_config_eee_aneg(struct phy_device *phydev);
1901 int genphy_c45_read_eee_adv(struct phy_device *phydev, unsigned long *adv);
1902 
1903 /* Generic C45 PHY driver */
1904 extern struct phy_driver genphy_c45_driver;
1905 
1906 /* The gen10g_* functions are the old Clause 45 stub */
1907 int gen10g_config_aneg(struct phy_device *phydev);
1908 
1909 static inline int phy_read_status(struct phy_device *phydev)
1910 {
1911 	if (!phydev->drv)
1912 		return -EIO;
1913 
1914 	if (phydev->drv->read_status)
1915 		return phydev->drv->read_status(phydev);
1916 	else
1917 		return genphy_read_status(phydev);
1918 }
1919 
1920 void phy_driver_unregister(struct phy_driver *drv);
1921 void phy_drivers_unregister(struct phy_driver *drv, int n);
1922 int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
1923 int phy_drivers_register(struct phy_driver *new_driver, int n,
1924 			 struct module *owner);
1925 void phy_error(struct phy_device *phydev);
1926 void phy_state_machine(struct work_struct *work);
1927 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
1928 void phy_trigger_machine(struct phy_device *phydev);
1929 void phy_mac_interrupt(struct phy_device *phydev);
1930 void phy_start_machine(struct phy_device *phydev);
1931 void phy_stop_machine(struct phy_device *phydev);
1932 void phy_ethtool_ksettings_get(struct phy_device *phydev,
1933 			       struct ethtool_link_ksettings *cmd);
1934 int phy_ethtool_ksettings_set(struct phy_device *phydev,
1935 			      const struct ethtool_link_ksettings *cmd);
1936 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
1937 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
1938 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd);
1939 int phy_disable_interrupts(struct phy_device *phydev);
1940 void phy_request_interrupt(struct phy_device *phydev);
1941 void phy_free_interrupt(struct phy_device *phydev);
1942 void phy_print_status(struct phy_device *phydev);
1943 int phy_get_rate_matching(struct phy_device *phydev,
1944 			    phy_interface_t iface);
1945 void phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
1946 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
1947 void phy_advertise_supported(struct phy_device *phydev);
1948 void phy_support_sym_pause(struct phy_device *phydev);
1949 void phy_support_asym_pause(struct phy_device *phydev);
1950 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
1951 		       bool autoneg);
1952 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
1953 bool phy_validate_pause(struct phy_device *phydev,
1954 			struct ethtool_pauseparam *pp);
1955 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause);
1956 
1957 s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
1958 			   const int *delay_values, int size, bool is_rx);
1959 
1960 void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv,
1961 		       bool *tx_pause, bool *rx_pause);
1962 
1963 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
1964 		       int (*run)(struct phy_device *));
1965 int phy_register_fixup_for_id(const char *bus_id,
1966 			      int (*run)(struct phy_device *));
1967 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
1968 			       int (*run)(struct phy_device *));
1969 
1970 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
1971 int phy_unregister_fixup_for_id(const char *bus_id);
1972 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
1973 
1974 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
1975 int phy_get_eee_err(struct phy_device *phydev);
1976 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
1977 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
1978 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
1979 void phy_ethtool_get_wol(struct phy_device *phydev,
1980 			 struct ethtool_wolinfo *wol);
1981 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1982 				   struct ethtool_link_ksettings *cmd);
1983 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1984 				   const struct ethtool_link_ksettings *cmd);
1985 int phy_ethtool_nway_reset(struct net_device *ndev);
1986 int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size);
1987 void phy_package_leave(struct phy_device *phydev);
1988 int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
1989 			  int base_addr, size_t priv_size);
1990 
1991 int __init mdio_bus_init(void);
1992 void mdio_bus_exit(void);
1993 
1994 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data);
1995 int phy_ethtool_get_sset_count(struct phy_device *phydev);
1996 int phy_ethtool_get_stats(struct phy_device *phydev,
1997 			  struct ethtool_stats *stats, u64 *data);
1998 int phy_ethtool_get_plca_cfg(struct phy_device *phydev,
1999 			     struct phy_plca_cfg *plca_cfg);
2000 int phy_ethtool_set_plca_cfg(struct phy_device *phydev,
2001 			     const struct phy_plca_cfg *plca_cfg,
2002 			     struct netlink_ext_ack *extack);
2003 int phy_ethtool_get_plca_status(struct phy_device *phydev,
2004 				struct phy_plca_status *plca_st);
2005 
2006 int __phy_hwtstamp_get(struct phy_device *phydev,
2007 		       struct kernel_hwtstamp_config *config);
2008 int __phy_hwtstamp_set(struct phy_device *phydev,
2009 		       struct kernel_hwtstamp_config *config,
2010 		       struct netlink_ext_ack *extack);
2011 
2012 static inline int phy_package_address(struct phy_device *phydev,
2013 				      unsigned int addr_offset)
2014 {
2015 	struct phy_package_shared *shared = phydev->shared;
2016 	u8 base_addr = shared->base_addr;
2017 
2018 	if (addr_offset >= PHY_MAX_ADDR - base_addr)
2019 		return -EIO;
2020 
2021 	/* we know that addr will be in the range 0..31 and thus the
2022 	 * implicit cast to a signed int is not a problem.
2023 	 */
2024 	return base_addr + addr_offset;
2025 }
2026 
2027 static inline int phy_package_read(struct phy_device *phydev,
2028 				   unsigned int addr_offset, u32 regnum)
2029 {
2030 	int addr = phy_package_address(phydev, addr_offset);
2031 
2032 	if (addr < 0)
2033 		return addr;
2034 
2035 	return mdiobus_read(phydev->mdio.bus, addr, regnum);
2036 }
2037 
2038 static inline int __phy_package_read(struct phy_device *phydev,
2039 				     unsigned int addr_offset, u32 regnum)
2040 {
2041 	int addr = phy_package_address(phydev, addr_offset);
2042 
2043 	if (addr < 0)
2044 		return addr;
2045 
2046 	return __mdiobus_read(phydev->mdio.bus, addr, regnum);
2047 }
2048 
2049 static inline int phy_package_write(struct phy_device *phydev,
2050 				    unsigned int addr_offset, u32 regnum,
2051 				    u16 val)
2052 {
2053 	int addr = phy_package_address(phydev, addr_offset);
2054 
2055 	if (addr < 0)
2056 		return addr;
2057 
2058 	return mdiobus_write(phydev->mdio.bus, addr, regnum, val);
2059 }
2060 
2061 static inline int __phy_package_write(struct phy_device *phydev,
2062 				      unsigned int addr_offset, u32 regnum,
2063 				      u16 val)
2064 {
2065 	int addr = phy_package_address(phydev, addr_offset);
2066 
2067 	if (addr < 0)
2068 		return addr;
2069 
2070 	return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
2071 }
2072 
2073 int __phy_package_read_mmd(struct phy_device *phydev,
2074 			   unsigned int addr_offset, int devad,
2075 			   u32 regnum);
2076 
2077 int phy_package_read_mmd(struct phy_device *phydev,
2078 			 unsigned int addr_offset, int devad,
2079 			 u32 regnum);
2080 
2081 int __phy_package_write_mmd(struct phy_device *phydev,
2082 			    unsigned int addr_offset, int devad,
2083 			    u32 regnum, u16 val);
2084 
2085 int phy_package_write_mmd(struct phy_device *phydev,
2086 			  unsigned int addr_offset, int devad,
2087 			  u32 regnum, u16 val);
2088 
2089 static inline bool __phy_package_set_once(struct phy_device *phydev,
2090 					  unsigned int b)
2091 {
2092 	struct phy_package_shared *shared = phydev->shared;
2093 
2094 	if (!shared)
2095 		return false;
2096 
2097 	return !test_and_set_bit(b, &shared->flags);
2098 }
2099 
2100 static inline bool phy_package_init_once(struct phy_device *phydev)
2101 {
2102 	return __phy_package_set_once(phydev, PHY_SHARED_F_INIT_DONE);
2103 }
2104 
2105 static inline bool phy_package_probe_once(struct phy_device *phydev)
2106 {
2107 	return __phy_package_set_once(phydev, PHY_SHARED_F_PROBE_DONE);
2108 }
2109 
2110 extern struct bus_type mdio_bus_type;
2111 
2112 struct mdio_board_info {
2113 	const char	*bus_id;
2114 	char		modalias[MDIO_NAME_SIZE];
2115 	int		mdio_addr;
2116 	const void	*platform_data;
2117 };
2118 
2119 #if IS_ENABLED(CONFIG_MDIO_DEVICE)
2120 int mdiobus_register_board_info(const struct mdio_board_info *info,
2121 				unsigned int n);
2122 #else
2123 static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
2124 					      unsigned int n)
2125 {
2126 	return 0;
2127 }
2128 #endif
2129 
2130 
2131 /**
2132  * phy_module_driver() - Helper macro for registering PHY drivers
2133  * @__phy_drivers: array of PHY drivers to register
2134  * @__count: Numbers of members in array
2135  *
2136  * Helper macro for PHY drivers which do not do anything special in module
2137  * init/exit. Each module may only use this macro once, and calling it
2138  * replaces module_init() and module_exit().
2139  */
2140 #define phy_module_driver(__phy_drivers, __count)			\
2141 static int __init phy_module_init(void)					\
2142 {									\
2143 	return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
2144 }									\
2145 module_init(phy_module_init);						\
2146 static void __exit phy_module_exit(void)				\
2147 {									\
2148 	phy_drivers_unregister(__phy_drivers, __count);			\
2149 }									\
2150 module_exit(phy_module_exit)
2151 
2152 #define module_phy_driver(__phy_drivers)				\
2153 	phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
2154 
2155 bool phy_driver_is_genphy(struct phy_device *phydev);
2156 bool phy_driver_is_genphy_10g(struct phy_device *phydev);
2157 
2158 #endif /* __PHY_H */
2159