xref: /linux/include/linux/phy.h (revision 9e7cd33fafb2ce1fd6cf19fd20f486ea341e6c0c)
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/device-id/mdio.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 #include <net/eee.h>
34 
35 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
36 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
37 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1s_p2mp_features) __ro_after_init;
38 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
39 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
40 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
41 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap1_features) __ro_after_init;
42 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap2_features) __ro_after_init;
43 
44 #define PHY_BASIC_FEATURES ((unsigned long *)&phy_basic_features)
45 #define PHY_BASIC_T1_FEATURES ((unsigned long *)&phy_basic_t1_features)
46 #define PHY_BASIC_T1S_P2MP_FEATURES ((unsigned long *)&phy_basic_t1s_p2mp_features)
47 #define PHY_GBIT_FEATURES ((unsigned long *)&phy_gbit_features)
48 #define PHY_GBIT_FIBRE_FEATURES ((unsigned long *)&phy_gbit_fibre_features)
49 #define PHY_10GBIT_FEATURES ((unsigned long *)&phy_10gbit_features)
50 #define PHY_EEE_CAP1_FEATURES ((unsigned long *)&phy_eee_cap1_features)
51 #define PHY_EEE_CAP2_FEATURES ((unsigned long *)&phy_eee_cap2_features)
52 
53 extern const int phy_basic_ports_array[3];
54 
55 /*
56  * Set phydev->irq to PHY_POLL if interrupts are not supported,
57  * or not desired for this PHY.  Set to PHY_MAC_INTERRUPT if
58  * the attached MAC driver handles the interrupt
59  */
60 #define PHY_POLL		-1
61 #define PHY_MAC_INTERRUPT	-2
62 
63 #define PHY_IS_INTERNAL		0x00000001
64 #define PHY_RST_AFTER_CLK_EN	0x00000002
65 #define PHY_POLL_CABLE_TEST	0x00000004
66 #define PHY_ALWAYS_CALL_SUSPEND	0x00000008
67 #define MDIO_DEVICE_IS_PHY	0x80000000
68 
69 /**
70  * enum phy_interface_t - Interface Mode definitions
71  *
72  * @PHY_INTERFACE_MODE_NA: Not Applicable - don't touch
73  * @PHY_INTERFACE_MODE_INTERNAL: No interface, MAC and PHY combined
74  * @PHY_INTERFACE_MODE_MII: Media-independent interface
75  * @PHY_INTERFACE_MODE_GMII: Gigabit media-independent interface
76  * @PHY_INTERFACE_MODE_SGMII: Serial gigabit media-independent interface
77  * @PHY_INTERFACE_MODE_TBI: Ten Bit Interface
78  * @PHY_INTERFACE_MODE_REVMII: Reverse Media Independent Interface
79  * @PHY_INTERFACE_MODE_RMII: Reduced Media Independent Interface
80  * @PHY_INTERFACE_MODE_REVRMII: Reduced Media Independent Interface in PHY role
81  * @PHY_INTERFACE_MODE_RGMII: Reduced gigabit media-independent interface
82  * @PHY_INTERFACE_MODE_RGMII_ID: RGMII with Internal RX+TX delay
83  * @PHY_INTERFACE_MODE_RGMII_RXID: RGMII with Internal RX delay
84  * @PHY_INTERFACE_MODE_RGMII_TXID: RGMII with Internal TX delay
85  * @PHY_INTERFACE_MODE_RTBI: Reduced TBI
86  * @PHY_INTERFACE_MODE_SMII: Serial MII
87  * @PHY_INTERFACE_MODE_XGMII: 10 gigabit media-independent interface
88  * @PHY_INTERFACE_MODE_XLGMII:40 gigabit media-independent interface
89  * @PHY_INTERFACE_MODE_MOCA: Multimedia over Coax
90  * @PHY_INTERFACE_MODE_PSGMII: Penta SGMII
91  * @PHY_INTERFACE_MODE_QSGMII: Quad SGMII
92  * @PHY_INTERFACE_MODE_TRGMII: Turbo RGMII
93  * @PHY_INTERFACE_MODE_100BASEX: 100 BaseX
94  * @PHY_INTERFACE_MODE_1000BASEX: 1000 BaseX
95  * @PHY_INTERFACE_MODE_2500BASEX: 2500 BaseX
96  * @PHY_INTERFACE_MODE_5GBASER: 5G BaseR
97  * @PHY_INTERFACE_MODE_RXAUI: Reduced XAUI
98  * @PHY_INTERFACE_MODE_XAUI: 10 Gigabit Attachment Unit Interface
99  * @PHY_INTERFACE_MODE_10GBASER: 10G BaseR
100  * @PHY_INTERFACE_MODE_25GBASER: 25G BaseR
101  * @PHY_INTERFACE_MODE_USXGMII:  Universal Serial 10GE MII
102  * @PHY_INTERFACE_MODE_10GKR: 10GBASE-KR - with Clause 73 AN
103  * @PHY_INTERFACE_MODE_QUSGMII: Quad Universal SGMII
104  * @PHY_INTERFACE_MODE_1000BASEKX: 1000Base-KX - with Clause 73 AN
105  * @PHY_INTERFACE_MODE_10G_QXGMII: 10G-QXGMII - 4 ports over 10G USXGMII
106  * @PHY_INTERFACE_MODE_50GBASER: 50GBase-R - with Clause 134 FEC
107  * @PHY_INTERFACE_MODE_LAUI: 50 Gigabit Attachment Unit Interface
108  * @PHY_INTERFACE_MODE_100GBASEP: 100GBase-P - with Clause 134 FEC
109  * @PHY_INTERFACE_MODE_MIILITE: MII-Lite - MII without RXER TXER CRS COL
110  * @PHY_INTERFACE_MODE_MAX: Book keeping
111  *
112  * Describes the interface between the MAC and PHY.
113  */
114 typedef enum {
115 	PHY_INTERFACE_MODE_NA,
116 	PHY_INTERFACE_MODE_INTERNAL,
117 	PHY_INTERFACE_MODE_MII,
118 	PHY_INTERFACE_MODE_GMII,
119 	PHY_INTERFACE_MODE_SGMII,
120 	PHY_INTERFACE_MODE_TBI,
121 	PHY_INTERFACE_MODE_REVMII,
122 	PHY_INTERFACE_MODE_RMII,
123 	PHY_INTERFACE_MODE_REVRMII,
124 	PHY_INTERFACE_MODE_RGMII,
125 	PHY_INTERFACE_MODE_RGMII_ID,
126 	PHY_INTERFACE_MODE_RGMII_RXID,
127 	PHY_INTERFACE_MODE_RGMII_TXID,
128 	PHY_INTERFACE_MODE_RTBI,
129 	PHY_INTERFACE_MODE_SMII,
130 	PHY_INTERFACE_MODE_XGMII,
131 	PHY_INTERFACE_MODE_XLGMII,
132 	PHY_INTERFACE_MODE_MOCA,
133 	PHY_INTERFACE_MODE_PSGMII,
134 	PHY_INTERFACE_MODE_QSGMII,
135 	PHY_INTERFACE_MODE_TRGMII,
136 	PHY_INTERFACE_MODE_100BASEX,
137 	PHY_INTERFACE_MODE_1000BASEX,
138 	PHY_INTERFACE_MODE_2500BASEX,
139 	PHY_INTERFACE_MODE_5GBASER,
140 	PHY_INTERFACE_MODE_RXAUI,
141 	PHY_INTERFACE_MODE_XAUI,
142 	/* 10GBASE-R, XFI, SFI - single lane 10G Serdes */
143 	PHY_INTERFACE_MODE_10GBASER,
144 	PHY_INTERFACE_MODE_25GBASER,
145 	PHY_INTERFACE_MODE_USXGMII,
146 	/* 10GBASE-KR - with Clause 73 AN */
147 	PHY_INTERFACE_MODE_10GKR,
148 	PHY_INTERFACE_MODE_QUSGMII,
149 	PHY_INTERFACE_MODE_1000BASEKX,
150 	PHY_INTERFACE_MODE_10G_QXGMII,
151 	PHY_INTERFACE_MODE_50GBASER,
152 	PHY_INTERFACE_MODE_LAUI,
153 	PHY_INTERFACE_MODE_100GBASEP,
154 	PHY_INTERFACE_MODE_MIILITE,
155 	PHY_INTERFACE_MODE_MAX,
156 } phy_interface_t;
157 
158 /* PHY interface mode bitmap handling */
159 #define DECLARE_PHY_INTERFACE_MASK(name) \
160 	DECLARE_BITMAP(name, PHY_INTERFACE_MODE_MAX)
161 
162 static inline void phy_interface_zero(unsigned long *intf)
163 {
164 	bitmap_zero(intf, PHY_INTERFACE_MODE_MAX);
165 }
166 
167 static inline bool phy_interface_empty(const unsigned long *intf)
168 {
169 	return bitmap_empty(intf, PHY_INTERFACE_MODE_MAX);
170 }
171 
172 static inline void phy_interface_copy(unsigned long *d, const unsigned long *s)
173 {
174 	bitmap_copy(d, s, PHY_INTERFACE_MODE_MAX);
175 }
176 
177 static inline unsigned int phy_interface_weight(const unsigned long *intf)
178 {
179 	return bitmap_weight(intf, PHY_INTERFACE_MODE_MAX);
180 }
181 
182 static inline void phy_interface_and(unsigned long *dst, const unsigned long *a,
183 				     const unsigned long *b)
184 {
185 	bitmap_and(dst, a, b, PHY_INTERFACE_MODE_MAX);
186 }
187 
188 static inline void phy_interface_or(unsigned long *dst, const unsigned long *a,
189 				    const unsigned long *b)
190 {
191 	bitmap_or(dst, a, b, PHY_INTERFACE_MODE_MAX);
192 }
193 
194 static inline void phy_interface_set_rgmii(unsigned long *intf)
195 {
196 	__set_bit(PHY_INTERFACE_MODE_RGMII, intf);
197 	__set_bit(PHY_INTERFACE_MODE_RGMII_ID, intf);
198 	__set_bit(PHY_INTERFACE_MODE_RGMII_RXID, intf);
199 	__set_bit(PHY_INTERFACE_MODE_RGMII_TXID, intf);
200 }
201 
202 /**
203  * phy_modes - map phy_interface_t enum to device tree binding of phy-mode
204  * @interface: enum phy_interface_t value
205  *
206  * Description: maps enum &phy_interface_t defined in this file
207  * into the device tree binding of 'phy-mode', so that Ethernet
208  * device driver can get PHY interface from device tree.
209  */
210 static inline const char *phy_modes(phy_interface_t interface)
211 {
212 	switch (interface) {
213 	case PHY_INTERFACE_MODE_NA:
214 		return "";
215 	case PHY_INTERFACE_MODE_INTERNAL:
216 		return "internal";
217 	case PHY_INTERFACE_MODE_MII:
218 		return "mii";
219 	case PHY_INTERFACE_MODE_GMII:
220 		return "gmii";
221 	case PHY_INTERFACE_MODE_SGMII:
222 		return "sgmii";
223 	case PHY_INTERFACE_MODE_TBI:
224 		return "tbi";
225 	case PHY_INTERFACE_MODE_REVMII:
226 		return "rev-mii";
227 	case PHY_INTERFACE_MODE_RMII:
228 		return "rmii";
229 	case PHY_INTERFACE_MODE_REVRMII:
230 		return "rev-rmii";
231 	case PHY_INTERFACE_MODE_RGMII:
232 		return "rgmii";
233 	case PHY_INTERFACE_MODE_RGMII_ID:
234 		return "rgmii-id";
235 	case PHY_INTERFACE_MODE_RGMII_RXID:
236 		return "rgmii-rxid";
237 	case PHY_INTERFACE_MODE_RGMII_TXID:
238 		return "rgmii-txid";
239 	case PHY_INTERFACE_MODE_RTBI:
240 		return "rtbi";
241 	case PHY_INTERFACE_MODE_SMII:
242 		return "smii";
243 	case PHY_INTERFACE_MODE_XGMII:
244 		return "xgmii";
245 	case PHY_INTERFACE_MODE_XLGMII:
246 		return "xlgmii";
247 	case PHY_INTERFACE_MODE_MOCA:
248 		return "moca";
249 	case PHY_INTERFACE_MODE_PSGMII:
250 		return "psgmii";
251 	case PHY_INTERFACE_MODE_QSGMII:
252 		return "qsgmii";
253 	case PHY_INTERFACE_MODE_TRGMII:
254 		return "trgmii";
255 	case PHY_INTERFACE_MODE_1000BASEX:
256 		return "1000base-x";
257 	case PHY_INTERFACE_MODE_1000BASEKX:
258 		return "1000base-kx";
259 	case PHY_INTERFACE_MODE_2500BASEX:
260 		return "2500base-x";
261 	case PHY_INTERFACE_MODE_5GBASER:
262 		return "5gbase-r";
263 	case PHY_INTERFACE_MODE_RXAUI:
264 		return "rxaui";
265 	case PHY_INTERFACE_MODE_XAUI:
266 		return "xaui";
267 	case PHY_INTERFACE_MODE_10GBASER:
268 		return "10gbase-r";
269 	case PHY_INTERFACE_MODE_25GBASER:
270 		return "25gbase-r";
271 	case PHY_INTERFACE_MODE_USXGMII:
272 		return "usxgmii";
273 	case PHY_INTERFACE_MODE_10GKR:
274 		return "10gbase-kr";
275 	case PHY_INTERFACE_MODE_100BASEX:
276 		return "100base-x";
277 	case PHY_INTERFACE_MODE_QUSGMII:
278 		return "qusgmii";
279 	case PHY_INTERFACE_MODE_10G_QXGMII:
280 		return "10g-qxgmii";
281 	case PHY_INTERFACE_MODE_50GBASER:
282 		return "50gbase-r";
283 	case PHY_INTERFACE_MODE_LAUI:
284 		return "laui";
285 	case PHY_INTERFACE_MODE_100GBASEP:
286 		return "100gbase-p";
287 	case PHY_INTERFACE_MODE_MIILITE:
288 		return "mii-lite";
289 	default:
290 		return "unknown";
291 	}
292 }
293 
294 /**
295  * rgmii_clock - map link speed to the clock rate
296  * @speed: link speed value
297  *
298  * Description: maps RGMII supported link speeds into the clock rates.
299  * This can also be used for MII, GMII, and RMII interface modes as the
300  * clock rates are identical, but the caller must be aware that errors
301  * for unsupported clock rates will not be signalled.
302  *
303  * Returns: clock rate or negative errno
304  */
305 static inline long rgmii_clock(int speed)
306 {
307 	switch (speed) {
308 	case SPEED_10:
309 		return 2500000;
310 	case SPEED_100:
311 		return 25000000;
312 	case SPEED_1000:
313 		return 125000000;
314 	default:
315 		return -EINVAL;
316 	}
317 }
318 
319 #define PHY_MAX_ADDR	32
320 
321 /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
322 #define PHY_ID_FMT "%s:%02x"
323 #define PHY_ID_SIZE	(MII_BUS_ID_SIZE + 3)
324 
325 #define MII_BUS_ID_SIZE	61
326 
327 struct device;
328 struct kernel_hwtstamp_config;
329 struct phylink;
330 struct phy_port;
331 struct sfp_bus;
332 struct sfp_upstream_ops;
333 struct sk_buff;
334 
335 /**
336  * struct mdio_bus_stats - Statistics counters for MDIO busses
337  * @transfers: Total number of transfers, i.e. @writes + @reads
338  * @errors: Number of MDIO transfers that returned an error
339  * @writes: Number of write transfers
340  * @reads: Number of read transfers
341  * @syncp: Synchronisation for incrementing statistics
342  */
343 struct mdio_bus_stats {
344 	u64_stats_t transfers;
345 	u64_stats_t errors;
346 	u64_stats_t writes;
347 	u64_stats_t reads;
348 	/* Must be last, add new statistics above */
349 	struct u64_stats_sync syncp;
350 };
351 
352 /**
353  * struct mii_bus - Represents an MDIO bus
354  *
355  * @owner: Who owns this device
356  * @name: User friendly name for this MDIO device, or driver name
357  * @id: Unique identifier for this bus, typical from bus hierarchy
358  * @priv: Driver private data
359  *
360  * The Bus class for PHYs.  Devices which provide access to
361  * PHYs should register using this structure
362  */
363 struct mii_bus {
364 	struct module *owner;
365 	const char *name;
366 	char id[MII_BUS_ID_SIZE];
367 	void *priv;
368 	/** @read: Perform a read transfer on the bus */
369 	int (*read)(struct mii_bus *bus, int addr, int regnum);
370 	/** @write: Perform a write transfer on the bus */
371 	int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val);
372 	/** @read_c45: Perform a C45 read transfer on the bus */
373 	int (*read_c45)(struct mii_bus *bus, int addr, int devnum, int regnum);
374 	/** @write_c45: Perform a C45 write transfer on the bus */
375 	int (*write_c45)(struct mii_bus *bus, int addr, int devnum,
376 			 int regnum, u16 val);
377 	/** @reset: Perform a reset of the bus */
378 	int (*reset)(struct mii_bus *bus);
379 	/**
380 	 * @notify_phy_attach: Perform post-attach handling for MDIO bus
381 	 * drivers. Optional and independent of @notify_phy_detach. Called
382 	 * in phy_attach_direct() right before phy_resume(). Runs in process
383 	 * context, may sleep and may be called with RTNL held. Must not
384 	 * acquire or rely on RTNL. Returns 0 on success or negative errno
385 	 * on failure. Must unwind its own state on error as attachment is
386 	 * aborted.
387 	 */
388 	int (*notify_phy_attach)(struct phy_device *phydev);
389 	/**
390 	 * @notify_phy_detach: Perform pre-detach handling for MDIO bus
391 	 * drivers. Optional and independent of @notify_phy_attach. Called
392 	 * in phy_detach() right after phy_suspend(). Runs in process context,
393 	 * may sleep and may be called with RTNL held. Must not acquire or
394 	 * rely on RTNL.
395 	 */
396 	void (*notify_phy_detach)(struct phy_device *phydev);
397 
398 	/** @stats: Statistic counters per device on the bus */
399 	struct mdio_bus_stats stats[PHY_MAX_ADDR];
400 
401 	/**
402 	 * @mdio_lock: A lock to ensure that only one thing can read/write
403 	 * the MDIO bus at a time
404 	 */
405 	struct mutex mdio_lock;
406 
407 	/** @parent: Parent device of this bus */
408 	struct device *parent;
409 	/** @state: State of bus structure */
410 	enum {
411 		MDIOBUS_ALLOCATED = 1,
412 		MDIOBUS_REGISTERED,
413 		MDIOBUS_UNREGISTERED,
414 		MDIOBUS_RELEASED,
415 	} state;
416 
417 	/** @dev: Kernel device representation */
418 	struct device dev;
419 
420 	/** @mdio_map: list of all MDIO devices on bus */
421 	struct mdio_device *mdio_map[PHY_MAX_ADDR];
422 
423 	/** @phy_mask: PHY addresses to be ignored when probing */
424 	u32 phy_mask;
425 
426 	/** @phy_ignore_ta_mask: PHY addresses to ignore the TA/read failure */
427 	u32 phy_ignore_ta_mask;
428 
429 	/**
430 	 * @irq: An array of interrupts, each PHY's interrupt at the index
431 	 * matching its address
432 	 */
433 	int irq[PHY_MAX_ADDR];
434 
435 	/** @reset_delay_us: GPIO reset pulse width in microseconds */
436 	int reset_delay_us;
437 	/** @reset_post_delay_us: GPIO reset deassert delay in microseconds */
438 	int reset_post_delay_us;
439 	/** @reset_gpiod: Reset GPIO descriptor pointer */
440 	struct gpio_desc *reset_gpiod;
441 
442 	/** @shared_lock: protect access to the shared element */
443 	struct mutex shared_lock;
444 
445 #if IS_ENABLED(CONFIG_PHY_PACKAGE)
446 	/** @shared: shared state across different PHYs */
447 	struct phy_package_shared *shared[PHY_MAX_ADDR];
448 #endif
449 };
450 #define to_mii_bus(d) container_of(d, struct mii_bus, dev)
451 
452 struct mii_bus *mdiobus_alloc_size(size_t size);
453 
454 /**
455  * mdiobus_alloc - Allocate an MDIO bus structure
456  *
457  * The internal state of the MDIO bus will be set of MDIOBUS_ALLOCATED ready
458  * for the driver to register the bus.
459  */
460 static inline struct mii_bus *mdiobus_alloc(void)
461 {
462 	return mdiobus_alloc_size(0);
463 }
464 
465 int __mdiobus_register(struct mii_bus *bus, struct module *owner);
466 int __devm_mdiobus_register(struct device *dev, struct mii_bus *bus,
467 			    struct module *owner);
468 #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
469 #define devm_mdiobus_register(dev, bus) \
470 		__devm_mdiobus_register(dev, bus, THIS_MODULE)
471 
472 void mdiobus_unregister(struct mii_bus *bus);
473 void mdiobus_free(struct mii_bus *bus);
474 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
475 static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
476 {
477 	return devm_mdiobus_alloc_size(dev, 0);
478 }
479 
480 struct mii_bus *mdio_find_bus(const char *mdio_name);
481 struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr);
482 
483 #define PHY_INTERRUPT_DISABLED	false
484 #define PHY_INTERRUPT_ENABLED	true
485 
486 /**
487  * enum phy_state - PHY state machine states:
488  *
489  * @PHY_DOWN: PHY device and driver are not ready for anything.  probe
490  * should be called if and only if the PHY is in this state,
491  * given that the PHY device exists.
492  * - PHY driver probe function will set the state to @PHY_READY
493  *
494  * @PHY_READY: PHY is ready to send and receive packets, but the
495  * controller is not.  By default, PHYs which do not implement
496  * probe will be set to this state by phy_probe().
497  * - start will set the state to UP
498  *
499  * @PHY_UP: The PHY and attached device are ready to do work.
500  * Interrupts should be started here.
501  * - timer moves to @PHY_NOLINK or @PHY_RUNNING
502  *
503  * @PHY_NOLINK: PHY is up, but not currently plugged in.
504  * - irq or timer will set @PHY_RUNNING if link comes back
505  * - phy_stop moves to @PHY_HALTED
506  *
507  * @PHY_RUNNING: PHY is currently up, running, and possibly sending
508  * and/or receiving packets
509  * - irq or timer will set @PHY_NOLINK if link goes down
510  * - phy_stop moves to @PHY_HALTED
511  *
512  * @PHY_CABLETEST: PHY is performing a cable test. Packet reception/sending
513  * is not expected to work, carrier will be indicated as down. PHY will be
514  * poll once per second, or on interrupt for it current state.
515  * Once complete, move to UP to restart the PHY.
516  * - phy_stop aborts the running test and moves to @PHY_HALTED
517  *
518  * @PHY_HALTED: PHY is up, but no polling or interrupts are done.
519  * - phy_start moves to @PHY_UP
520  *
521  * @PHY_ERROR: PHY is up, but is in an error state.
522  * - phy_stop moves to @PHY_HALTED
523  */
524 enum phy_state {
525 	PHY_DOWN = 0,
526 	PHY_READY,
527 	PHY_HALTED,
528 	PHY_ERROR,
529 	PHY_UP,
530 	PHY_RUNNING,
531 	PHY_NOLINK,
532 	PHY_CABLETEST,
533 };
534 
535 #define MDIO_MMD_NUM 32
536 
537 /**
538  * struct phy_c45_device_ids - 802.3-c45 Device Identifiers
539  * @devices_in_package: IEEE 802.3 devices in package register value.
540  * @mmds_present: bit vector of MMDs present.
541  * @device_ids: The device identifier for each present device.
542  */
543 struct phy_c45_device_ids {
544 	u32 devices_in_package;
545 	u32 mmds_present;
546 	u32 device_ids[MDIO_MMD_NUM];
547 };
548 
549 struct macsec_context;
550 struct macsec_ops;
551 
552 /**
553  * struct phy_oatc14_sqi_capability - SQI capability information for OATC14
554  *                                    10Base-T1S PHY
555  * @updated: Indicates whether the SQI capability fields have been updated.
556  * @sqi_max: Maximum supported Signal Quality Indicator (SQI) level reported by
557  *           the PHY.
558  * @sqiplus_bits: Bits for SQI+ levels supported by the PHY.
559  *                0 - SQI+ is not supported
560  *                3 - SQI+ is supported, using 3 bits (8 levels)
561  *                4 - SQI+ is supported, using 4 bits (16 levels)
562  *                5 - SQI+ is supported, using 5 bits (32 levels)
563  *                6 - SQI+ is supported, using 6 bits (64 levels)
564  *                7 - SQI+ is supported, using 7 bits (128 levels)
565  *                8 - SQI+ is supported, using 8 bits (256 levels)
566  *
567  * This structure is used by the OATC14 10Base-T1S PHY driver to store the SQI
568  * and SQI+ capability information retrieved from the PHY.
569  */
570 struct phy_oatc14_sqi_capability {
571 	bool updated;
572 	int sqi_max;
573 	u8 sqiplus_bits;
574 };
575 
576 /**
577  * struct phy_device - An instance of a PHY
578  *
579  * @mdio: MDIO bus this PHY is on
580  * @drv: Pointer to the driver for this PHY instance
581  * @drv_owner: Driver module phy_attach_direct() took a reference on
582  * @devlink: Create a link between phy dev and mac dev, if the external phy
583  *           used by current mac interface is managed by another mac interface.
584  * @phyindex: Unique id across the phy's parent tree of phys to address the PHY
585  *	      from userspace, similar to ifindex. A zero index means the PHY
586  *	      wasn't assigned an id yet.
587  * @phy_id: UID for this device found during discovery
588  * @c45_ids: 802.3-c45 Device Identifiers if is_c45.
589  * @is_c45:  Set to true if this PHY uses clause 45 addressing.
590  * @is_internal: Set to true if this PHY is internal to a MAC.
591  * @is_pseudo_fixed_link: Set to true if this PHY is an Ethernet switch, etc.
592  * @is_gigabit_capable: Set to true if PHY supports 1000Mbps
593  * @has_fixups: Set to true if this PHY has fixups/quirks.
594  * @suspended: Set to true if this PHY has been suspended successfully.
595  * @suspended_by_mdio_bus: Set to true if this PHY was suspended by MDIO bus.
596  * @sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
597  * @loopback_enabled: Set true if this PHY has been loopbacked successfully.
598  * @downshifted_rate: Set true if link speed has been downshifted.
599  * @is_on_sfp_module: Set true if PHY is located on an SFP module.
600  * @mac_managed_pm: Set true if MAC driver takes of suspending/resuming PHY
601  * @wol_enabled: Set to true if the PHY or the attached MAC have Wake-on-LAN
602  * 		 enabled.
603  * @is_genphy_driven: PHY is driven by one of the generic PHY drivers
604  * @state: State of the PHY for management purposes
605  * @dev_flags: Device-specific flags used by the PHY driver.
606  *
607  *      - Bits [15:0] are free to use by the PHY driver to communicate
608  *        driver specific behavior.
609  *      - Bits [23:16] are currently reserved for future use.
610  *      - Bits [31:24] are reserved for defining generic
611  *        PHY driver behavior.
612  * @irq: IRQ number of the PHY's interrupt (-1 if none)
613  * @phylink: Pointer to phylink instance for this PHY
614  * @sfp_bus_attached: Flag indicating whether the SFP bus has been attached
615  * @has_sfp_mod_phy: Set true if downstream SFP bus's module contains a PHY
616  * @sfp_bus: SFP bus attached to this PHY's fiber port
617  * @sfp_cage_port: The phy_port connected to the downstream SFP cage
618  * @mod_port: phy_port representing the SFP module, if it is phy-less
619  * @upstream_port: phy_port this PHY's MII attaches to, if any
620  * @attached_dev: The attached enet driver's device instance ptr
621  * @adjust_link: Callback for the enet controller to respond to changes: in the
622  *               link state.
623  * @phy_link_change: Callback for phylink for notification of link change
624  * @macsec_ops: MACsec offloading ops.
625  *
626  * @speed: Current link speed
627  * @duplex: Current duplex
628  * @port: Current port
629  * @pause: Current pause
630  * @asym_pause: Current asymmetric pause
631  * @supported: Combined MAC/PHY supported linkmodes
632  * @advertising: Currently advertised linkmodes
633  * @adv_old: Saved advertised while power saving for WoL
634  * @supported_eee: supported PHY EEE linkmodes
635  * @advertising_eee: Currently advertised EEE linkmodes
636  * @enable_tx_lpi: When True, MAC should transmit LPI to PHY
637  * @eee_active: phylib private state, indicating that EEE has been negotiated
638  * @autonomous_eee_disabled: Set when autonomous EEE has been disabled,
639  *	used to re-apply after PHY soft reset
640  * @eee_cfg: User configuration of EEE
641  * @lp_advertising: Current link partner advertised linkmodes
642  * @host_interfaces: PHY interface modes supported by host
643  * @eee_disabled_modes: Energy efficient ethernet modes not to be advertised
644  * @autoneg: Flag autoneg being used
645  * @rate_matching: Current rate matching mode
646  * @link: Current link state
647  * @autoneg_complete: Flag auto negotiation of the link has completed
648  * @mdix: Current crossover
649  * @mdix_ctrl: User setting of crossover
650  * @pma_extable: Cached value of PMA/PMD Extended Abilities Register
651  * @interrupts: Flag interrupts have been enabled
652  * @irq_suspended: Flag indicating PHY is suspended and therefore interrupt
653  *                 handling shall be postponed until PHY has resumed
654  * @irq_rerun: Flag indicating interrupts occurred while PHY was suspended,
655  *             requiring a rerun of the interrupt handler after resume
656  * @default_timestamp: Flag indicating whether we are using the phy
657  *		       timestamp as the default one
658  * @interface: enum phy_interface_t value
659  * @possible_interfaces: bitmap if interface modes that the attached PHY
660  *			 will switch between depending on media speed.
661  * @skb: Netlink message for cable diagnostics
662  * @nest: Netlink nest used for cable diagnostics
663  * @ehdr: nNtlink header for cable diagnostics
664  * @phy_led_triggers: Array of LED triggers
665  * @phy_num_led_triggers: Number of triggers in @phy_led_triggers
666  * @led_link_trigger: LED trigger for link up/down
667  * @last_triggered: last LED trigger for link speed
668  * @leds: list of PHY LED structures
669  * @master_slave_set: User requested master/slave configuration
670  * @master_slave_get: Current master/slave advertisement
671  * @master_slave_state: Current master/slave configuration
672  * @mii_ts: Pointer to time stamper callbacks
673  * @psec: Pointer to Power Sourcing Equipment control struct
674  * @ports: List of PHY ports structures
675  * @n_ports: Number of ports currently attached to the PHY
676  * @max_n_ports: Max number of ports this PHY can expose
677  * @lock:  Mutex for serialization access to PHY
678  * @state_queue: Work queue for state machine
679  * @link_down_events: Number of times link was lost
680  * @shared: Pointer to private data shared by phys in one package
681  * @priv: Pointer to driver private data
682  * @oatc14_sqi_capability: SQI capability information for OATC14 10Base-T1S PHY
683  *
684  * interrupts currently only supports enabled or disabled,
685  * but could be changed in the future to support enabling
686  * and disabling specific interrupts
687  *
688  * Contains some infrastructure for polling and interrupt
689  * handling, as well as handling shifts in PHY hardware state
690  */
691 struct phy_device {
692 	struct mdio_device mdio;
693 
694 	/* Information about the PHY type */
695 	/* And management functions */
696 	const struct phy_driver *drv;
697 	struct module *drv_owner;
698 
699 	struct device_link *devlink;
700 
701 	u32 phyindex;
702 	u32 phy_id;
703 
704 	struct phy_c45_device_ids c45_ids;
705 	unsigned is_c45:1;
706 	unsigned is_internal:1;
707 	unsigned is_pseudo_fixed_link:1;
708 	unsigned is_gigabit_capable:1;
709 	unsigned has_fixups:1;
710 	unsigned suspended:1;
711 	unsigned suspended_by_mdio_bus:1;
712 	unsigned sysfs_links:1;
713 	unsigned loopback_enabled:1;
714 	unsigned downshifted_rate:1;
715 	unsigned is_on_sfp_module:1;
716 	unsigned mac_managed_pm:1;
717 	unsigned wol_enabled:1;
718 	unsigned is_genphy_driven:1;
719 
720 	unsigned autoneg:1;
721 	/* The most recently read link state */
722 	unsigned link:1;
723 	unsigned autoneg_complete:1;
724 	bool pause:1;
725 	bool asym_pause:1;
726 
727 	/* Interrupts are enabled */
728 	unsigned interrupts:1;
729 	unsigned irq_suspended:1;
730 	unsigned irq_rerun:1;
731 
732 	unsigned default_timestamp:1;
733 
734 	int rate_matching;
735 
736 	enum phy_state state;
737 
738 	u32 dev_flags;
739 
740 	phy_interface_t interface;
741 	DECLARE_PHY_INTERFACE_MASK(possible_interfaces);
742 
743 	/*
744 	 * forced speed & duplex (no autoneg)
745 	 * partner speed & duplex & pause (autoneg)
746 	 */
747 	int speed;
748 	int duplex;
749 	int port;
750 	u8 master_slave_get;
751 	u8 master_slave_set;
752 	u8 master_slave_state;
753 
754 	/* Union of PHY and Attached devices' supported link modes */
755 	/* See ethtool.h for more info */
756 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
757 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
758 	__ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
759 	/* used with phy_speed_down */
760 	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
761 	/* used for eee validation and configuration*/
762 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported_eee);
763 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising_eee);
764 	/* Energy efficient ethernet modes which should be prohibited */
765 	__ETHTOOL_DECLARE_LINK_MODE_MASK(eee_disabled_modes);
766 	bool enable_tx_lpi;
767 	bool eee_active;
768 	bool autonomous_eee_disabled;
769 	struct eee_config eee_cfg;
770 
771 	/* Host supported PHY interface types. Should be ignored if empty. */
772 	DECLARE_PHY_INTERFACE_MASK(host_interfaces);
773 
774 #ifdef CONFIG_LED_TRIGGER_PHY
775 	struct phy_led_trigger *phy_led_triggers;
776 	unsigned int phy_num_led_triggers;
777 	struct phy_led_trigger *last_triggered;
778 
779 	struct phy_led_trigger *led_link_trigger;
780 #endif
781 	struct list_head leds;
782 
783 	/*
784 	 * Interrupt number for this PHY
785 	 * -1 means no interrupt
786 	 */
787 	int irq;
788 
789 	/* private data pointer */
790 	/* For use by PHYs to maintain extra state */
791 	void *priv;
792 
793 #if IS_ENABLED(CONFIG_PHY_PACKAGE)
794 	/* shared data pointer */
795 	/* For use by PHYs inside the same package that need a shared state. */
796 	struct phy_package_shared *shared;
797 #endif
798 
799 	/* Reporting cable test results */
800 	struct sk_buff *skb;
801 	void *ehdr;
802 	struct nlattr *nest;
803 
804 	/* Interrupt and Polling infrastructure */
805 	struct delayed_work state_queue;
806 
807 	struct mutex lock;
808 
809 	/* This may be modified under the rtnl lock */
810 	bool sfp_bus_attached;
811 	bool has_sfp_mod_phy;
812 	struct sfp_bus *sfp_bus;
813 	struct phy_port *sfp_cage_port;
814 	struct phy_port *mod_port;
815 	struct phy_port *upstream_port;
816 	struct phylink *phylink;
817 	struct net_device *attached_dev;
818 	struct mii_timestamper *mii_ts;
819 	struct pse_control *psec;
820 
821 	struct list_head ports;
822 	int n_ports;
823 	int max_n_ports;
824 
825 	u8 mdix;
826 	u8 mdix_ctrl;
827 
828 	int pma_extable;
829 
830 	unsigned int link_down_events;
831 
832 	void (*phy_link_change)(struct phy_device *phydev, bool up);
833 	void (*adjust_link)(struct net_device *dev);
834 
835 #if IS_ENABLED(CONFIG_MACSEC)
836 	/* MACsec management functions */
837 	const struct macsec_ops *macsec_ops;
838 #endif
839 
840 	struct phy_oatc14_sqi_capability oatc14_sqi_capability;
841 };
842 
843 /* Generic phy_device::dev_flags */
844 #define PHY_F_NO_IRQ			0x80000000
845 #define PHY_F_RXC_ALWAYS_ON		0x40000000
846 #define PHY_F_KEEP_PREAMBLE_BEFORE_SFD	0x20000000
847 
848 #define to_phy_device(__dev)	container_of_const(to_mdio_device(__dev), struct phy_device, mdio)
849 
850 #define phy_for_each_port(phydev, port) \
851 	list_for_each_entry(port, &(phydev)->ports, head)
852 
853 /**
854  * struct phy_tdr_config - Configuration of a TDR raw test
855  *
856  * @first: Distance for first data collection point
857  * @last: Distance for last data collection point
858  * @step: Step between data collection points
859  * @pair: Bitmap of cable pairs to collect data for
860  *
861  * A structure containing possible configuration parameters
862  * for a TDR cable test. The driver does not need to implement
863  * all the parameters, but should report what is actually used.
864  * All distances are in centimeters.
865  */
866 struct phy_tdr_config {
867 	u32 first;
868 	u32 last;
869 	u32 step;
870 	s8 pair;
871 };
872 #define PHY_PAIR_ALL -1
873 
874 /**
875  * enum link_inband_signalling - in-band signalling modes that are supported
876  *
877  * @LINK_INBAND_DISABLE: in-band signalling can be disabled
878  * @LINK_INBAND_ENABLE: in-band signalling can be enabled without bypass
879  * @LINK_INBAND_BYPASS: in-band signalling can be enabled with bypass
880  *
881  * The possible and required bits can only be used if the valid bit is set.
882  * If possible is clear, that means inband signalling can not be used.
883  * Required is only valid when possible is set, and means that inband
884  * signalling must be used.
885  */
886 enum link_inband_signalling {
887 	LINK_INBAND_DISABLE		= BIT(0),
888 	LINK_INBAND_ENABLE		= BIT(1),
889 	LINK_INBAND_BYPASS		= BIT(2),
890 };
891 
892 /**
893  * struct phy_plca_cfg - Configuration of the PLCA (Physical Layer Collision
894  * Avoidance) Reconciliation Sublayer.
895  *
896  * @version: read-only PLCA register map version. -1 = not available. Ignored
897  *   when setting the configuration. Format is the same as reported by the PLCA
898  *   IDVER register (31.CA00). -1 = not available.
899  * @enabled: PLCA configured mode (enabled/disabled). -1 = not available / don't
900  *   set. 0 = disabled, anything else = enabled.
901  * @node_id: the PLCA local node identifier. -1 = not available / don't set.
902  *   Allowed values [0 .. 254]. 255 = node disabled.
903  * @node_cnt: the PLCA node count (maximum number of nodes having a TO). Only
904  *   meaningful for the coordinator (node_id = 0). -1 = not available / don't
905  *   set. Allowed values [1 .. 255].
906  * @to_tmr: The value of the PLCA to_timer in bit-times, which determines the
907  *   PLCA transmit opportunity window opening. See IEEE802.3 Clause 148 for
908  *   more details. The to_timer shall be set equal over all nodes.
909  *   -1 = not available / don't set. Allowed values [0 .. 255].
910  * @burst_cnt: controls how many additional frames a node is allowed to send in
911  *   single transmit opportunity (TO). The default value of 0 means that the
912  *   node is allowed exactly one frame per TO. A value of 1 allows two frames
913  *   per TO, and so on. -1 = not available / don't set.
914  *   Allowed values [0 .. 255].
915  * @burst_tmr: controls how many bit times to wait for the MAC to send a new
916  *   frame before interrupting the burst. This value should be set to a value
917  *   greater than the MAC inter-packet gap (which is typically 96 bits).
918  *   -1 = not available / don't set. Allowed values [0 .. 255].
919  *
920  * A structure containing configuration parameters for setting/getting the PLCA
921  * RS configuration. The driver does not need to implement all the parameters,
922  * but should report what is actually used.
923  */
924 struct phy_plca_cfg {
925 	int version;
926 	int enabled;
927 	int node_id;
928 	int node_cnt;
929 	int to_tmr;
930 	int burst_cnt;
931 	int burst_tmr;
932 };
933 
934 /**
935  * struct phy_plca_status - Status of the PLCA (Physical Layer Collision
936  * Avoidance) Reconciliation Sublayer.
937  *
938  * @pst: The PLCA status as reported by the PST bit in the PLCA STATUS
939  *	register(31.CA03), indicating BEACON activity.
940  *
941  * A structure containing status information of the PLCA RS configuration.
942  * The driver does not need to implement all the parameters, but should report
943  * what is actually used.
944  */
945 struct phy_plca_status {
946 	bool pst;
947 };
948 
949 /* Modes for PHY LED configuration */
950 enum phy_led_modes {
951 	PHY_LED_ACTIVE_HIGH = 0,
952 	PHY_LED_ACTIVE_LOW = 1,
953 	PHY_LED_INACTIVE_HIGH_IMPEDANCE = 2,
954 
955 	/* keep it last */
956 	__PHY_LED_MODES_NUM,
957 };
958 
959 /**
960  * struct phy_led: An LED driven by the PHY
961  *
962  * @list: List of LEDs
963  * @phydev: PHY this LED is attached to
964  * @led_cdev: Standard LED class structure
965  * @index: Number of the LED
966  */
967 struct phy_led {
968 	struct list_head list;
969 	struct phy_device *phydev;
970 	struct led_classdev led_cdev;
971 	u8 index;
972 };
973 
974 #define to_phy_led(d) container_of(d, struct phy_led, led_cdev)
975 
976 /*
977  * PHY_MSE_CAP_* - Bitmask flags for Mean Square Error (MSE) capabilities
978  *
979  * These flags describe which MSE metrics and selectors are implemented
980  * by the PHY for the current link mode. They are used in
981  * struct phy_mse_capability.supported_caps.
982  *
983  * Standardization:
984  *   The OPEN Alliance (OA) defines the presence of MSE/SQI/pMSE but not their
985  *   numeric scaling, update intervals, or aggregation windows.  See:
986  *     OA 100BASE-T1 TC1 v1.0, sections 6.1.1-6.1.3
987  *     OA 1000BASE-T1 TC12 v2.2, sections 6.1.1-6.1.2
988  *
989  * Description of flags:
990  *
991  *   PHY_MSE_CAP_CHANNEL_A
992  *     Per-pair diagnostics for Channel A are supported.  Mapping to the
993  *     physical wire pair may depend on MDI/MDI-X polarity.
994  *
995  *   PHY_MSE_CAP_CHANNEL_B, _C, _D
996  *     Same as above for channels B-D.
997  *
998  *   PHY_MSE_CAP_WORST_CHANNEL
999  *     The PHY or driver can identify and report the single worst-performing
1000  *     channel without querying each one individually.
1001  *
1002  *   PHY_MSE_CAP_LINK
1003  *     The PHY provides only a link-wide aggregate measurement or cannot map
1004  *     results to a specific pair (for example 100BASE-TX with unknown
1005  *     MDI/MDI-X).
1006  *
1007  *   PHY_MSE_CAP_AVG
1008  *     Average MSE (mean DCQ metric) is supported.  For 100/1000BASE-T1 the OA
1009  *     recommends 2^16 symbols, scaled 0..511, but the exact scaling is
1010  *     vendor-specific.
1011  *
1012  *   PHY_MSE_CAP_PEAK
1013  *     Peak MSE (current peak within the measurement window) is supported.
1014  *     Defined as pMSE for 100BASE-T1; vendor-specific for others.
1015  *
1016  *   PHY_MSE_CAP_WORST_PEAK
1017  *     Latched worst-case peak MSE since the last read (read-to-clear if
1018  *     implemented).  Optional in OA 100BASE-T1 TC1 6.1.3.
1019  */
1020 #define PHY_MSE_CAP_CHANNEL_A BIT(0)
1021 #define PHY_MSE_CAP_CHANNEL_B BIT(1)
1022 #define PHY_MSE_CAP_CHANNEL_C BIT(2)
1023 #define PHY_MSE_CAP_CHANNEL_D BIT(3)
1024 #define PHY_MSE_CAP_WORST_CHANNEL BIT(4)
1025 #define PHY_MSE_CAP_LINK BIT(5)
1026 #define PHY_MSE_CAP_AVG BIT(6)
1027 #define PHY_MSE_CAP_PEAK BIT(7)
1028 #define PHY_MSE_CAP_WORST_PEAK BIT(8)
1029 
1030 /*
1031  * enum phy_mse_channel - Identifiers for selecting MSE measurement channels
1032  *
1033  * PHY_MSE_CHANNEL_A - PHY_MSE_CHANNEL_D
1034  *   Select per-pair measurement for the corresponding channel.
1035  *
1036  * PHY_MSE_CHANNEL_WORST
1037  *   Select the single worst-performing channel reported by hardware.
1038  *
1039  * PHY_MSE_CHANNEL_LINK
1040  *   Select link-wide aggregate data (used when per-pair results are
1041  *   unavailable).
1042  */
1043 enum phy_mse_channel {
1044 	PHY_MSE_CHANNEL_A,
1045 	PHY_MSE_CHANNEL_B,
1046 	PHY_MSE_CHANNEL_C,
1047 	PHY_MSE_CHANNEL_D,
1048 	PHY_MSE_CHANNEL_WORST,
1049 	PHY_MSE_CHANNEL_LINK,
1050 };
1051 
1052 /**
1053  * struct phy_mse_capability - Capabilities of Mean Square Error (MSE)
1054  *                             measurement interface
1055  *
1056  * Standardization notes:
1057  *
1058  * - Presence of MSE/SQI/pMSE is defined by OPEN Alliance specs, but numeric
1059  *   scaling, refresh/update rate and aggregation windows are not fixed and
1060  *   are vendor-/product-specific. (OA 100BASE-T1 TC1 v1.0 6.1.*;
1061  *   OA 1000BASE-T1 TC12 v2.2 6.1.*)
1062  *
1063  * - Typical recommendations: 2^16 symbols and 0..511 scaling for MSE; pMSE only
1064  *   defined for 100BASE-T1 (sliding window example), others are vendor
1065  *   extensions. Drivers must report actual scale/limits here.
1066  *
1067  * Describes the MSE measurement capabilities for the current link mode. These
1068  * properties are dynamic and may change when link settings are modified.
1069  * Callers should re-query this capability after any link state change to
1070  * ensure they have the most up-to-date information.
1071  *
1072  * Callers should only request measurements for channels and types that are
1073  * indicated as supported by the @supported_caps bitmask. If @supported_caps
1074  * is 0, the device provides no MSE diagnostics, and driver operations should
1075  * typically return -EOPNOTSUPP.
1076  *
1077  * Snapshot values for average and peak MSE can be normalized to a 0..1 ratio
1078  * by dividing the raw snapshot by the corresponding @max_average_mse or
1079  * @max_peak_mse value.
1080  *
1081  * @max_average_mse: The maximum value for an average MSE snapshot. This
1082  *   defines the scale for the measurement. If the PHY_MSE_CAP_AVG capability is
1083  *   supported, this value MUST be greater than 0. (vendor-specific units).
1084  * @max_peak_mse: The maximum value for a peak MSE snapshot. If either
1085  *   PHY_MSE_CAP_PEAK or PHY_MSE_CAP_WORST_PEAK is supported, this value MUST
1086  *   be greater than 0. (vendor-specific units).
1087  * @refresh_rate_ps: The typical interval, in picoseconds, between hardware
1088  *   updates of the MSE values. This is an estimate, and callers should not
1089  *   assume synchronous sampling. (vendor-specific units).
1090  * @num_symbols: The number of symbols aggregated per hardware sample to
1091  *   calculate the MSE. (vendor-specific units).
1092  * @supported_caps: A bitmask of PHY_MSE_CAP_* values indicating which
1093  *   measurement types (e.g., average, peak) and channels
1094  *   (e.g., per-pair or link-wide) are supported.
1095  */
1096 struct phy_mse_capability {
1097 	u64 max_average_mse;
1098 	u64 max_peak_mse;
1099 	u64 refresh_rate_ps;
1100 	u64 num_symbols;
1101 	u32 supported_caps;
1102 };
1103 
1104 /**
1105  * struct phy_mse_snapshot - A snapshot of Mean Square Error (MSE) diagnostics
1106  *
1107  * Holds a set of MSE diagnostic values that were all captured from a single
1108  * measurement window.
1109  *
1110  * Values are raw, device-scaled and not normalized. Use struct
1111  * phy_mse_capability to interpret the scale and sampling window.
1112  *
1113  * @average_mse: The average MSE value over the measurement window.
1114  *   OPEN Alliance references MSE as a DCQ metric; recommends 2^16 symbols and
1115  *   0..511 scaling. Exact scale and refresh are vendor-specific.
1116  *   (100BASE-T1 TC1 v1.0 6.1.1; 1000BASE-T1 TC12 v2.2 6.1.1).
1117  *
1118  * @peak_mse: The peak MSE value observed within the measurement window.
1119  *   For 100BASE-T1, "pMSE" is optional and may be implemented via a sliding
1120  *   128-symbol window with periodic capture; not standardized for 1000BASE-T1.
1121  *   (100BASE-T1 TC1 v1.0 6.1.3, Table "DCQ.peakMSE").
1122  *
1123  * @worst_peak_mse: A latched high-water mark of the peak MSE since last read
1124  *   (read-to-clear if implemented). OPEN Alliance shows a latched "worst case
1125  *   peak MSE" for 100BASE-T1 pMSE; availability/semantics outside that are
1126  *   vendor-specific. (100BASE-T1 TC1 v1.0 6.1.3, DCQ.peakMSE high byte;
1127  *   1000BASE-T1 TC12 v2.2 treats DCQ details as vendor-specific.)
1128  */
1129 struct phy_mse_snapshot {
1130 	u64 average_mse;
1131 	u64 peak_mse;
1132 	u64 worst_peak_mse;
1133 };
1134 
1135 /**
1136  * struct phy_driver - Driver structure for a particular PHY type
1137  *
1138  * @mdiodrv: Data common to all MDIO devices
1139  * @phy_id: The result of reading the UID registers of this PHY
1140  *   type, and ANDing them with the phy_id_mask.  This driver
1141  *   only works for PHYs with IDs which match this field
1142  * @name: The friendly name of this PHY type
1143  * @phy_id_mask: Defines the important bits of the phy_id
1144  * @features: A mandatory list of features (speed, duplex, etc)
1145  *   supported by this PHY
1146  * @flags: A bitfield defining certain other features this PHY
1147  *   supports (like interrupts)
1148  * @driver_data: Static driver data
1149  *
1150  * All functions are optional. If config_aneg or read_status
1151  * are not implemented, the phy core uses the genphy versions.
1152  * Note that none of these functions should be called from
1153  * interrupt time. The goal is for the bus read/write functions
1154  * to be able to block when the bus transaction is happening,
1155  * and be freed up by an interrupt (The MPC85xx has this ability,
1156  * though it is not currently supported in the driver).
1157  */
1158 struct phy_driver {
1159 	struct mdio_driver_common mdiodrv;
1160 	u32 phy_id;
1161 	char *name;
1162 	u32 phy_id_mask;
1163 	const unsigned long * const features;
1164 	u32 flags;
1165 	const void *driver_data;
1166 
1167 	/**
1168 	 * @soft_reset: Called to issue a PHY software reset
1169 	 */
1170 	int (*soft_reset)(struct phy_device *phydev);
1171 
1172 	/**
1173 	 * @config_init: Called to initialize the PHY,
1174 	 * including after a reset
1175 	 */
1176 	int (*config_init)(struct phy_device *phydev);
1177 
1178 	/**
1179 	 * @probe: Called during discovery.  Used to set
1180 	 * up device-specific structures, if any
1181 	 */
1182 	int (*probe)(struct phy_device *phydev);
1183 
1184 	/**
1185 	 * @get_features: Probe the hardware to determine what
1186 	 * abilities it has.  Should only set phydev->supported.
1187 	 */
1188 	int (*get_features)(struct phy_device *phydev);
1189 
1190 	/**
1191 	 * @inband_caps: query whether in-band is supported for the given PHY
1192 	 * interface mode. Returns a bitmask of bits defined by enum
1193 	 * link_inband_signalling.
1194 	 */
1195 	unsigned int (*inband_caps)(struct phy_device *phydev,
1196 				    phy_interface_t interface);
1197 
1198 	/**
1199 	 * @config_inband: configure in-band mode for the PHY
1200 	 */
1201 	int (*config_inband)(struct phy_device *phydev, unsigned int modes);
1202 
1203 	/**
1204 	 * @get_rate_matching: Get the supported type of rate matching for a
1205 	 * particular phy interface. This is used by phy consumers to determine
1206 	 * whether to advertise lower-speed modes for that interface. It is
1207 	 * assumed that if a rate matching mode is supported on an interface,
1208 	 * then that interface's rate can be adapted to all slower link speeds
1209 	 * supported by the phy. If the interface is not supported, this should
1210 	 * return %RATE_MATCH_NONE.
1211 	 */
1212 	int (*get_rate_matching)(struct phy_device *phydev,
1213 				   phy_interface_t iface);
1214 
1215 	/* PHY Power Management */
1216 	/** @suspend: Suspend the hardware, saving state if needed */
1217 	int (*suspend)(struct phy_device *phydev);
1218 	/** @resume: Resume the hardware, restoring state if needed */
1219 	int (*resume)(struct phy_device *phydev);
1220 
1221 	/**
1222 	 * @config_aneg: Configures the advertisement and resets
1223 	 * autonegotiation if phydev->autoneg is on,
1224 	 * forces the speed to the current settings in phydev
1225 	 * if phydev->autoneg is off
1226 	 */
1227 	int (*config_aneg)(struct phy_device *phydev);
1228 
1229 	/** @aneg_done: Determines the auto negotiation result */
1230 	int (*aneg_done)(struct phy_device *phydev);
1231 
1232 	/** @read_status: Determines the negotiated speed and duplex */
1233 	int (*read_status)(struct phy_device *phydev);
1234 
1235 	/**
1236 	 * @config_intr: Enables or disables interrupts.
1237 	 * It should also clear any pending interrupts prior to enabling the
1238 	 * IRQs and after disabling them.
1239 	 */
1240 	int (*config_intr)(struct phy_device *phydev);
1241 
1242 	/** @handle_interrupt: Override default interrupt handling */
1243 	irqreturn_t (*handle_interrupt)(struct phy_device *phydev);
1244 
1245 	/** @remove: Clears up any memory if needed */
1246 	void (*remove)(struct phy_device *phydev);
1247 
1248 	/**
1249 	 * @match_phy_device: Returns true if this is a suitable
1250 	 * driver for the given phydev.	 If NULL, matching is based on
1251 	 * phy_id and phy_id_mask.
1252 	 */
1253 	int (*match_phy_device)(struct phy_device *phydev,
1254 				const struct phy_driver *phydrv);
1255 
1256 	/**
1257 	 * @set_wol: Some devices (e.g. qnap TS-119P II) require PHY
1258 	 * register changes to enable Wake on LAN, so set_wol is
1259 	 * provided to be called in the ethernet driver's set_wol
1260 	 * function.
1261 	 */
1262 	int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
1263 
1264 	/**
1265 	 * @get_wol: See set_wol, but for checking whether Wake on LAN
1266 	 * is enabled.
1267 	 */
1268 	void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
1269 
1270 	/**
1271 	 * @link_change_notify: Called to inform a PHY device driver
1272 	 * when the core is about to change the link state. This
1273 	 * callback is supposed to be used as fixup hook for drivers
1274 	 * that need to take action when the link state
1275 	 * changes. Drivers are by no means allowed to mess with the
1276 	 * PHY device structure in their implementations.
1277 	 */
1278 	void (*link_change_notify)(struct phy_device *dev);
1279 
1280 	/**
1281 	 * @read_mmd: PHY specific driver override for reading a MMD
1282 	 * register.  This function is optional for PHY specific
1283 	 * drivers.  When not provided, the default MMD read function
1284 	 * will be used by phy_read_mmd(), which will use either a
1285 	 * direct read for Clause 45 PHYs or an indirect read for
1286 	 * Clause 22 PHYs.  devnum is the MMD device number within the
1287 	 * PHY device, regnum is the register within the selected MMD
1288 	 * device.
1289 	 */
1290 	int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
1291 
1292 	/**
1293 	 * @write_mmd: PHY specific driver override for writing a MMD
1294 	 * register.  This function is optional for PHY specific
1295 	 * drivers.  When not provided, the default MMD write function
1296 	 * will be used by phy_write_mmd(), which will use either a
1297 	 * direct write for Clause 45 PHYs, or an indirect write for
1298 	 * Clause 22 PHYs.  devnum is the MMD device number within the
1299 	 * PHY device, regnum is the register within the selected MMD
1300 	 * device.  val is the value to be written.
1301 	 */
1302 	int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
1303 			 u16 val);
1304 
1305 	/** @read_page: Return the current PHY register page number */
1306 	int (*read_page)(struct phy_device *dev);
1307 	/** @write_page: Set the current PHY register page number */
1308 	int (*write_page)(struct phy_device *dev, int page);
1309 
1310 	/**
1311 	 * @module_info: Get the size and type of the eeprom contained
1312 	 * within a plug-in module
1313 	 */
1314 	int (*module_info)(struct phy_device *dev,
1315 			   struct ethtool_modinfo *modinfo);
1316 
1317 	/**
1318 	 * @module_eeprom: Get the eeprom information from the plug-in
1319 	 * module
1320 	 */
1321 	int (*module_eeprom)(struct phy_device *dev,
1322 			     struct ethtool_eeprom *ee, u8 *data);
1323 
1324 	/** @cable_test_start: Start a cable test */
1325 	int (*cable_test_start)(struct phy_device *dev);
1326 
1327 	/**  @cable_test_tdr_start: Start a raw TDR cable test */
1328 	int (*cable_test_tdr_start)(struct phy_device *dev,
1329 				    const struct phy_tdr_config *config);
1330 
1331 	/**
1332 	 * @cable_test_get_status: Once per second, or on interrupt,
1333 	 * request the status of the test.
1334 	 */
1335 	int (*cable_test_get_status)(struct phy_device *dev, bool *finished);
1336 
1337 	/* Get statistics from the PHY using ethtool */
1338 	/**
1339 	 * @get_phy_stats: Retrieve PHY statistics.
1340 	 * @dev: The PHY device for which the statistics are retrieved.
1341 	 * @eth_stats: structure where Ethernet PHY stats will be stored.
1342 	 * @stats: structure where additional PHY-specific stats will be stored.
1343 	 *
1344 	 * Retrieves the supported PHY statistics and populates the provided
1345 	 * structures. The input structures are pre-initialized with
1346 	 * `ETHTOOL_STAT_NOT_SET`, and the driver must only modify members
1347 	 * corresponding to supported statistics. Unmodified members will remain
1348 	 * set to `ETHTOOL_STAT_NOT_SET` and will not be returned to userspace.
1349 	 */
1350 	void (*get_phy_stats)(struct phy_device *dev,
1351 			      struct ethtool_eth_phy_stats *eth_stats,
1352 			      struct ethtool_phy_stats *stats);
1353 
1354 	/**
1355 	 * @get_link_stats: Retrieve link statistics.
1356 	 * @dev: The PHY device for which the statistics are retrieved.
1357 	 * @link_stats: structure where link-specific stats will be stored.
1358 	 *
1359 	 * Retrieves link-related statistics for the given PHY device. The input
1360 	 * structure is pre-initialized with `ETHTOOL_STAT_NOT_SET`, and the
1361 	 * driver must only modify members corresponding to supported
1362 	 * statistics. Unmodified members will remain set to
1363 	 * `ETHTOOL_STAT_NOT_SET` and will not be returned to userspace.
1364 	 */
1365 	void (*get_link_stats)(struct phy_device *dev,
1366 			       struct ethtool_link_ext_stats *link_stats);
1367 
1368 	/**
1369 	 * @update_stats: Trigger periodic statistics updates.
1370 	 * @dev: The PHY device for which statistics updates are triggered.
1371 	 *
1372 	 * Periodically gathers statistics from the PHY device to update locally
1373 	 * maintained 64-bit counters. This is necessary for PHYs that implement
1374 	 * reduced-width counters (e.g., 16-bit or 32-bit) which can overflow
1375 	 * more frequently compared to 64-bit counters. By invoking this
1376 	 * callback, drivers can fetch the current counter values, handle
1377 	 * overflow detection, and accumulate the results into local 64-bit
1378 	 * counters for accurate reporting through the `get_phy_stats` and
1379 	 * `get_link_stats` interfaces.
1380 	 *
1381 	 * Return: 0 on success or a negative error code on failure.
1382 	 */
1383 	int (*update_stats)(struct phy_device *dev);
1384 
1385 	/** @get_sset_count: Number of statistic counters */
1386 	int (*get_sset_count)(struct phy_device *dev);
1387 	/** @get_strings: Names of the statistic counters */
1388 	void (*get_strings)(struct phy_device *dev, u8 *data);
1389 	/** @get_stats: Return the statistic counter values */
1390 	void (*get_stats)(struct phy_device *dev,
1391 			  struct ethtool_stats *stats, u64 *data);
1392 
1393 	/**
1394 	 * @disable_autonomous_eee: Disable PHY-autonomous EEE
1395 	 *
1396 	 * Some PHYs manage EEE autonomously, preventing the MAC from
1397 	 * controlling LPI signaling. This callback disables autonomous
1398 	 * EEE at the PHY.
1399 	 *
1400 	 * Return: 0 on success, negative errno on failure.
1401 	 */
1402 	int (*disable_autonomous_eee)(struct phy_device *dev);
1403 
1404 	/* Get and Set PHY tunables */
1405 	/** @get_tunable: Return the value of a tunable */
1406 	int (*get_tunable)(struct phy_device *dev,
1407 			   struct ethtool_tunable *tuna, void *data);
1408 	/** @set_tunable: Set the value of a tunable */
1409 	int (*set_tunable)(struct phy_device *dev,
1410 			    struct ethtool_tunable *tuna,
1411 			    const void *data);
1412 	/**
1413 	 * @set_loopback: Set the loopback mode of the PHY
1414 	 * enable selects if the loopback mode is enabled or disabled. If the
1415 	 * loopback mode is enabled, then the speed of the loopback mode can be
1416 	 * requested with the speed argument. If the speed argument is zero,
1417 	 * then any speed can be selected. If the speed argument is > 0, then
1418 	 * this speed shall be selected for the loopback mode or EOPNOTSUPP
1419 	 * shall be returned if speed selection is not supported.
1420 	 */
1421 	int (*set_loopback)(struct phy_device *dev, bool enable, int speed);
1422 	/** @get_sqi: Get the signal quality indication */
1423 	int (*get_sqi)(struct phy_device *dev);
1424 	/** @get_sqi_max: Get the maximum signal quality indication */
1425 	int (*get_sqi_max)(struct phy_device *dev);
1426 
1427 	/**
1428 	 * @get_mse_capability: Get capabilities and scale of MSE measurement
1429 	 * @dev:    PHY device
1430 	 * @cap: Output (filled on success)
1431 	 *
1432 	 * Fill @cap with the PHY's MSE capability for the current
1433 	 * link mode: scale limits (max_average_mse, max_peak_mse), update
1434 	 * interval (refresh_rate_ps), sample length (num_symbols) and the
1435 	 * capability bitmask (supported_caps).
1436 	 *
1437 	 * Implementations may defer capability report until hardware has
1438 	 * converged; in that case they should return -EAGAIN and allow the
1439 	 * caller to retry later.
1440 	 *
1441 	 * Return: 0 on success. On failure, returns a negative errno code, such
1442 	 * as -EOPNOTSUPP if MSE measurement is not supported by the PHY or in
1443 	 * the current link mode, or -EAGAIN if the capability information is
1444 	 * not yet available.
1445 	 */
1446 	int (*get_mse_capability)(struct phy_device *dev,
1447 				  struct phy_mse_capability *cap);
1448 
1449 	/**
1450 	 * @get_mse_snapshot: Retrieve a snapshot of MSE diagnostic values
1451 	 * @dev:      PHY device
1452 	 * @channel:  Channel identifier (PHY_MSE_CHANNEL_*)
1453 	 * @snapshot: Output (filled on success)
1454 	 *
1455 	 * Fill @snapshot with a correlated set of MSE values from the most
1456 	 * recent measurement window.
1457 	 *
1458 	 * Callers must validate @channel against supported_caps returned by
1459 	 * get_mse_capability(). Drivers must not coerce @channel; if the
1460 	 * requested selector is not implemented by the device or current link
1461 	 * mode, the operation must fail.
1462 	 *
1463 	 * worst_peak_mse is latched and must be treated as read-to-clear.
1464 	 *
1465 	 * Return: 0 on success. On failure, returns a negative errno code, such
1466 	 * as -EOPNOTSUPP if MSE measurement is not supported by the PHY or in
1467 	 * the current link mode, or -EAGAIN if measurements are not yet
1468 	 * available.
1469 	 */
1470 	int (*get_mse_snapshot)(struct phy_device *dev,
1471 				enum phy_mse_channel channel,
1472 				struct phy_mse_snapshot *snapshot);
1473 
1474 	/* PLCA RS interface */
1475 	/** @get_plca_cfg: Return the current PLCA configuration */
1476 	int (*get_plca_cfg)(struct phy_device *dev,
1477 			    struct phy_plca_cfg *plca_cfg);
1478 	/** @set_plca_cfg: Set the PLCA configuration */
1479 	int (*set_plca_cfg)(struct phy_device *dev,
1480 			    const struct phy_plca_cfg *plca_cfg);
1481 	/** @get_plca_status: Return the current PLCA status info */
1482 	int (*get_plca_status)(struct phy_device *dev,
1483 			       struct phy_plca_status *plca_st);
1484 
1485 	/**
1486 	 * @led_brightness_set: Set a PHY LED brightness. Index
1487 	 * indicates which of the PHYs led should be set. Value
1488 	 * follows the standard LED class meaning, e.g. LED_OFF,
1489 	 * LED_HALF, LED_FULL.
1490 	 */
1491 	int (*led_brightness_set)(struct phy_device *dev,
1492 				  u8 index, enum led_brightness value);
1493 
1494 	/**
1495 	 * @led_blink_set: Set a PHY LED blinking.  Index indicates
1496 	 * which of the PHYs led should be configured to blink. Delays
1497 	 * are in milliseconds and if both are zero then a sensible
1498 	 * default should be chosen.  The call should adjust the
1499 	 * timings in that case and if it can't match the values
1500 	 * specified exactly.
1501 	 */
1502 	int (*led_blink_set)(struct phy_device *dev, u8 index,
1503 			     unsigned long *delay_on,
1504 			     unsigned long *delay_off);
1505 	/**
1506 	 * @led_hw_is_supported: Can the HW support the given rules.
1507 	 * @dev: PHY device which has the LED
1508 	 * @index: Which LED of the PHY device
1509 	 * @rules The core is interested in these rules
1510 	 *
1511 	 * Return 0 if yes,  -EOPNOTSUPP if not, or an error code.
1512 	 */
1513 	int (*led_hw_is_supported)(struct phy_device *dev, u8 index,
1514 				   unsigned long rules);
1515 	/**
1516 	 * @led_hw_control_set: Set the HW to control the LED
1517 	 * @dev: PHY device which has the LED
1518 	 * @index: Which LED of the PHY device
1519 	 * @rules The rules used to control the LED
1520 	 *
1521 	 * Returns 0, or a an error code.
1522 	 */
1523 	int (*led_hw_control_set)(struct phy_device *dev, u8 index,
1524 				  unsigned long rules);
1525 	/**
1526 	 * @led_hw_control_get: Get how the HW is controlling the LED
1527 	 * @dev: PHY device which has the LED
1528 	 * @index: Which LED of the PHY device
1529 	 * @rules Pointer to the rules used to control the LED
1530 	 *
1531 	 * Set *@rules to how the HW is currently blinking. Returns 0
1532 	 * on success, or a error code if the current blinking cannot
1533 	 * be represented in rules, or some other error happens.
1534 	 */
1535 	int (*led_hw_control_get)(struct phy_device *dev, u8 index,
1536 				  unsigned long *rules);
1537 
1538 	/**
1539 	 * @led_polarity_set: Set the LED polarity modes
1540 	 * @dev: PHY device which has the LED
1541 	 * @index: Which LED of the PHY device
1542 	 * @modes: bitmap of LED polarity modes
1543 	 *
1544 	 * Configure LED with all the required polarity modes in @modes
1545 	 * to make it correctly turn ON or OFF.
1546 	 *
1547 	 * Returns 0, or an error code.
1548 	 */
1549 	int (*led_polarity_set)(struct phy_device *dev, int index,
1550 				unsigned long modes);
1551 
1552 	/**
1553 	 * @get_next_update_time: Get the time until the next update event
1554 	 * @dev: PHY device
1555 	 *
1556 	 * Callback to determine the time (in jiffies) until the next
1557 	 * update event for the PHY state  machine. Allows PHY drivers to
1558 	 * dynamically adjust polling intervals based on link state or other
1559 	 * conditions.
1560 	 *
1561 	 * Returns the time in jiffies until the next update event.
1562 	 */
1563 	unsigned int (*get_next_update_time)(struct phy_device *dev);
1564 
1565 	/**
1566 	 * @attach_mii_port: Attach the given MII port to the PHY device
1567 	 * @dev: PHY device to notify
1568 	 * @port: The port being added
1569 	 *
1570 	 * Called when an MII port that needs to be driven by the PHY is found.
1571 	 *
1572 	 * The port that is being passed may or may not be initialized. If it is
1573 	 * already initialized, it is by the generic port representation from
1574 	 * devicetree, which superseeds any strapping or vendor-specific
1575 	 * properties.
1576 	 *
1577 	 * If the port isn't initialized, the port->mediums and port->lanes
1578 	 * fields must be set, possibly according to strapping information.
1579 	 *
1580 	 * The PHY driver must set the port->interfaces field to indicate the
1581 	 * possible MII modes that this PHY can output on the port.
1582 	 *
1583 	 * Returns 0, or an error code.
1584 	 */
1585 	int (*attach_mii_port)(struct phy_device *dev, struct phy_port *port);
1586 
1587 	/**
1588 	 * @attach_mdi_port: Attach the given MII port to the PHY device
1589 	 * @dev: PHY device to notify
1590 	 * @port: The port being added
1591 	 *
1592 	 * Called when a port that needs to be driven by the PHY is found. The
1593 	 * number of time this will be called depends on phydev->max_n_ports,
1594 	 * which the driver can change in .probe().
1595 	 *
1596 	 * The port that is being passed may or may not be initialized. If it is
1597 	 * already initialized, it is by the generic port representation from
1598 	 * devicetree, which superseeds any strapping or vendor-specific
1599 	 * properties.
1600 	 *
1601 	 * If the port isn't initialized, the port->mediums and port->lanes
1602 	 * fields must be set, possibly according to strapping information.
1603 	 *
1604 	 * Returns 0, or an error code.
1605 	 */
1606 	int (*attach_mdi_port)(struct phy_device *dev, struct phy_port *port);
1607 };
1608 #define to_phy_driver(d) container_of_const(to_mdio_common_driver(d),		\
1609 				      struct phy_driver, mdiodrv)
1610 
1611 #define PHY_ID_MATCH_EXTACT_MASK GENMASK(31, 0)
1612 #define PHY_ID_MATCH_MODEL_MASK GENMASK(31, 4)
1613 #define PHY_ID_MATCH_VENDOR_MASK GENMASK(31, 10)
1614 
1615 #define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = PHY_ID_MATCH_EXTACT_MASK
1616 #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = PHY_ID_MATCH_MODEL_MASK
1617 #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = PHY_ID_MATCH_VENDOR_MASK
1618 
1619 /**
1620  * phy_id_compare - compare @id1 with @id2 taking account of @mask
1621  * @id1: first PHY ID
1622  * @id2: second PHY ID
1623  * @mask: the PHY ID mask, set bits are significant in matching
1624  *
1625  * Return true if the bits from @id1 and @id2 specified by @mask match.
1626  * This uses an equivalent test to (@id & @mask) == (@phy_id & @mask).
1627  */
1628 static inline bool phy_id_compare(u32 id1, u32 id2, u32 mask)
1629 {
1630 	return !((id1 ^ id2) & mask);
1631 }
1632 
1633 /**
1634  * phy_id_compare_vendor - compare @id with @vendor mask
1635  * @id: PHY ID
1636  * @vendor_mask: PHY Vendor mask
1637  *
1638  * Return: true if the bits from @id match @vendor using the
1639  *	   generic PHY Vendor mask.
1640  */
1641 static inline bool phy_id_compare_vendor(u32 id, u32 vendor_mask)
1642 {
1643 	return phy_id_compare(id, vendor_mask, PHY_ID_MATCH_VENDOR_MASK);
1644 }
1645 
1646 /**
1647  * phy_id_compare_model - compare @id with @model mask
1648  * @id: PHY ID
1649  * @model_mask: PHY Model mask
1650  *
1651  * Return: true if the bits from @id match @model using the
1652  *	   generic PHY Model mask.
1653  */
1654 static inline bool phy_id_compare_model(u32 id, u32 model_mask)
1655 {
1656 	return phy_id_compare(id, model_mask, PHY_ID_MATCH_MODEL_MASK);
1657 }
1658 
1659 /**
1660  * phydev_id_compare - compare @id with the PHY's Clause 22 ID
1661  * @phydev: the PHY device
1662  * @id: the PHY ID to be matched
1663  *
1664  * Compare the @phydev clause 22 ID with the provided @id and return true or
1665  * false depending whether it matches, using the bound driver mask. The
1666  * @phydev must be bound to a driver.
1667  */
1668 static inline bool phydev_id_compare(struct phy_device *phydev, u32 id)
1669 {
1670 	return phy_id_compare(id, phydev->phy_id, phydev->drv->phy_id_mask);
1671 }
1672 
1673 const char *phy_speed_to_str(int speed);
1674 const char *phy_duplex_to_str(unsigned int duplex);
1675 const char *phy_rate_matching_to_str(int rate_matching);
1676 
1677 int phy_interface_num_ports(phy_interface_t interface);
1678 
1679 /**
1680  * phy_is_started - Convenience function to check whether PHY is started
1681  * @phydev: The phy_device struct
1682  */
1683 static inline bool phy_is_started(struct phy_device *phydev)
1684 {
1685 	return phydev->state >= PHY_UP;
1686 }
1687 
1688 /**
1689  * phy_driver_is_genphy - Convenience function to check whether PHY is driven
1690  *                        by one of the generic PHY drivers
1691  * @phydev: The phy_device struct
1692  * Return: true if PHY is driven by one of the genphy drivers
1693  */
1694 static inline bool phy_driver_is_genphy(struct phy_device *phydev)
1695 {
1696 	return phydev->is_genphy_driven;
1697 }
1698 
1699 /**
1700  * phy_disable_eee_mode - Don't advertise an EEE mode.
1701  * @phydev: The phy_device struct
1702  * @link_mode: The EEE mode to be disabled
1703  */
1704 static inline void phy_disable_eee_mode(struct phy_device *phydev, u32 link_mode)
1705 {
1706 	WARN_ON(phy_is_started(phydev));
1707 
1708 	linkmode_set_bit(link_mode, phydev->eee_disabled_modes);
1709 	linkmode_clear_bit(link_mode, phydev->advertising_eee);
1710 }
1711 
1712 /**
1713  * phy_can_wakeup() - indicate whether PHY has driver model wakeup capabilities
1714  * @phydev: The phy_device struct
1715  *
1716  * Returns: true/false depending on the PHY driver's device_set_wakeup_capable()
1717  * setting.
1718  */
1719 static inline bool phy_can_wakeup(struct phy_device *phydev)
1720 {
1721 	return device_can_wakeup(&phydev->mdio.dev);
1722 }
1723 
1724 /**
1725  * phy_may_wakeup() - indicate whether PHY has wakeup enabled
1726  * @phydev: The phy_device struct
1727  *
1728  * Returns: true/false depending on the PHY driver's device_set_wakeup_enable()
1729  * setting if using the driver model, otherwise the legacy determination.
1730  */
1731 bool phy_may_wakeup(struct phy_device *phydev);
1732 
1733 void phy_resolve_aneg_pause(struct phy_device *phydev);
1734 void phy_resolve_aneg_linkmode(struct phy_device *phydev);
1735 
1736 /**
1737  * phy_read - Convenience function for reading a given PHY register
1738  * @phydev: the phy_device struct
1739  * @regnum: register number to read
1740  *
1741  * NOTE: MUST NOT be called from interrupt context,
1742  * because the bus read/write functions may wait for an interrupt
1743  * to conclude the operation.
1744  */
1745 static inline int phy_read(struct phy_device *phydev, u32 regnum)
1746 {
1747 	return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
1748 }
1749 
1750 #define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
1751 				timeout_us, sleep_before_read) \
1752 ({ \
1753 	int __ret, __val; \
1754 	__ret = read_poll_timeout(__val = phy_read, val, \
1755 				  __val < 0 || (cond), \
1756 		sleep_us, timeout_us, sleep_before_read, phydev, regnum); \
1757 	if (__val < 0) \
1758 		__ret = __val; \
1759 	if (__ret) \
1760 		phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
1761 	__ret; \
1762 })
1763 
1764 /**
1765  * __phy_read - convenience function for reading a given PHY register
1766  * @phydev: the phy_device struct
1767  * @regnum: register number to read
1768  *
1769  * The caller must have taken the MDIO bus lock.
1770  */
1771 static inline int __phy_read(struct phy_device *phydev, u32 regnum)
1772 {
1773 	return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
1774 }
1775 
1776 /**
1777  * phy_write - Convenience function for writing a given PHY register
1778  * @phydev: the phy_device struct
1779  * @regnum: register number to write
1780  * @val: value to write to @regnum
1781  *
1782  * NOTE: MUST NOT be called from interrupt context,
1783  * because the bus read/write functions may wait for an interrupt
1784  * to conclude the operation.
1785  */
1786 static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
1787 {
1788 	return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
1789 }
1790 
1791 /**
1792  * __phy_write - Convenience function for writing a given PHY register
1793  * @phydev: the phy_device struct
1794  * @regnum: register number to write
1795  * @val: value to write to @regnum
1796  *
1797  * The caller must have taken the MDIO bus lock.
1798  */
1799 static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
1800 {
1801 	return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum,
1802 			       val);
1803 }
1804 
1805 /**
1806  * __phy_modify_changed() - Convenience function for modifying a PHY register
1807  * @phydev: a pointer to a &struct phy_device
1808  * @regnum: register number
1809  * @mask: bit mask of bits to clear
1810  * @set: bit mask of bits to set
1811  *
1812  * Unlocked helper function which allows a PHY register to be modified as
1813  * new register value = (old register value & ~mask) | set
1814  *
1815  * Returns negative errno, 0 if there was no change, and 1 in case of change
1816  */
1817 static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
1818 				       u16 mask, u16 set)
1819 {
1820 	return __mdiobus_modify_changed(phydev->mdio.bus, phydev->mdio.addr,
1821 					regnum, mask, set);
1822 }
1823 
1824 /*
1825  * phy_read_mmd - Convenience function for reading a register
1826  * from an MMD on a given PHY.
1827  */
1828 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1829 
1830 /**
1831  * phy_read_mmd_poll_timeout - Periodically poll a PHY register until a
1832  *                             condition is met or a timeout occurs
1833  *
1834  * @phydev: The phy_device struct
1835  * @devaddr: The MMD to read from
1836  * @regnum: The register on the MMD to read
1837  * @val: Variable to read the register into
1838  * @cond: Break condition (usually involving @val)
1839  * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops). Please
1840  *            read usleep_range() function description for details and
1841  *            limitations.
1842  * @timeout_us: Timeout in us, 0 means never timeout
1843  * @sleep_before_read: if it is true, sleep @sleep_us before read.
1844  *
1845  * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
1846  * case, the last read value at @args is stored in @val. Must not
1847  * be called from atomic context if sleep_us or timeout_us are used.
1848  */
1849 #define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
1850 				  sleep_us, timeout_us, sleep_before_read) \
1851 ({ \
1852 	int __ret, __val; \
1853 	__ret = read_poll_timeout(__val = phy_read_mmd, val, \
1854 				  __val < 0 || (cond), \
1855 				  sleep_us, timeout_us, sleep_before_read, \
1856 				  phydev, devaddr, regnum); \
1857 	if (__val < 0) \
1858 		__ret = __val; \
1859 	if (__ret) \
1860 		phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
1861 	__ret; \
1862 })
1863 
1864 /*
1865  * __phy_read_mmd - Convenience function for reading a register
1866  * from an MMD on a given PHY.
1867  */
1868 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1869 
1870 /*
1871  * phy_write_mmd - Convenience function for writing a register
1872  * on an MMD on a given PHY.
1873  */
1874 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1875 
1876 /*
1877  * __phy_write_mmd - Convenience function for writing a register
1878  * on an MMD on a given PHY.
1879  */
1880 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1881 
1882 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1883 			 u16 set);
1884 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1885 		       u16 set);
1886 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1887 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1888 
1889 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1890 			     u16 mask, u16 set);
1891 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1892 			   u16 mask, u16 set);
1893 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1894 		     u16 mask, u16 set);
1895 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1896 		   u16 mask, u16 set);
1897 
1898 /**
1899  * __phy_set_bits - Convenience function for setting bits in a PHY register
1900  * @phydev: the phy_device struct
1901  * @regnum: register number to write
1902  * @val: bits to set
1903  *
1904  * The caller must have taken the MDIO bus lock.
1905  */
1906 static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1907 {
1908 	return __phy_modify(phydev, regnum, 0, val);
1909 }
1910 
1911 /**
1912  * __phy_clear_bits - Convenience function for clearing bits in a PHY register
1913  * @phydev: the phy_device struct
1914  * @regnum: register number to write
1915  * @val: bits to clear
1916  *
1917  * The caller must have taken the MDIO bus lock.
1918  */
1919 static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum,
1920 				   u16 val)
1921 {
1922 	return __phy_modify(phydev, regnum, val, 0);
1923 }
1924 
1925 /**
1926  * phy_set_bits - Convenience function for setting bits in a PHY register
1927  * @phydev: the phy_device struct
1928  * @regnum: register number to write
1929  * @val: bits to set
1930  */
1931 static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1932 {
1933 	return phy_modify(phydev, regnum, 0, val);
1934 }
1935 
1936 /**
1937  * phy_clear_bits - Convenience function for clearing bits in a PHY register
1938  * @phydev: the phy_device struct
1939  * @regnum: register number to write
1940  * @val: bits to clear
1941  */
1942 static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val)
1943 {
1944 	return phy_modify(phydev, regnum, val, 0);
1945 }
1946 
1947 /**
1948  * __phy_set_bits_mmd - Convenience function for setting bits in a register
1949  * on MMD
1950  * @phydev: the phy_device struct
1951  * @devad: the MMD containing register to modify
1952  * @regnum: register number to modify
1953  * @val: bits to set
1954  *
1955  * The caller must have taken the MDIO bus lock.
1956  */
1957 static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
1958 		u32 regnum, u16 val)
1959 {
1960 	return __phy_modify_mmd(phydev, devad, regnum, 0, val);
1961 }
1962 
1963 /**
1964  * __phy_clear_bits_mmd - Convenience function for clearing bits in a register
1965  * on MMD
1966  * @phydev: the phy_device struct
1967  * @devad: the MMD containing register to modify
1968  * @regnum: register number to modify
1969  * @val: bits to clear
1970  *
1971  * The caller must have taken the MDIO bus lock.
1972  */
1973 static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1974 		u32 regnum, u16 val)
1975 {
1976 	return __phy_modify_mmd(phydev, devad, regnum, val, 0);
1977 }
1978 
1979 /**
1980  * phy_set_bits_mmd - Convenience function for setting bits in a register
1981  * on MMD
1982  * @phydev: the phy_device struct
1983  * @devad: the MMD containing register to modify
1984  * @regnum: register number to modify
1985  * @val: bits to set
1986  */
1987 static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
1988 		u32 regnum, u16 val)
1989 {
1990 	return phy_modify_mmd(phydev, devad, regnum, 0, val);
1991 }
1992 
1993 /**
1994  * phy_clear_bits_mmd - Convenience function for clearing bits in a register
1995  * on MMD
1996  * @phydev: the phy_device struct
1997  * @devad: the MMD containing register to modify
1998  * @regnum: register number to modify
1999  * @val: bits to clear
2000  */
2001 static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
2002 		u32 regnum, u16 val)
2003 {
2004 	return phy_modify_mmd(phydev, devad, regnum, val, 0);
2005 }
2006 
2007 /**
2008  * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
2009  * @phydev: the phy_device struct
2010  *
2011  * NOTE: must be kept in sync with addition/removal of PHY_POLL and
2012  * PHY_MAC_INTERRUPT
2013  */
2014 static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
2015 {
2016 	return phydev->irq != PHY_POLL && phydev->irq != PHY_MAC_INTERRUPT;
2017 }
2018 
2019 /**
2020  * phy_polling_mode - Convenience function for testing whether polling is
2021  * used to detect PHY status changes
2022  * @phydev: the phy_device struct
2023  */
2024 static inline bool phy_polling_mode(struct phy_device *phydev)
2025 {
2026 	if (phydev->state == PHY_CABLETEST)
2027 		if (phydev->drv->flags & PHY_POLL_CABLE_TEST)
2028 			return true;
2029 
2030 	if (phydev->drv->update_stats)
2031 		return true;
2032 
2033 	return phydev->irq == PHY_POLL;
2034 }
2035 
2036 /**
2037  * phy_has_hwtstamp - Tests whether a PHY time stamp configuration.
2038  * @phydev: the phy_device struct
2039  */
2040 static inline bool phy_has_hwtstamp(struct phy_device *phydev)
2041 {
2042 	return phydev && phydev->mii_ts && phydev->mii_ts->hwtstamp_set;
2043 }
2044 
2045 /**
2046  * phy_has_rxtstamp - Tests whether a PHY supports receive time stamping.
2047  * @phydev: the phy_device struct
2048  */
2049 static inline bool phy_has_rxtstamp(struct phy_device *phydev)
2050 {
2051 	return phydev && phydev->mii_ts && phydev->mii_ts->rxtstamp;
2052 }
2053 
2054 /**
2055  * phy_has_tsinfo - Tests whether a PHY reports time stamping and/or
2056  * PTP hardware clock capabilities.
2057  * @phydev: the phy_device struct
2058  */
2059 static inline bool phy_has_tsinfo(struct phy_device *phydev)
2060 {
2061 	return phydev && phydev->mii_ts && phydev->mii_ts->ts_info;
2062 }
2063 
2064 /**
2065  * phy_has_txtstamp - Tests whether a PHY supports transmit time stamping.
2066  * @phydev: the phy_device struct
2067  */
2068 static inline bool phy_has_txtstamp(struct phy_device *phydev)
2069 {
2070 	return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp;
2071 }
2072 
2073 static inline int phy_hwtstamp(struct phy_device *phydev,
2074 			       struct kernel_hwtstamp_config *cfg,
2075 			       struct netlink_ext_ack *extack)
2076 {
2077 	return phydev->mii_ts->hwtstamp_set(phydev->mii_ts, cfg, extack);
2078 }
2079 
2080 static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb,
2081 				int type)
2082 {
2083 	return phydev->mii_ts->rxtstamp(phydev->mii_ts, skb, type);
2084 }
2085 
2086 static inline int phy_ts_info(struct phy_device *phydev,
2087 			      struct kernel_ethtool_ts_info *tsinfo)
2088 {
2089 	return phydev->mii_ts->ts_info(phydev->mii_ts, tsinfo);
2090 }
2091 
2092 static inline void phy_txtstamp(struct phy_device *phydev, struct sk_buff *skb,
2093 				int type)
2094 {
2095 	phydev->mii_ts->txtstamp(phydev->mii_ts, skb, type);
2096 }
2097 
2098 /**
2099  * phy_is_default_hwtstamp - Is the PHY hwtstamp the default timestamp
2100  * @phydev: Pointer to phy_device
2101  *
2102  * This is used to get default timestamping device taking into account
2103  * the new API choice, which is selecting the timestamping from MAC by
2104  * default if the phydev does not have default_timestamp flag enabled.
2105  *
2106  * Return: True if phy is the default hw timestamp, false otherwise.
2107  */
2108 static inline bool phy_is_default_hwtstamp(struct phy_device *phydev)
2109 {
2110 	return phy_has_hwtstamp(phydev) && phydev->default_timestamp;
2111 }
2112 
2113 /**
2114  * phy_on_sfp - Convenience function for testing if a PHY is on an SFP module
2115  * @phydev: the phy_device struct
2116  */
2117 static inline bool phy_on_sfp(struct phy_device *phydev)
2118 {
2119 	return phydev->is_on_sfp_module;
2120 }
2121 
2122 /**
2123  * phy_interface_mode_is_rgmii - Convenience function for testing if a
2124  * PHY interface mode is RGMII (all variants)
2125  * @mode: the &phy_interface_t enum
2126  */
2127 static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
2128 {
2129 	return mode >= PHY_INTERFACE_MODE_RGMII &&
2130 		mode <= PHY_INTERFACE_MODE_RGMII_TXID;
2131 };
2132 
2133 /**
2134  * phy_interface_mode_is_8023z() - does the PHY interface mode use 802.3z
2135  *   negotiation
2136  * @mode: one of &enum phy_interface_t
2137  *
2138  * Returns true if the PHY interface mode uses the 16-bit negotiation
2139  * word as defined in 802.3z. (See 802.3-2015 37.2.1 Config_Reg encoding)
2140  */
2141 static inline bool phy_interface_mode_is_8023z(phy_interface_t mode)
2142 {
2143 	return mode == PHY_INTERFACE_MODE_1000BASEX ||
2144 	       mode == PHY_INTERFACE_MODE_2500BASEX;
2145 }
2146 
2147 /**
2148  * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
2149  * is RGMII (all variants)
2150  * @phydev: the phy_device struct
2151  */
2152 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
2153 {
2154 	return phy_interface_mode_is_rgmii(phydev->interface);
2155 };
2156 
2157 /**
2158  * phy_is_pseudo_fixed_link - Convenience function for testing if this
2159  * PHY is the CPU port facing side of an Ethernet switch, or similar.
2160  * @phydev: the phy_device struct
2161  */
2162 static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
2163 {
2164 	return phydev->is_pseudo_fixed_link;
2165 }
2166 
2167 phy_interface_t phy_fix_phy_mode_for_mac_delays(phy_interface_t interface,
2168 						bool mac_txid, bool mac_rxid);
2169 
2170 int phy_save_page(struct phy_device *phydev);
2171 int phy_select_page(struct phy_device *phydev, int page);
2172 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
2173 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
2174 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
2175 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
2176 			     u16 mask, u16 set);
2177 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
2178 		     u16 mask, u16 set);
2179 
2180 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
2181 				     bool is_c45,
2182 				     struct phy_c45_device_ids *c45_ids);
2183 int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id);
2184 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode);
2185 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode);
2186 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode);
2187 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
2188 int phy_device_register(struct phy_device *phy);
2189 void phy_device_free(struct phy_device *phydev);
2190 void phy_device_remove(struct phy_device *phydev);
2191 int phy_get_c45_ids(struct phy_device *phydev);
2192 int phy_init_hw(struct phy_device *phydev);
2193 int phy_suspend(struct phy_device *phydev);
2194 int phy_resume(struct phy_device *phydev);
2195 int __phy_resume(struct phy_device *phydev);
2196 int phy_loopback(struct phy_device *phydev, bool enable, int speed);
2197 struct phy_device *phy_find_next(struct mii_bus *bus, struct phy_device *pos);
2198 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
2199 		      u32 flags, phy_interface_t interface);
2200 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
2201 		       void (*handler)(struct net_device *),
2202 		       phy_interface_t interface);
2203 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
2204 			       void (*handler)(struct net_device *),
2205 			       phy_interface_t interface);
2206 void phy_disconnect(struct phy_device *phydev);
2207 void phy_detach(struct phy_device *phydev);
2208 void phy_start(struct phy_device *phydev);
2209 void phy_stop(struct phy_device *phydev);
2210 int phy_config_aneg(struct phy_device *phydev);
2211 int _phy_start_aneg(struct phy_device *phydev);
2212 int phy_start_aneg(struct phy_device *phydev);
2213 int phy_aneg_done(struct phy_device *phydev);
2214 unsigned int phy_inband_caps(struct phy_device *phydev,
2215 			     phy_interface_t interface);
2216 int phy_config_inband(struct phy_device *phydev, unsigned int modes);
2217 int phy_speed_down(struct phy_device *phydev, bool sync);
2218 int phy_speed_up(struct phy_device *phydev);
2219 bool phy_check_valid(int speed, int duplex, unsigned long *features);
2220 
2221 int phy_restart_aneg(struct phy_device *phydev);
2222 int phy_reset_after_clk_enable(struct phy_device *phydev);
2223 
2224 static inline struct phy_device *phy_find_first(struct mii_bus *bus)
2225 {
2226 	return phy_find_next(bus, NULL);
2227 }
2228 
2229 #define mdiobus_for_each_phy(_bus, _phydev)		\
2230 	for (_phydev = phy_find_first(_bus); _phydev;	\
2231 	     _phydev = phy_find_next(_bus, _phydev))
2232 
2233 #if IS_ENABLED(CONFIG_PHYLIB)
2234 int phy_start_cable_test(struct phy_device *phydev,
2235 			 struct netlink_ext_ack *extack);
2236 int phy_start_cable_test_tdr(struct phy_device *phydev,
2237 			     struct netlink_ext_ack *extack,
2238 			     const struct phy_tdr_config *config);
2239 #else
2240 static inline
2241 int phy_start_cable_test(struct phy_device *phydev,
2242 			 struct netlink_ext_ack *extack)
2243 {
2244 	NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
2245 	return -EOPNOTSUPP;
2246 }
2247 static inline
2248 int phy_start_cable_test_tdr(struct phy_device *phydev,
2249 			     struct netlink_ext_ack *extack,
2250 			     const struct phy_tdr_config *config)
2251 {
2252 	NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
2253 	return -EOPNOTSUPP;
2254 }
2255 #endif
2256 
2257 static inline void phy_device_reset(struct phy_device *phydev, int value)
2258 {
2259 	mdio_device_reset(&phydev->mdio, value);
2260 }
2261 
2262 static inline bool phy_device_has_reset(struct phy_device *phydev)
2263 {
2264 	return mdiodev_has_reset(&phydev->mdio);
2265 }
2266 
2267 #define phydev_err(_phydev, format, args...)	\
2268 	dev_err(&_phydev->mdio.dev, format, ##args)
2269 
2270 #define phydev_err_probe(_phydev, err, format, args...)	\
2271 	dev_err_probe(&_phydev->mdio.dev, err, format, ##args)
2272 
2273 #define phydev_info(_phydev, format, args...)	\
2274 	dev_info(&_phydev->mdio.dev, format, ##args)
2275 
2276 #define phydev_warn(_phydev, format, args...)	\
2277 	dev_warn(&_phydev->mdio.dev, format, ##args)
2278 
2279 #define phydev_dbg(_phydev, format, args...)	\
2280 	dev_dbg(&_phydev->mdio.dev, format, ##args)
2281 
2282 static inline const char *phydev_name(const struct phy_device *phydev)
2283 {
2284 	return dev_name(&phydev->mdio.dev);
2285 }
2286 
2287 static inline void phy_lock_mdio_bus(struct phy_device *phydev)
2288 {
2289 	mutex_lock(&phydev->mdio.bus->mdio_lock);
2290 }
2291 
2292 static inline void phy_unlock_mdio_bus(struct phy_device *phydev)
2293 {
2294 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
2295 }
2296 
2297 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
2298 	__printf(2, 3);
2299 char *phy_attached_info_irq(struct phy_device *phydev)
2300 	__malloc;
2301 void phy_attached_info(struct phy_device *phydev);
2302 
2303 int genphy_match_phy_device(struct phy_device *phydev,
2304 			    const struct phy_driver *phydrv);
2305 
2306 /* Clause 22 PHY */
2307 int genphy_read_abilities(struct phy_device *phydev);
2308 int genphy_setup_forced(struct phy_device *phydev);
2309 int genphy_restart_aneg(struct phy_device *phydev);
2310 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart);
2311 int __genphy_config_aneg(struct phy_device *phydev, bool changed);
2312 int genphy_aneg_done(struct phy_device *phydev);
2313 int genphy_update_link(struct phy_device *phydev);
2314 int genphy_read_lpa(struct phy_device *phydev);
2315 int genphy_read_status_fixed(struct phy_device *phydev);
2316 int genphy_read_status(struct phy_device *phydev);
2317 int genphy_read_master_slave(struct phy_device *phydev);
2318 int genphy_suspend(struct phy_device *phydev);
2319 int genphy_resume(struct phy_device *phydev);
2320 int genphy_loopback(struct phy_device *phydev, bool enable, int speed);
2321 int genphy_soft_reset(struct phy_device *phydev);
2322 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev);
2323 
2324 static inline int genphy_config_aneg(struct phy_device *phydev)
2325 {
2326 	return __genphy_config_aneg(phydev, false);
2327 }
2328 
2329 static inline int genphy_no_config_intr(struct phy_device *phydev)
2330 {
2331 	return 0;
2332 }
2333 int genphy_read_mmd_c45(struct phy_device *phydev, int devnum, u16 regnum);
2334 int genphy_write_mmd_c45(struct phy_device *phydev, int devnum, u16 regnum,
2335 			 u16 val);
2336 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
2337 				u16 regnum);
2338 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2339 				 u16 regnum, u16 val);
2340 
2341 /* Clause 37 */
2342 int genphy_c37_config_aneg(struct phy_device *phydev);
2343 int genphy_c37_read_status(struct phy_device *phydev, bool *changed);
2344 
2345 /* Clause 45 PHY */
2346 int genphy_c45_restart_aneg(struct phy_device *phydev);
2347 int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);
2348 int genphy_c45_pma_soft_reset(struct phy_device *phydev);
2349 int genphy_c45_aneg_done(struct phy_device *phydev);
2350 int genphy_c45_read_link(struct phy_device *phydev);
2351 int genphy_c45_read_lpa(struct phy_device *phydev);
2352 int genphy_c45_read_pma(struct phy_device *phydev);
2353 int genphy_c45_pma_setup_forced(struct phy_device *phydev);
2354 int genphy_c45_pma_baset1_setup_master_slave(struct phy_device *phydev);
2355 int genphy_c45_an_config_aneg(struct phy_device *phydev);
2356 int genphy_c45_an_disable_aneg(struct phy_device *phydev);
2357 int genphy_c45_read_mdix(struct phy_device *phydev);
2358 int genphy_c45_pma_read_abilities(struct phy_device *phydev);
2359 int genphy_c45_pma_read_ext_abilities(struct phy_device *phydev);
2360 int genphy_c45_pma_baset1_read_abilities(struct phy_device *phydev);
2361 int genphy_c45_read_eee_abilities(struct phy_device *phydev);
2362 int genphy_c45_pma_baset1_read_master_slave(struct phy_device *phydev);
2363 int genphy_c45_read_status(struct phy_device *phydev);
2364 int genphy_c45_baset1_read_status(struct phy_device *phydev);
2365 int genphy_c45_config_aneg(struct phy_device *phydev);
2366 int genphy_c45_loopback(struct phy_device *phydev, bool enable, int speed);
2367 int genphy_c45_pma_resume(struct phy_device *phydev);
2368 int genphy_c45_pma_suspend(struct phy_device *phydev);
2369 int genphy_c45_fast_retrain(struct phy_device *phydev, bool enable);
2370 int genphy_c45_plca_get_cfg(struct phy_device *phydev,
2371 			    struct phy_plca_cfg *plca_cfg);
2372 int genphy_c45_plca_set_cfg(struct phy_device *phydev,
2373 			    const struct phy_plca_cfg *plca_cfg);
2374 int genphy_c45_plca_get_status(struct phy_device *phydev,
2375 			       struct phy_plca_status *plca_st);
2376 int genphy_c45_eee_is_active(struct phy_device *phydev, unsigned long *lp);
2377 int genphy_c45_ethtool_get_eee(struct phy_device *phydev,
2378 			       struct ethtool_keee *data);
2379 int genphy_c45_ethtool_set_eee(struct phy_device *phydev,
2380 			       struct ethtool_keee *data);
2381 int genphy_c45_an_config_eee_aneg(struct phy_device *phydev);
2382 int genphy_c45_oatc14_cable_test_start(struct phy_device *phydev);
2383 int genphy_c45_oatc14_cable_test_get_status(struct phy_device *phydev,
2384 					    bool *finished);
2385 int genphy_c45_oatc14_get_sqi_max(struct phy_device *phydev);
2386 int genphy_c45_oatc14_get_sqi(struct phy_device *phydev);
2387 
2388 /* The gen10g_* functions are the old Clause 45 stub */
2389 int gen10g_config_aneg(struct phy_device *phydev);
2390 
2391 static inline int phy_read_status(struct phy_device *phydev)
2392 {
2393 	if (!phydev->drv)
2394 		return -EIO;
2395 
2396 	if (phydev->drv->read_status)
2397 		return phydev->drv->read_status(phydev);
2398 	else
2399 		return genphy_read_status(phydev);
2400 }
2401 
2402 void phy_drivers_unregister(struct phy_driver *drv, int n);
2403 int phy_drivers_register(struct phy_driver *new_driver, int n,
2404 			 struct module *owner);
2405 void phy_error(struct phy_device *phydev);
2406 void phy_state_machine(struct work_struct *work);
2407 void phy_trigger_machine(struct phy_device *phydev);
2408 void phy_mac_interrupt(struct phy_device *phydev);
2409 void phy_start_machine(struct phy_device *phydev);
2410 void phy_stop_machine(struct phy_device *phydev);
2411 
2412 void phy_ethtool_ksettings_get(struct phy_device *phydev,
2413 			       struct ethtool_link_ksettings *cmd);
2414 int phy_ethtool_ksettings_set(struct phy_device *phydev,
2415 			      const struct ethtool_link_ksettings *cmd);
2416 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
2417 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
2418 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd);
2419 int phy_disable_interrupts(struct phy_device *phydev);
2420 void phy_request_interrupt(struct phy_device *phydev);
2421 void phy_free_interrupt(struct phy_device *phydev);
2422 void phy_print_status(struct phy_device *phydev);
2423 int phy_get_rate_matching(struct phy_device *phydev,
2424 			    phy_interface_t iface);
2425 void phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
2426 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
2427 void phy_advertise_supported(struct phy_device *phydev);
2428 void phy_advertise_eee_all(struct phy_device *phydev);
2429 void phy_support_sym_pause(struct phy_device *phydev);
2430 void phy_support_asym_pause(struct phy_device *phydev);
2431 void phy_support_eee(struct phy_device *phydev);
2432 void phy_disable_eee(struct phy_device *phydev);
2433 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2434 		       bool autoneg);
2435 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
2436 bool phy_validate_pause(struct phy_device *phydev,
2437 			struct ethtool_pauseparam *pp);
2438 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause);
2439 
2440 s32 phy_get_internal_delay(struct phy_device *phydev, const int *delay_values,
2441 			   int size, bool is_rx);
2442 
2443 int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev,
2444 			      enum ethtool_link_mode_bit_indices linkmode,
2445 			      u32 *val);
2446 
2447 int phy_get_mac_termination(struct phy_device *phydev, struct device *dev,
2448 			    u32 *val);
2449 
2450 void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv,
2451 		       bool *tx_pause, bool *rx_pause);
2452 
2453 void __init phy_register_fixup_for_id(const char *bus_id,
2454 				      int (*run)(struct phy_device *));
2455 void __init phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
2456 				       int (*run)(struct phy_device *));
2457 
2458 int phy_eee_tx_clock_stop_capable(struct phy_device *phydev);
2459 int phy_eee_rx_clock_stop(struct phy_device *phydev, bool clk_stop_enable);
2460 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
2461 int phy_get_eee_err(struct phy_device *phydev);
2462 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_keee *data);
2463 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_keee *data);
2464 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
2465 void phy_ethtool_get_wol(struct phy_device *phydev,
2466 			 struct ethtool_wolinfo *wol);
2467 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
2468 				   struct ethtool_link_ksettings *cmd);
2469 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
2470 				   const struct ethtool_link_ksettings *cmd);
2471 int phy_ethtool_nway_reset(struct net_device *ndev);
2472 
2473 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data);
2474 int phy_ethtool_get_sset_count(struct phy_device *phydev);
2475 int phy_ethtool_get_stats(struct phy_device *phydev,
2476 			  struct ethtool_stats *stats, u64 *data);
2477 
2478 void __phy_ethtool_get_phy_stats(struct phy_device *phydev,
2479 			 struct ethtool_eth_phy_stats *phy_stats,
2480 			 struct ethtool_phy_stats *phydev_stats);
2481 void __phy_ethtool_get_link_ext_stats(struct phy_device *phydev,
2482 				      struct ethtool_link_ext_stats *link_stats);
2483 
2484 int phy_ethtool_get_plca_cfg(struct phy_device *phydev,
2485 			     struct phy_plca_cfg *plca_cfg);
2486 int phy_ethtool_set_plca_cfg(struct phy_device *phydev,
2487 			     const struct phy_plca_cfg *plca_cfg,
2488 			     struct netlink_ext_ack *extack);
2489 int phy_ethtool_get_plca_status(struct phy_device *phydev,
2490 				struct phy_plca_status *plca_st);
2491 
2492 int __phy_hwtstamp_get(struct phy_device *phydev,
2493 		       struct kernel_hwtstamp_config *config);
2494 int __phy_hwtstamp_set(struct phy_device *phydev,
2495 		       struct kernel_hwtstamp_config *config,
2496 		       struct netlink_ext_ack *extack);
2497 
2498 struct phy_port *phy_get_sfp_port(struct phy_device *phydev);
2499 
2500 void phy_set_upstream_port(struct phy_device *phydev, struct phy_port *port);
2501 
2502 /**
2503  * phy_module_driver() - Helper macro for registering PHY drivers
2504  * @__phy_drivers: array of PHY drivers to register
2505  * @__count: Numbers of members in array
2506  *
2507  * Helper macro for PHY drivers which do not do anything special in module
2508  * init/exit. Each module may only use this macro once, and calling it
2509  * replaces module_init() and module_exit().
2510  */
2511 #define phy_module_driver(__phy_drivers, __count)			\
2512 static int __init phy_module_init(void)					\
2513 {									\
2514 	return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
2515 }									\
2516 module_init(phy_module_init);						\
2517 static void __exit phy_module_exit(void)				\
2518 {									\
2519 	phy_drivers_unregister(__phy_drivers, __count);			\
2520 }									\
2521 module_exit(phy_module_exit)
2522 
2523 #define module_phy_driver(__phy_drivers)				\
2524 	phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
2525 
2526 #endif /* __PHY_H */
2527