xref: /linux-6.15/include/linux/mii.h (revision b31cdffa)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * linux/mii.h: definitions for MII-compatible transceivers
4  * Originally drivers/net/sunhme.h.
5  *
6  * Copyright (C) 1996, 1999, 2001 David S. Miller ([email protected])
7  */
8 #ifndef __LINUX_MII_H__
9 #define __LINUX_MII_H__
10 
11 
12 #include <linux/if.h>
13 #include <linux/linkmode.h>
14 #include <uapi/linux/mii.h>
15 
16 struct ethtool_cmd;
17 
18 struct mii_if_info {
19 	int phy_id;
20 	int advertising;
21 	int phy_id_mask;
22 	int reg_num_mask;
23 
24 	unsigned int full_duplex : 1;	/* is full duplex? */
25 	unsigned int force_media : 1;	/* is autoneg. disabled? */
26 	unsigned int supports_gmii : 1; /* are GMII registers supported? */
27 
28 	struct net_device *dev;
29 	int (*mdio_read) (struct net_device *dev, int phy_id, int location);
30 	void (*mdio_write) (struct net_device *dev, int phy_id, int location, int val);
31 };
32 
33 extern int mii_link_ok (struct mii_if_info *mii);
34 extern int mii_nway_restart (struct mii_if_info *mii);
35 extern int mii_ethtool_gset(struct mii_if_info *mii, struct ethtool_cmd *ecmd);
36 extern void mii_ethtool_get_link_ksettings(
37 	struct mii_if_info *mii, struct ethtool_link_ksettings *cmd);
38 extern int mii_ethtool_sset(struct mii_if_info *mii, struct ethtool_cmd *ecmd);
39 extern int mii_ethtool_set_link_ksettings(
40 	struct mii_if_info *mii, const struct ethtool_link_ksettings *cmd);
41 extern int mii_check_gmii_support(struct mii_if_info *mii);
42 extern void mii_check_link (struct mii_if_info *mii);
43 extern unsigned int mii_check_media (struct mii_if_info *mii,
44 				     unsigned int ok_to_print,
45 				     unsigned int init_media);
46 extern int generic_mii_ioctl(struct mii_if_info *mii_if,
47 			     struct mii_ioctl_data *mii_data, int cmd,
48 			     unsigned int *duplex_changed);
49 
50 
51 static inline struct mii_ioctl_data *if_mii(struct ifreq *rq)
52 {
53 	return (struct mii_ioctl_data *) &rq->ifr_ifru;
54 }
55 
56 /**
57  * mii_nway_result
58  * @negotiated: value of MII ANAR and'd with ANLPAR
59  *
60  * Given a set of MII abilities, check each bit and returns the
61  * currently supported media, in the priority order defined by
62  * IEEE 802.3u.  We use LPA_xxx constants but note this is not the
63  * value of LPA solely, as described above.
64  *
65  * The one exception to IEEE 802.3u is that 100baseT4 is placed
66  * between 100T-full and 100T-half.  If your phy does not support
67  * 100T4 this is fine.  If your phy places 100T4 elsewhere in the
68  * priority order, you will need to roll your own function.
69  */
70 static inline unsigned int mii_nway_result (unsigned int negotiated)
71 {
72 	unsigned int ret;
73 
74 	if (negotiated & LPA_100FULL)
75 		ret = LPA_100FULL;
76 	else if (negotiated & LPA_100BASE4)
77 		ret = LPA_100BASE4;
78 	else if (negotiated & LPA_100HALF)
79 		ret = LPA_100HALF;
80 	else if (negotiated & LPA_10FULL)
81 		ret = LPA_10FULL;
82 	else
83 		ret = LPA_10HALF;
84 
85 	return ret;
86 }
87 
88 /**
89  * mii_duplex
90  * @duplex_lock: Non-zero if duplex is locked at full
91  * @negotiated: value of MII ANAR and'd with ANLPAR
92  *
93  * A small helper function for a common case.  Returns one
94  * if the media is operating or locked at full duplex, and
95  * returns zero otherwise.
96  */
97 static inline unsigned int mii_duplex (unsigned int duplex_lock,
98 				       unsigned int negotiated)
99 {
100 	if (duplex_lock)
101 		return 1;
102 	if (mii_nway_result(negotiated) & LPA_DUPLEX)
103 		return 1;
104 	return 0;
105 }
106 
107 /**
108  * ethtool_adv_to_mii_adv_t
109  * @ethadv: the ethtool advertisement settings
110  *
111  * A small helper function that translates ethtool advertisement
112  * settings to phy autonegotiation advertisements for the
113  * MII_ADVERTISE register.
114  */
115 static inline u32 ethtool_adv_to_mii_adv_t(u32 ethadv)
116 {
117 	u32 result = 0;
118 
119 	if (ethadv & ADVERTISED_10baseT_Half)
120 		result |= ADVERTISE_10HALF;
121 	if (ethadv & ADVERTISED_10baseT_Full)
122 		result |= ADVERTISE_10FULL;
123 	if (ethadv & ADVERTISED_100baseT_Half)
124 		result |= ADVERTISE_100HALF;
125 	if (ethadv & ADVERTISED_100baseT_Full)
126 		result |= ADVERTISE_100FULL;
127 	if (ethadv & ADVERTISED_Pause)
128 		result |= ADVERTISE_PAUSE_CAP;
129 	if (ethadv & ADVERTISED_Asym_Pause)
130 		result |= ADVERTISE_PAUSE_ASYM;
131 
132 	return result;
133 }
134 
135 /**
136  * mii_adv_to_ethtool_adv_t
137  * @adv: value of the MII_ADVERTISE register
138  *
139  * A small helper function that translates MII_ADVERTISE bits
140  * to ethtool advertisement settings.
141  */
142 static inline u32 mii_adv_to_ethtool_adv_t(u32 adv)
143 {
144 	u32 result = 0;
145 
146 	if (adv & ADVERTISE_10HALF)
147 		result |= ADVERTISED_10baseT_Half;
148 	if (adv & ADVERTISE_10FULL)
149 		result |= ADVERTISED_10baseT_Full;
150 	if (adv & ADVERTISE_100HALF)
151 		result |= ADVERTISED_100baseT_Half;
152 	if (adv & ADVERTISE_100FULL)
153 		result |= ADVERTISED_100baseT_Full;
154 	if (adv & ADVERTISE_PAUSE_CAP)
155 		result |= ADVERTISED_Pause;
156 	if (adv & ADVERTISE_PAUSE_ASYM)
157 		result |= ADVERTISED_Asym_Pause;
158 
159 	return result;
160 }
161 
162 /**
163  * ethtool_adv_to_mii_ctrl1000_t
164  * @ethadv: the ethtool advertisement settings
165  *
166  * A small helper function that translates ethtool advertisement
167  * settings to phy autonegotiation advertisements for the
168  * MII_CTRL1000 register when in 1000T mode.
169  */
170 static inline u32 ethtool_adv_to_mii_ctrl1000_t(u32 ethadv)
171 {
172 	u32 result = 0;
173 
174 	if (ethadv & ADVERTISED_1000baseT_Half)
175 		result |= ADVERTISE_1000HALF;
176 	if (ethadv & ADVERTISED_1000baseT_Full)
177 		result |= ADVERTISE_1000FULL;
178 
179 	return result;
180 }
181 
182 /**
183  * mii_ctrl1000_to_ethtool_adv_t
184  * @adv: value of the MII_CTRL1000 register
185  *
186  * A small helper function that translates MII_CTRL1000
187  * bits, when in 1000Base-T mode, to ethtool
188  * advertisement settings.
189  */
190 static inline u32 mii_ctrl1000_to_ethtool_adv_t(u32 adv)
191 {
192 	u32 result = 0;
193 
194 	if (adv & ADVERTISE_1000HALF)
195 		result |= ADVERTISED_1000baseT_Half;
196 	if (adv & ADVERTISE_1000FULL)
197 		result |= ADVERTISED_1000baseT_Full;
198 
199 	return result;
200 }
201 
202 /**
203  * mii_lpa_to_ethtool_lpa_t
204  * @adv: value of the MII_LPA register
205  *
206  * A small helper function that translates MII_LPA
207  * bits, when in 1000Base-T mode, to ethtool
208  * LP advertisement settings.
209  */
210 static inline u32 mii_lpa_to_ethtool_lpa_t(u32 lpa)
211 {
212 	u32 result = 0;
213 
214 	if (lpa & LPA_LPACK)
215 		result |= ADVERTISED_Autoneg;
216 
217 	return result | mii_adv_to_ethtool_adv_t(lpa);
218 }
219 
220 /**
221  * mii_stat1000_to_ethtool_lpa_t
222  * @adv: value of the MII_STAT1000 register
223  *
224  * A small helper function that translates MII_STAT1000
225  * bits, when in 1000Base-T mode, to ethtool
226  * advertisement settings.
227  */
228 static inline u32 mii_stat1000_to_ethtool_lpa_t(u32 lpa)
229 {
230 	u32 result = 0;
231 
232 	if (lpa & LPA_1000HALF)
233 		result |= ADVERTISED_1000baseT_Half;
234 	if (lpa & LPA_1000FULL)
235 		result |= ADVERTISED_1000baseT_Full;
236 
237 	return result;
238 }
239 
240 /**
241  * ethtool_adv_to_mii_adv_x
242  * @ethadv: the ethtool advertisement settings
243  *
244  * A small helper function that translates ethtool advertisement
245  * settings to phy autonegotiation advertisements for the
246  * MII_CTRL1000 register when in 1000Base-X mode.
247  */
248 static inline u32 ethtool_adv_to_mii_adv_x(u32 ethadv)
249 {
250 	u32 result = 0;
251 
252 	if (ethadv & ADVERTISED_1000baseT_Half)
253 		result |= ADVERTISE_1000XHALF;
254 	if (ethadv & ADVERTISED_1000baseT_Full)
255 		result |= ADVERTISE_1000XFULL;
256 	if (ethadv & ADVERTISED_Pause)
257 		result |= ADVERTISE_1000XPAUSE;
258 	if (ethadv & ADVERTISED_Asym_Pause)
259 		result |= ADVERTISE_1000XPSE_ASYM;
260 
261 	return result;
262 }
263 
264 /**
265  * mii_adv_to_ethtool_adv_x
266  * @adv: value of the MII_CTRL1000 register
267  *
268  * A small helper function that translates MII_CTRL1000
269  * bits, when in 1000Base-X mode, to ethtool
270  * advertisement settings.
271  */
272 static inline u32 mii_adv_to_ethtool_adv_x(u32 adv)
273 {
274 	u32 result = 0;
275 
276 	if (adv & ADVERTISE_1000XHALF)
277 		result |= ADVERTISED_1000baseT_Half;
278 	if (adv & ADVERTISE_1000XFULL)
279 		result |= ADVERTISED_1000baseT_Full;
280 	if (adv & ADVERTISE_1000XPAUSE)
281 		result |= ADVERTISED_Pause;
282 	if (adv & ADVERTISE_1000XPSE_ASYM)
283 		result |= ADVERTISED_Asym_Pause;
284 
285 	return result;
286 }
287 
288 /**
289  * mii_lpa_to_ethtool_lpa_x
290  * @adv: value of the MII_LPA register
291  *
292  * A small helper function that translates MII_LPA
293  * bits, when in 1000Base-X mode, to ethtool
294  * LP advertisement settings.
295  */
296 static inline u32 mii_lpa_to_ethtool_lpa_x(u32 lpa)
297 {
298 	u32 result = 0;
299 
300 	if (lpa & LPA_LPACK)
301 		result |= ADVERTISED_Autoneg;
302 
303 	return result | mii_adv_to_ethtool_adv_x(lpa);
304 }
305 
306 /**
307  * mii_advertise_flowctrl - get flow control advertisement flags
308  * @cap: Flow control capabilities (FLOW_CTRL_RX, FLOW_CTRL_TX or both)
309  */
310 static inline u16 mii_advertise_flowctrl(int cap)
311 {
312 	u16 adv = 0;
313 
314 	if (cap & FLOW_CTRL_RX)
315 		adv = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
316 	if (cap & FLOW_CTRL_TX)
317 		adv ^= ADVERTISE_PAUSE_ASYM;
318 
319 	return adv;
320 }
321 
322 /**
323  * mii_resolve_flowctrl_fdx
324  * @lcladv: value of MII ADVERTISE register
325  * @rmtadv: value of MII LPA register
326  *
327  * Resolve full duplex flow control as per IEEE 802.3-2005 table 28B-3
328  */
329 static inline u8 mii_resolve_flowctrl_fdx(u16 lcladv, u16 rmtadv)
330 {
331 	u8 cap = 0;
332 
333 	if (lcladv & rmtadv & ADVERTISE_PAUSE_CAP) {
334 		cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
335 	} else if (lcladv & rmtadv & ADVERTISE_PAUSE_ASYM) {
336 		if (lcladv & ADVERTISE_PAUSE_CAP)
337 			cap = FLOW_CTRL_RX;
338 		else if (rmtadv & ADVERTISE_PAUSE_CAP)
339 			cap = FLOW_CTRL_TX;
340 	}
341 
342 	return cap;
343 }
344 
345 #endif /* __LINUX_MII_H__ */
346