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