xref: /linux/include/linux/mdio.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * linux/mdio.h: definitions for MDIO (clause 45) transceivers
4  * Copyright 2006-2009 Solarflare Communications Inc.
5  */
6 #ifndef __LINUX_MDIO_H__
7 #define __LINUX_MDIO_H__
8 
9 #include <uapi/linux/mdio.h>
10 #include <linux/bitfield.h>
11 #include <linux/mod_devicetable.h>
12 
13 struct gpio_desc;
14 struct mii_bus;
15 struct reset_control;
16 
17 /* Multiple levels of nesting are possible. However typically this is
18  * limited to nested DSA like layer, a MUX layer, and the normal
19  * user. Instead of trying to handle the general case, just define
20  * these cases.
21  */
22 enum mdio_mutex_lock_class {
23 	MDIO_MUTEX_NORMAL,
24 	MDIO_MUTEX_MUX,
25 	MDIO_MUTEX_NESTED,
26 };
27 
28 struct mdio_device {
29 	struct device dev;
30 
31 	struct mii_bus *bus;
32 	char modalias[MDIO_NAME_SIZE];
33 
34 	int (*bus_match)(struct device *dev, const struct device_driver *drv);
35 	void (*device_free)(struct mdio_device *mdiodev);
36 	void (*device_remove)(struct mdio_device *mdiodev);
37 
38 	/* Bus address of the MDIO device (0-31) */
39 	int addr;
40 	int flags;
41 	int reset_state;
42 	struct gpio_desc *reset_gpio;
43 	struct reset_control *reset_ctrl;
44 	unsigned int reset_assert_delay;
45 	unsigned int reset_deassert_delay;
46 };
47 
to_mdio_device(const struct device * dev)48 static inline struct mdio_device *to_mdio_device(const struct device *dev)
49 {
50 	return container_of(dev, struct mdio_device, dev);
51 }
52 
53 /* struct mdio_driver_common: Common to all MDIO drivers */
54 struct mdio_driver_common {
55 	struct device_driver driver;
56 	int flags;
57 };
58 #define MDIO_DEVICE_FLAG_PHY		1
59 
60 #define to_mdio_common_driver(__drv_c)	container_of_const(__drv_c, struct mdio_driver_common,	\
61 							   driver)
62 
63 /* struct mdio_driver: Generic MDIO driver */
64 struct mdio_driver {
65 	struct mdio_driver_common mdiodrv;
66 
67 	/*
68 	 * Called during discovery.  Used to set
69 	 * up device-specific structures, if any
70 	 */
71 	int (*probe)(struct mdio_device *mdiodev);
72 
73 	/* Clears up any memory if needed */
74 	void (*remove)(struct mdio_device *mdiodev);
75 
76 	/* Quiesces the device on system shutdown, turns off interrupts etc */
77 	void (*shutdown)(struct mdio_device *mdiodev);
78 };
79 
80 #define to_mdio_driver(__drv_m)	container_of_const(to_mdio_common_driver(__drv_m),	\
81 						   struct mdio_driver, mdiodrv)
82 
83 /* device driver data */
mdiodev_set_drvdata(struct mdio_device * mdio,void * data)84 static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data)
85 {
86 	dev_set_drvdata(&mdio->dev, data);
87 }
88 
mdiodev_get_drvdata(struct mdio_device * mdio)89 static inline void *mdiodev_get_drvdata(struct mdio_device *mdio)
90 {
91 	return dev_get_drvdata(&mdio->dev);
92 }
93 
94 void mdio_device_free(struct mdio_device *mdiodev);
95 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
96 int mdio_device_register(struct mdio_device *mdiodev);
97 void mdio_device_remove(struct mdio_device *mdiodev);
98 void mdio_device_reset(struct mdio_device *mdiodev, int value);
99 int mdio_driver_register(struct mdio_driver *drv);
100 void mdio_driver_unregister(struct mdio_driver *drv);
101 int mdio_device_bus_match(struct device *dev, const struct device_driver *drv);
102 
mdio_device_get(struct mdio_device * mdiodev)103 static inline void mdio_device_get(struct mdio_device *mdiodev)
104 {
105 	get_device(&mdiodev->dev);
106 }
107 
mdio_device_put(struct mdio_device * mdiodev)108 static inline void mdio_device_put(struct mdio_device *mdiodev)
109 {
110 	mdio_device_free(mdiodev);
111 }
112 
mdio_phy_id_is_c45(int phy_id)113 static inline bool mdio_phy_id_is_c45(int phy_id)
114 {
115 	return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
116 }
117 
mdio_phy_id_prtad(int phy_id)118 static inline __u16 mdio_phy_id_prtad(int phy_id)
119 {
120 	return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
121 }
122 
mdio_phy_id_devad(int phy_id)123 static inline __u16 mdio_phy_id_devad(int phy_id)
124 {
125 	return phy_id & MDIO_PHY_ID_DEVAD;
126 }
127 
128 /**
129  * struct mdio_if_info - Ethernet controller MDIO interface
130  * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
131  * @mmds: Mask of MMDs expected to be present in the PHY.  This must be
132  *	non-zero unless @prtad = %MDIO_PRTAD_NONE.
133  * @mode_support: MDIO modes supported.  If %MDIO_SUPPORTS_C22 is set then
134  *	MII register access will be passed through with @devad =
135  *	%MDIO_DEVAD_NONE.  If %MDIO_EMULATE_C22 is set then access to
136  *	commonly used clause 22 registers will be translated into
137  *	clause 45 registers.
138  * @dev: Net device structure
139  * @mdio_read: Register read function; returns value or negative error code
140  * @mdio_write: Register write function; returns 0 or negative error code
141  */
142 struct mdio_if_info {
143 	int prtad;
144 	u32 mmds;
145 	unsigned mode_support;
146 
147 	struct net_device *dev;
148 	int (*mdio_read)(struct net_device *dev, int prtad, int devad,
149 			 u16 addr);
150 	int (*mdio_write)(struct net_device *dev, int prtad, int devad,
151 			  u16 addr, u16 val);
152 };
153 
154 #define MDIO_PRTAD_NONE			(-1)
155 #define MDIO_DEVAD_NONE			(-1)
156 #define MDIO_SUPPORTS_C22		1
157 #define MDIO_SUPPORTS_C45		2
158 #define MDIO_EMULATE_C22		4
159 
160 struct ethtool_cmd;
161 struct ethtool_pauseparam;
162 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
163 extern int mdio_set_flag(const struct mdio_if_info *mdio,
164 			 int prtad, int devad, u16 addr, int mask,
165 			 bool sense);
166 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
167 extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
168 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
169 				      struct ethtool_cmd *ecmd,
170 				      u32 npage_adv, u32 npage_lpa);
171 extern void
172 mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
173 				   struct ethtool_link_ksettings *cmd,
174 				   u32 npage_adv, u32 npage_lpa);
175 
176 /**
177  * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
178  * @mdio: MDIO interface
179  * @ecmd: Ethtool request structure
180  *
181  * Since the CSRs for auto-negotiation using next pages are not fully
182  * standardised, this function does not attempt to decode them.  Use
183  * mdio45_ethtool_gset_npage() to specify advertisement bits from next
184  * pages.
185  */
mdio45_ethtool_gset(const struct mdio_if_info * mdio,struct ethtool_cmd * ecmd)186 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
187 				       struct ethtool_cmd *ecmd)
188 {
189 	mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
190 }
191 
192 /**
193  * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
194  * @mdio: MDIO interface
195  * @cmd: Ethtool request structure
196  *
197  * Since the CSRs for auto-negotiation using next pages are not fully
198  * standardised, this function does not attempt to decode them.  Use
199  * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
200  * from next pages.
201  */
202 static inline void
mdio45_ethtool_ksettings_get(const struct mdio_if_info * mdio,struct ethtool_link_ksettings * cmd)203 mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
204 			     struct ethtool_link_ksettings *cmd)
205 {
206 	mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
207 }
208 
209 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
210 			  struct mii_ioctl_data *mii_data, int cmd);
211 
212 /**
213  * mmd_eee_cap_to_ethtool_sup_t
214  * @eee_cap: value of the MMD EEE Capability register
215  *
216  * A small helper function that translates MMD EEE Capability (3.20) bits
217  * to ethtool supported settings.
218  */
mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)219 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
220 {
221 	u32 supported = 0;
222 
223 	if (eee_cap & MDIO_EEE_100TX)
224 		supported |= SUPPORTED_100baseT_Full;
225 	if (eee_cap & MDIO_EEE_1000T)
226 		supported |= SUPPORTED_1000baseT_Full;
227 	if (eee_cap & MDIO_EEE_10GT)
228 		supported |= SUPPORTED_10000baseT_Full;
229 	if (eee_cap & MDIO_EEE_1000KX)
230 		supported |= SUPPORTED_1000baseKX_Full;
231 	if (eee_cap & MDIO_EEE_10GKX4)
232 		supported |= SUPPORTED_10000baseKX4_Full;
233 	if (eee_cap & MDIO_EEE_10GKR)
234 		supported |= SUPPORTED_10000baseKR_Full;
235 
236 	return supported;
237 }
238 
239 /**
240  * mmd_eee_adv_to_ethtool_adv_t
241  * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
242  *
243  * A small helper function that translates the MMD EEE Advertisment (7.60)
244  * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
245  * settings.
246  */
mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)247 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
248 {
249 	u32 adv = 0;
250 
251 	if (eee_adv & MDIO_EEE_100TX)
252 		adv |= ADVERTISED_100baseT_Full;
253 	if (eee_adv & MDIO_EEE_1000T)
254 		adv |= ADVERTISED_1000baseT_Full;
255 	if (eee_adv & MDIO_EEE_10GT)
256 		adv |= ADVERTISED_10000baseT_Full;
257 	if (eee_adv & MDIO_EEE_1000KX)
258 		adv |= ADVERTISED_1000baseKX_Full;
259 	if (eee_adv & MDIO_EEE_10GKX4)
260 		adv |= ADVERTISED_10000baseKX4_Full;
261 	if (eee_adv & MDIO_EEE_10GKR)
262 		adv |= ADVERTISED_10000baseKR_Full;
263 
264 	return adv;
265 }
266 
267 /**
268  * ethtool_adv_to_mmd_eee_adv_t
269  * @adv: the ethtool advertisement settings
270  *
271  * A small helper function that translates ethtool advertisement settings
272  * to EEE advertisements for the MMD EEE Advertisement (7.60) and
273  * MMD EEE Link Partner Ability (7.61) registers.
274  */
ethtool_adv_to_mmd_eee_adv_t(u32 adv)275 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
276 {
277 	u16 reg = 0;
278 
279 	if (adv & ADVERTISED_100baseT_Full)
280 		reg |= MDIO_EEE_100TX;
281 	if (adv & ADVERTISED_1000baseT_Full)
282 		reg |= MDIO_EEE_1000T;
283 	if (adv & ADVERTISED_10000baseT_Full)
284 		reg |= MDIO_EEE_10GT;
285 	if (adv & ADVERTISED_1000baseKX_Full)
286 		reg |= MDIO_EEE_1000KX;
287 	if (adv & ADVERTISED_10000baseKX4_Full)
288 		reg |= MDIO_EEE_10GKX4;
289 	if (adv & ADVERTISED_10000baseKR_Full)
290 		reg |= MDIO_EEE_10GKR;
291 
292 	return reg;
293 }
294 
295 /**
296  * linkmode_adv_to_mii_10gbt_adv_t
297  * @advertising: the linkmode advertisement settings
298  *
299  * A small helper function that translates linkmode advertisement
300  * settings to phy autonegotiation advertisements for the C45
301  * 10GBASE-T AN CONTROL (7.32) register.
302  */
linkmode_adv_to_mii_10gbt_adv_t(unsigned long * advertising)303 static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising)
304 {
305 	u32 result = 0;
306 
307 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
308 			      advertising))
309 		result |= MDIO_AN_10GBT_CTRL_ADV2_5G;
310 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
311 			      advertising))
312 		result |= MDIO_AN_10GBT_CTRL_ADV5G;
313 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
314 			      advertising))
315 		result |= MDIO_AN_10GBT_CTRL_ADV10G;
316 
317 	return result;
318 }
319 
320 /**
321  * mii_10gbt_stat_mod_linkmode_lpa_t
322  * @advertising: target the linkmode advertisement settings
323  * @lpa: value of the C45 10GBASE-T AN STATUS register
324  *
325  * A small helper function that translates C45 10GBASE-T AN STATUS register bits
326  * to linkmode advertisement settings. Other bits in advertising aren't changed.
327  */
mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long * advertising,u32 lpa)328 static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising,
329 						     u32 lpa)
330 {
331 	linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
332 			 advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G);
333 	linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
334 			 advertising, lpa & MDIO_AN_10GBT_STAT_LP5G);
335 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
336 			 advertising, lpa & MDIO_AN_10GBT_STAT_LP10G);
337 }
338 
339 /**
340  * mii_t1_adv_l_mod_linkmode_t
341  * @advertising: target the linkmode advertisement settings
342  * @lpa: value of the BASE-T1 Autonegotiation Advertisement [15:0] Register
343  *
344  * A small helper function that translates BASE-T1 Autonegotiation
345  * Advertisement [15:0] Register bits to linkmode advertisement settings.
346  * Other bits in advertising aren't changed.
347  */
mii_t1_adv_l_mod_linkmode_t(unsigned long * advertising,u32 lpa)348 static inline void mii_t1_adv_l_mod_linkmode_t(unsigned long *advertising, u32 lpa)
349 {
350 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising,
351 			 lpa & MDIO_AN_T1_ADV_L_PAUSE_CAP);
352 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising,
353 			 lpa & MDIO_AN_T1_ADV_L_PAUSE_ASYM);
354 }
355 
356 /**
357  * mii_t1_adv_m_mod_linkmode_t
358  * @advertising: target the linkmode advertisement settings
359  * @lpa: value of the BASE-T1 Autonegotiation Advertisement [31:16] Register
360  *
361  * A small helper function that translates BASE-T1 Autonegotiation
362  * Advertisement [31:16] Register bits to linkmode advertisement settings.
363  * Other bits in advertising aren't changed.
364  */
mii_t1_adv_m_mod_linkmode_t(unsigned long * advertising,u32 lpa)365 static inline void mii_t1_adv_m_mod_linkmode_t(unsigned long *advertising, u32 lpa)
366 {
367 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT,
368 			 advertising, lpa & MDIO_AN_T1_ADV_M_B10L);
369 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
370 			 advertising, lpa & MDIO_AN_T1_ADV_M_100BT1);
371 	linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT,
372 			 advertising, lpa & MDIO_AN_T1_ADV_M_1000BT1);
373 }
374 
375 /**
376  * linkmode_adv_to_mii_t1_adv_l_t
377  * @advertising: the linkmode advertisement settings
378  *
379  * A small helper function that translates linkmode advertisement
380  * settings to phy autonegotiation advertisements for the
381  * BASE-T1 Autonegotiation Advertisement [15:0] Register.
382  */
linkmode_adv_to_mii_t1_adv_l_t(unsigned long * advertising)383 static inline u32 linkmode_adv_to_mii_t1_adv_l_t(unsigned long *advertising)
384 {
385 	u32 result = 0;
386 
387 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising))
388 		result |= MDIO_AN_T1_ADV_L_PAUSE_CAP;
389 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising))
390 		result |= MDIO_AN_T1_ADV_L_PAUSE_ASYM;
391 
392 	return result;
393 }
394 
395 /**
396  * linkmode_adv_to_mii_t1_adv_m_t
397  * @advertising: the linkmode advertisement settings
398  *
399  * A small helper function that translates linkmode advertisement
400  * settings to phy autonegotiation advertisements for the
401  * BASE-T1 Autonegotiation Advertisement [31:16] Register.
402  */
linkmode_adv_to_mii_t1_adv_m_t(unsigned long * advertising)403 static inline u32 linkmode_adv_to_mii_t1_adv_m_t(unsigned long *advertising)
404 {
405 	u32 result = 0;
406 
407 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, advertising))
408 		result |= MDIO_AN_T1_ADV_M_B10L;
409 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT, advertising))
410 		result |= MDIO_AN_T1_ADV_M_100BT1;
411 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT, advertising))
412 		result |= MDIO_AN_T1_ADV_M_1000BT1;
413 
414 	return result;
415 }
416 
417 /**
418  * mii_eee_cap1_mod_linkmode_t()
419  * @adv: target the linkmode advertisement settings
420  * @val: register value
421  *
422  * A function that translates value of following registers to the linkmode:
423  * IEEE 802.3-2018 45.2.3.10 "EEE control and capability 1" register (3.20)
424  * IEEE 802.3-2018 45.2.7.13 "EEE advertisement 1" register (7.60)
425  * IEEE 802.3-2018 45.2.7.14 "EEE link partner ability 1" register (7.61)
426  */
mii_eee_cap1_mod_linkmode_t(unsigned long * adv,u32 val)427 static inline void mii_eee_cap1_mod_linkmode_t(unsigned long *adv, u32 val)
428 {
429 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
430 			 adv, val & MDIO_EEE_100TX);
431 	linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
432 			 adv, val & MDIO_EEE_1000T);
433 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
434 			 adv, val & MDIO_EEE_10GT);
435 	linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
436 			 adv, val & MDIO_EEE_1000KX);
437 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
438 			 adv, val & MDIO_EEE_10GKX4);
439 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
440 			 adv, val & MDIO_EEE_10GKR);
441 }
442 
443 /**
444  * mii_eee_cap2_mod_linkmode_sup_t()
445  * @adv: target the linkmode settings
446  * @val: register value
447  *
448  * A function that translates value of following registers to the linkmode:
449  * IEEE 802.3-2022 45.2.3.11 "EEE control and capability 2" register (3.21)
450  */
mii_eee_cap2_mod_linkmode_sup_t(unsigned long * adv,u32 val)451 static inline void mii_eee_cap2_mod_linkmode_sup_t(unsigned long *adv, u32 val)
452 {
453 	linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
454 			 adv, val & MDIO_EEE_2_5GT);
455 	linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
456 			 adv, val & MDIO_EEE_5GT);
457 }
458 
459 /**
460  * mii_eee_cap2_mod_linkmode_adv_t()
461  * @adv: target the linkmode advertisement settings
462  * @val: register value
463  *
464  * A function that translates value of following registers to the linkmode:
465  * IEEE 802.3-2022 45.2.7.16 "EEE advertisement 2" register (7.62)
466  * IEEE 802.3-2022 45.2.7.17 "EEE link partner ability 2" register (7.63)
467  * Note: Currently this function is the same as mii_eee_cap2_mod_linkmode_sup_t.
468  *       For certain, not yet supported, modes however the bits differ.
469  *       Therefore create separate functions already.
470  */
mii_eee_cap2_mod_linkmode_adv_t(unsigned long * adv,u32 val)471 static inline void mii_eee_cap2_mod_linkmode_adv_t(unsigned long *adv, u32 val)
472 {
473 	linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
474 			 adv, val & MDIO_EEE_2_5GT);
475 	linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
476 			 adv, val & MDIO_EEE_5GT);
477 }
478 
479 /**
480  * linkmode_to_mii_eee_cap1_t()
481  * @adv: the linkmode advertisement settings
482  *
483  * A function that translates linkmode to value for IEEE 802.3-2018 45.2.7.13
484  * "EEE advertisement 1" register (7.60)
485  */
linkmode_to_mii_eee_cap1_t(unsigned long * adv)486 static inline u32 linkmode_to_mii_eee_cap1_t(unsigned long *adv)
487 {
488 	u32 result = 0;
489 
490 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, adv))
491 		result |= MDIO_EEE_100TX;
492 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, adv))
493 		result |= MDIO_EEE_1000T;
494 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, adv))
495 		result |= MDIO_EEE_10GT;
496 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, adv))
497 		result |= MDIO_EEE_1000KX;
498 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, adv))
499 		result |= MDIO_EEE_10GKX4;
500 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, adv))
501 		result |= MDIO_EEE_10GKR;
502 
503 	return result;
504 }
505 
506 /**
507  * linkmode_to_mii_eee_cap2_t()
508  * @adv: the linkmode advertisement settings
509  *
510  * A function that translates linkmode to value for IEEE 802.3-2022 45.2.7.16
511  * "EEE advertisement 2" register (7.62)
512  */
linkmode_to_mii_eee_cap2_t(unsigned long * adv)513 static inline u32 linkmode_to_mii_eee_cap2_t(unsigned long *adv)
514 {
515 	u32 result = 0;
516 
517 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, adv))
518 		result |= MDIO_EEE_2_5GT;
519 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, adv))
520 		result |= MDIO_EEE_5GT;
521 
522 	return result;
523 }
524 
525 /**
526  * mii_10base_t1_adv_mod_linkmode_t()
527  * @adv: linkmode advertisement settings
528  * @val: register value
529  *
530  * A function that translates IEEE 802.3cg-2019 45.2.7.26 "10BASE-T1 AN status"
531  * register (7.527) value to the linkmode.
532  */
mii_10base_t1_adv_mod_linkmode_t(unsigned long * adv,u16 val)533 static inline void mii_10base_t1_adv_mod_linkmode_t(unsigned long *adv, u16 val)
534 {
535 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT,
536 			 adv, val & MDIO_AN_10BT1_AN_CTRL_ADV_EEE_T1L);
537 }
538 
539 /**
540  * linkmode_adv_to_mii_10base_t1_t()
541  * @adv: linkmode advertisement settings
542  *
543  * A function that translates the linkmode to IEEE 802.3cg-2019 45.2.7.25
544  * "10BASE-T1 AN control" register (7.526) value.
545  */
linkmode_adv_to_mii_10base_t1_t(unsigned long * adv)546 static inline u32 linkmode_adv_to_mii_10base_t1_t(unsigned long *adv)
547 {
548 	u32 result = 0;
549 
550 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, adv))
551 		result |= MDIO_AN_10BT1_AN_CTRL_ADV_EEE_T1L;
552 
553 	return result;
554 }
555 
556 /**
557  * mii_c73_mod_linkmode - convert a Clause 73 advertisement to linkmodes
558  * @adv: linkmode advertisement setting
559  * @lpa: array of three u16s containing the advertisement
560  *
561  * Convert an IEEE 802.3 Clause 73 advertisement to ethtool link modes.
562  */
mii_c73_mod_linkmode(unsigned long * adv,u16 * lpa)563 static inline void mii_c73_mod_linkmode(unsigned long *adv, u16 *lpa)
564 {
565 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
566 			 adv, lpa[0] & MDIO_AN_C73_0_PAUSE);
567 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
568 			 adv, lpa[0] & MDIO_AN_C73_0_ASM_DIR);
569 	linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
570 			 adv, lpa[1] & MDIO_AN_C73_1_1000BASE_KX);
571 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
572 			 adv, lpa[1] & MDIO_AN_C73_1_10GBASE_KX4);
573 	linkmode_mod_bit(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
574 			 adv, lpa[1] & MDIO_AN_C73_1_40GBASE_KR4);
575 	linkmode_mod_bit(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
576 			 adv, lpa[1] & MDIO_AN_C73_1_40GBASE_CR4);
577 	/* 100GBASE_CR10 and 100GBASE_KP4 not implemented */
578 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
579 			 adv, lpa[1] & MDIO_AN_C73_1_100GBASE_KR4);
580 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
581 			 adv, lpa[1] & MDIO_AN_C73_1_100GBASE_CR4);
582 	/* 25GBASE_R_S not implemented */
583 	/* The 25GBASE_R bit can be used for 25Gbase KR or CR modes */
584 	linkmode_mod_bit(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
585 			 adv, lpa[1] & MDIO_AN_C73_1_25GBASE_R);
586 	linkmode_mod_bit(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
587 			 adv, lpa[1] & MDIO_AN_C73_1_25GBASE_R);
588 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
589 			 adv, lpa[1] & MDIO_AN_C73_1_10GBASE_KR);
590 	linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
591 			 adv, lpa[2] & MDIO_AN_C73_2_2500BASE_KX);
592 	/* 5GBASE_KR not implemented */
593 }
594 
595 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
596 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
597 int __mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
598 		     u16 set);
599 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
600 			     u16 mask, u16 set);
601 
602 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
603 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
604 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
605 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
606 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
607 		   u16 set);
608 int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
609 			   u16 mask, u16 set);
610 int __mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum);
611 int mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum);
612 int mdiobus_c45_read_nested(struct mii_bus *bus, int addr, int devad,
613 			     u32 regnum);
614 int __mdiobus_c45_write(struct mii_bus *bus, int addr,  int devad, u32 regnum,
615 			u16 val);
616 int mdiobus_c45_write(struct mii_bus *bus, int addr,  int devad, u32 regnum,
617 		      u16 val);
618 int mdiobus_c45_write_nested(struct mii_bus *bus, int addr,  int devad,
619 			     u32 regnum, u16 val);
620 int mdiobus_c45_modify(struct mii_bus *bus, int addr, int devad, u32 regnum,
621 		       u16 mask, u16 set);
622 
623 int mdiobus_c45_modify_changed(struct mii_bus *bus, int addr, int devad,
624 			       u32 regnum, u16 mask, u16 set);
625 
__mdiodev_read(struct mdio_device * mdiodev,u32 regnum)626 static inline int __mdiodev_read(struct mdio_device *mdiodev, u32 regnum)
627 {
628 	return __mdiobus_read(mdiodev->bus, mdiodev->addr, regnum);
629 }
630 
__mdiodev_write(struct mdio_device * mdiodev,u32 regnum,u16 val)631 static inline int __mdiodev_write(struct mdio_device *mdiodev, u32 regnum,
632 				  u16 val)
633 {
634 	return __mdiobus_write(mdiodev->bus, mdiodev->addr, regnum, val);
635 }
636 
__mdiodev_modify(struct mdio_device * mdiodev,u32 regnum,u16 mask,u16 set)637 static inline int __mdiodev_modify(struct mdio_device *mdiodev, u32 regnum,
638 				   u16 mask, u16 set)
639 {
640 	return __mdiobus_modify(mdiodev->bus, mdiodev->addr, regnum, mask, set);
641 }
642 
__mdiodev_modify_changed(struct mdio_device * mdiodev,u32 regnum,u16 mask,u16 set)643 static inline int __mdiodev_modify_changed(struct mdio_device *mdiodev,
644 					   u32 regnum, u16 mask, u16 set)
645 {
646 	return __mdiobus_modify_changed(mdiodev->bus, mdiodev->addr, regnum,
647 					mask, set);
648 }
649 
mdiodev_read(struct mdio_device * mdiodev,u32 regnum)650 static inline int mdiodev_read(struct mdio_device *mdiodev, u32 regnum)
651 {
652 	return mdiobus_read(mdiodev->bus, mdiodev->addr, regnum);
653 }
654 
mdiodev_write(struct mdio_device * mdiodev,u32 regnum,u16 val)655 static inline int mdiodev_write(struct mdio_device *mdiodev, u32 regnum,
656 				u16 val)
657 {
658 	return mdiobus_write(mdiodev->bus, mdiodev->addr, regnum, val);
659 }
660 
mdiodev_modify(struct mdio_device * mdiodev,u32 regnum,u16 mask,u16 set)661 static inline int mdiodev_modify(struct mdio_device *mdiodev, u32 regnum,
662 				 u16 mask, u16 set)
663 {
664 	return mdiobus_modify(mdiodev->bus, mdiodev->addr, regnum, mask, set);
665 }
666 
mdiodev_modify_changed(struct mdio_device * mdiodev,u32 regnum,u16 mask,u16 set)667 static inline int mdiodev_modify_changed(struct mdio_device *mdiodev,
668 					 u32 regnum, u16 mask, u16 set)
669 {
670 	return mdiobus_modify_changed(mdiodev->bus, mdiodev->addr, regnum,
671 				      mask, set);
672 }
673 
mdiodev_c45_modify(struct mdio_device * mdiodev,int devad,u32 regnum,u16 mask,u16 set)674 static inline int mdiodev_c45_modify(struct mdio_device *mdiodev, int devad,
675 				     u32 regnum, u16 mask, u16 set)
676 {
677 	return mdiobus_c45_modify(mdiodev->bus, mdiodev->addr, devad, regnum,
678 				  mask, set);
679 }
680 
mdiodev_c45_modify_changed(struct mdio_device * mdiodev,int devad,u32 regnum,u16 mask,u16 set)681 static inline int mdiodev_c45_modify_changed(struct mdio_device *mdiodev,
682 					     int devad, u32 regnum, u16 mask,
683 					     u16 set)
684 {
685 	return mdiobus_c45_modify_changed(mdiodev->bus, mdiodev->addr, devad,
686 					  regnum, mask, set);
687 }
688 
mdiodev_c45_read(struct mdio_device * mdiodev,int devad,u16 regnum)689 static inline int mdiodev_c45_read(struct mdio_device *mdiodev, int devad,
690 				   u16 regnum)
691 {
692 	return mdiobus_c45_read(mdiodev->bus, mdiodev->addr, devad, regnum);
693 }
694 
mdiodev_c45_write(struct mdio_device * mdiodev,u32 devad,u16 regnum,u16 val)695 static inline int mdiodev_c45_write(struct mdio_device *mdiodev, u32 devad,
696 				    u16 regnum, u16 val)
697 {
698 	return mdiobus_c45_write(mdiodev->bus, mdiodev->addr, devad, regnum,
699 				 val);
700 }
701 
702 int mdiobus_register_device(struct mdio_device *mdiodev);
703 int mdiobus_unregister_device(struct mdio_device *mdiodev);
704 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
705 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
706 
707 /**
708  * mdio_module_driver() - Helper macro for registering mdio drivers
709  * @_mdio_driver: driver to register
710  *
711  * Helper macro for MDIO drivers which do not do anything special in module
712  * init/exit. Each module may only use this macro once, and calling it
713  * replaces module_init() and module_exit().
714  */
715 #define mdio_module_driver(_mdio_driver)				\
716 static int __init mdio_module_init(void)				\
717 {									\
718 	return mdio_driver_register(&_mdio_driver);			\
719 }									\
720 module_init(mdio_module_init);						\
721 static void __exit mdio_module_exit(void)				\
722 {									\
723 	mdio_driver_unregister(&_mdio_driver);				\
724 }									\
725 module_exit(mdio_module_exit)
726 
727 #endif /* __LINUX_MDIO_H__ */
728