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