1
2
3
4
5
6
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
16struct ethtool_cmd;
17
18struct 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;
25 unsigned int force_media : 1;
26 unsigned int supports_gmii : 1;
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
33extern int mii_link_ok (struct mii_if_info *mii);
34extern int mii_nway_restart (struct mii_if_info *mii);
35extern int mii_ethtool_gset(struct mii_if_info *mii, struct ethtool_cmd *ecmd);
36extern void mii_ethtool_get_link_ksettings(
37 struct mii_if_info *mii, struct ethtool_link_ksettings *cmd);
38extern int mii_ethtool_sset(struct mii_if_info *mii, struct ethtool_cmd *ecmd);
39extern int mii_ethtool_set_link_ksettings(
40 struct mii_if_info *mii, const struct ethtool_link_ksettings *cmd);
41extern int mii_check_gmii_support(struct mii_if_info *mii);
42extern void mii_check_link (struct mii_if_info *mii);
43extern unsigned int mii_check_media (struct mii_if_info *mii,
44 unsigned int ok_to_print,
45 unsigned int init_media);
46extern 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
51static inline struct mii_ioctl_data *if_mii(struct ifreq *rq)
52{
53 return (struct mii_ioctl_data *) &rq->ifr_ifru;
54}
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70static 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
90
91
92
93
94
95
96
97static 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
109
110
111
112
113
114
115static 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
137
138
139
140
141
142
143static inline u32 linkmode_adv_to_mii_adv_t(unsigned long *advertising)
144{
145 u32 result = 0;
146
147 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, advertising))
148 result |= ADVERTISE_10HALF;
149 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, advertising))
150 result |= ADVERTISE_10FULL;
151 if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, advertising))
152 result |= ADVERTISE_100HALF;
153 if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, advertising))
154 result |= ADVERTISE_100FULL;
155 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising))
156 result |= ADVERTISE_PAUSE_CAP;
157 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising))
158 result |= ADVERTISE_PAUSE_ASYM;
159
160 return result;
161}
162
163
164
165
166
167
168
169
170static inline u32 mii_adv_to_ethtool_adv_t(u32 adv)
171{
172 u32 result = 0;
173
174 if (adv & ADVERTISE_10HALF)
175 result |= ADVERTISED_10baseT_Half;
176 if (adv & ADVERTISE_10FULL)
177 result |= ADVERTISED_10baseT_Full;
178 if (adv & ADVERTISE_100HALF)
179 result |= ADVERTISED_100baseT_Half;
180 if (adv & ADVERTISE_100FULL)
181 result |= ADVERTISED_100baseT_Full;
182 if (adv & ADVERTISE_PAUSE_CAP)
183 result |= ADVERTISED_Pause;
184 if (adv & ADVERTISE_PAUSE_ASYM)
185 result |= ADVERTISED_Asym_Pause;
186
187 return result;
188}
189
190
191
192
193
194
195
196
197
198static inline u32 ethtool_adv_to_mii_ctrl1000_t(u32 ethadv)
199{
200 u32 result = 0;
201
202 if (ethadv & ADVERTISED_1000baseT_Half)
203 result |= ADVERTISE_1000HALF;
204 if (ethadv & ADVERTISED_1000baseT_Full)
205 result |= ADVERTISE_1000FULL;
206
207 return result;
208}
209
210
211
212
213
214
215
216
217
218static inline u32 linkmode_adv_to_mii_ctrl1000_t(unsigned long *advertising)
219{
220 u32 result = 0;
221
222 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
223 advertising))
224 result |= ADVERTISE_1000HALF;
225 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
226 advertising))
227 result |= ADVERTISE_1000FULL;
228
229 return result;
230}
231
232
233
234
235
236
237
238
239
240static inline u32 mii_ctrl1000_to_ethtool_adv_t(u32 adv)
241{
242 u32 result = 0;
243
244 if (adv & ADVERTISE_1000HALF)
245 result |= ADVERTISED_1000baseT_Half;
246 if (adv & ADVERTISE_1000FULL)
247 result |= ADVERTISED_1000baseT_Full;
248
249 return result;
250}
251
252
253
254
255
256
257
258
259
260static inline u32 mii_lpa_to_ethtool_lpa_t(u32 lpa)
261{
262 u32 result = 0;
263
264 if (lpa & LPA_LPACK)
265 result |= ADVERTISED_Autoneg;
266
267 return result | mii_adv_to_ethtool_adv_t(lpa);
268}
269
270
271
272
273
274
275
276
277
278static inline u32 mii_stat1000_to_ethtool_lpa_t(u32 lpa)
279{
280 u32 result = 0;
281
282 if (lpa & LPA_1000HALF)
283 result |= ADVERTISED_1000baseT_Half;
284 if (lpa & LPA_1000FULL)
285 result |= ADVERTISED_1000baseT_Full;
286
287 return result;
288}
289
290
291
292
293
294
295
296
297
298static inline u32 ethtool_adv_to_mii_adv_x(u32 ethadv)
299{
300 u32 result = 0;
301
302 if (ethadv & ADVERTISED_1000baseT_Half)
303 result |= ADVERTISE_1000XHALF;
304 if (ethadv & ADVERTISED_1000baseT_Full)
305 result |= ADVERTISE_1000XFULL;
306 if (ethadv & ADVERTISED_Pause)
307 result |= ADVERTISE_1000XPAUSE;
308 if (ethadv & ADVERTISED_Asym_Pause)
309 result |= ADVERTISE_1000XPSE_ASYM;
310
311 return result;
312}
313
314
315
316
317
318
319
320
321
322static inline u32 mii_adv_to_ethtool_adv_x(u32 adv)
323{
324 u32 result = 0;
325
326 if (adv & ADVERTISE_1000XHALF)
327 result |= ADVERTISED_1000baseT_Half;
328 if (adv & ADVERTISE_1000XFULL)
329 result |= ADVERTISED_1000baseT_Full;
330 if (adv & ADVERTISE_1000XPAUSE)
331 result |= ADVERTISED_Pause;
332 if (adv & ADVERTISE_1000XPSE_ASYM)
333 result |= ADVERTISED_Asym_Pause;
334
335 return result;
336}
337
338
339
340
341
342
343
344
345
346static inline u32 mii_lpa_to_ethtool_lpa_x(u32 lpa)
347{
348 u32 result = 0;
349
350 if (lpa & LPA_LPACK)
351 result |= ADVERTISED_Autoneg;
352
353 return result | mii_adv_to_ethtool_adv_x(lpa);
354}
355
356
357
358
359
360
361
362
363
364static inline void mii_adv_to_linkmode_adv_t(unsigned long *advertising,
365 u32 adv)
366{
367 linkmode_zero(advertising);
368
369 if (adv & ADVERTISE_10HALF)
370 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
371 advertising);
372 if (adv & ADVERTISE_10FULL)
373 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
374 advertising);
375 if (adv & ADVERTISE_100HALF)
376 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
377 advertising);
378 if (adv & ADVERTISE_100FULL)
379 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
380 advertising);
381 if (adv & ADVERTISE_PAUSE_CAP)
382 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising);
383 if (adv & ADVERTISE_PAUSE_ASYM)
384 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising);
385}
386
387
388
389
390
391
392
393
394static inline u32 ethtool_adv_to_lcl_adv_t(u32 advertising)
395{
396 u32 lcl_adv = 0;
397
398 if (advertising & ADVERTISED_Pause)
399 lcl_adv |= ADVERTISE_PAUSE_CAP;
400 if (advertising & ADVERTISED_Asym_Pause)
401 lcl_adv |= ADVERTISE_PAUSE_ASYM;
402
403 return lcl_adv;
404}
405
406
407
408
409
410static inline u16 mii_advertise_flowctrl(int cap)
411{
412 u16 adv = 0;
413
414 if (cap & FLOW_CTRL_RX)
415 adv = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
416 if (cap & FLOW_CTRL_TX)
417 adv ^= ADVERTISE_PAUSE_ASYM;
418
419 return adv;
420}
421
422
423
424
425
426
427
428
429static inline u8 mii_resolve_flowctrl_fdx(u16 lcladv, u16 rmtadv)
430{
431 u8 cap = 0;
432
433 if (lcladv & rmtadv & ADVERTISE_PAUSE_CAP) {
434 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
435 } else if (lcladv & rmtadv & ADVERTISE_PAUSE_ASYM) {
436 if (lcladv & ADVERTISE_PAUSE_CAP)
437 cap = FLOW_CTRL_RX;
438 else if (rmtadv & ADVERTISE_PAUSE_CAP)
439 cap = FLOW_CTRL_TX;
440 }
441
442 return cap;
443}
444
445#endif
446