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