diff options
Diffstat (limited to 'drivers/net/mdio.c')
-rw-r--r-- | drivers/net/mdio.c | 431 |
1 files changed, 431 insertions, 0 deletions
diff --git a/drivers/net/mdio.c b/drivers/net/mdio.c new file mode 100644 index 000000000000..dc45e9856c35 --- /dev/null +++ b/drivers/net/mdio.c | |||
@@ -0,0 +1,431 @@ | |||
1 | /* | ||
2 | * mdio.c: Generic support for MDIO-compatible transceivers | ||
3 | * Copyright 2006-2009 Solarflare Communications Inc. | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify it | ||
6 | * under the terms of the GNU General Public License version 2 as published | ||
7 | * by the Free Software Foundation, incorporated herein by reference. | ||
8 | */ | ||
9 | |||
10 | #include <linux/kernel.h> | ||
11 | #include <linux/capability.h> | ||
12 | #include <linux/errno.h> | ||
13 | #include <linux/ethtool.h> | ||
14 | #include <linux/mdio.h> | ||
15 | #include <linux/module.h> | ||
16 | |||
17 | /** | ||
18 | * mdio45_probe - probe for an MDIO (clause 45) device | ||
19 | * @mdio: MDIO interface | ||
20 | * @prtad: Expected PHY address | ||
21 | * | ||
22 | * This sets @prtad and @mmds in the MDIO interface if successful. | ||
23 | * Returns 0 on success, negative on error. | ||
24 | */ | ||
25 | int mdio45_probe(struct mdio_if_info *mdio, int prtad) | ||
26 | { | ||
27 | int mmd, stat2, devs1, devs2; | ||
28 | |||
29 | /* Assume PHY must have at least one of PMA/PMD, WIS, PCS, PHY | ||
30 | * XS or DTE XS; give up if none is present. */ | ||
31 | for (mmd = 1; mmd <= 5; mmd++) { | ||
32 | /* Is this MMD present? */ | ||
33 | stat2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_STAT2); | ||
34 | if (stat2 < 0 || | ||
35 | (stat2 & MDIO_STAT2_DEVPRST) != MDIO_STAT2_DEVPRST_VAL) | ||
36 | continue; | ||
37 | |||
38 | /* It should tell us about all the other MMDs */ | ||
39 | devs1 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS1); | ||
40 | devs2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS2); | ||
41 | if (devs1 < 0 || devs2 < 0) | ||
42 | continue; | ||
43 | |||
44 | mdio->prtad = prtad; | ||
45 | mdio->mmds = devs1 | (devs2 << 16); | ||
46 | return 0; | ||
47 | } | ||
48 | |||
49 | return -ENODEV; | ||
50 | } | ||
51 | EXPORT_SYMBOL(mdio45_probe); | ||
52 | |||
53 | /** | ||
54 | * mdio_set_flag - set or clear flag in an MDIO register | ||
55 | * @mdio: MDIO interface | ||
56 | * @prtad: PHY address | ||
57 | * @devad: MMD address | ||
58 | * @addr: Register address | ||
59 | * @mask: Mask for flag (single bit set) | ||
60 | * @sense: New value of flag | ||
61 | * | ||
62 | * This debounces changes: it does not write the register if the flag | ||
63 | * already has the proper value. Returns 0 on success, negative on error. | ||
64 | */ | ||
65 | int mdio_set_flag(const struct mdio_if_info *mdio, | ||
66 | int prtad, int devad, u16 addr, int mask, | ||
67 | bool sense) | ||
68 | { | ||
69 | int old_val = mdio->mdio_read(mdio->dev, prtad, devad, addr); | ||
70 | int new_val; | ||
71 | |||
72 | if (old_val < 0) | ||
73 | return old_val; | ||
74 | if (sense) | ||
75 | new_val = old_val | mask; | ||
76 | else | ||
77 | new_val = old_val & ~mask; | ||
78 | if (old_val == new_val) | ||
79 | return 0; | ||
80 | return mdio->mdio_write(mdio->dev, prtad, devad, addr, new_val); | ||
81 | } | ||
82 | EXPORT_SYMBOL(mdio_set_flag); | ||
83 | |||
84 | /** | ||
85 | * mdio_link_ok - is link status up/OK | ||
86 | * @mdio: MDIO interface | ||
87 | * @mmd_mask: Mask for MMDs to check | ||
88 | * | ||
89 | * Returns 1 if the PHY reports link status up/OK, 0 otherwise. | ||
90 | * @mmd_mask is normally @mdio->mmds, but if loopback is enabled | ||
91 | * the MMDs being bypassed should be excluded from the mask. | ||
92 | */ | ||
93 | int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmd_mask) | ||
94 | { | ||
95 | int devad, reg; | ||
96 | |||
97 | if (!mmd_mask) { | ||
98 | /* Use absence of XGMII faults in lieu of link state */ | ||
99 | reg = mdio->mdio_read(mdio->dev, mdio->prtad, | ||
100 | MDIO_MMD_PHYXS, MDIO_STAT2); | ||
101 | return reg >= 0 && !(reg & MDIO_STAT2_RXFAULT); | ||
102 | } | ||
103 | |||
104 | for (devad = 0; mmd_mask; devad++) { | ||
105 | if (mmd_mask & (1 << devad)) { | ||
106 | mmd_mask &= ~(1 << devad); | ||
107 | |||
108 | /* Read twice because link state is latched and a | ||
109 | * read moves the current state into the register */ | ||
110 | mdio->mdio_read(mdio->dev, mdio->prtad, | ||
111 | devad, MDIO_STAT1); | ||
112 | reg = mdio->mdio_read(mdio->dev, mdio->prtad, | ||
113 | devad, MDIO_STAT1); | ||
114 | if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS)) | ||
115 | return false; | ||
116 | } | ||
117 | } | ||
118 | |||
119 | return true; | ||
120 | } | ||
121 | EXPORT_SYMBOL(mdio45_links_ok); | ||
122 | |||
123 | /** | ||
124 | * mdio45_nway_restart - restart auto-negotiation for this interface | ||
125 | * @mdio: MDIO interface | ||
126 | * | ||
127 | * Returns 0 on success, negative on error. | ||
128 | */ | ||
129 | int mdio45_nway_restart(const struct mdio_if_info *mdio) | ||
130 | { | ||
131 | if (!(mdio->mmds & MDIO_DEVS_AN)) | ||
132 | return -EOPNOTSUPP; | ||
133 | |||
134 | mdio_set_flag(mdio, mdio->prtad, MDIO_MMD_AN, MDIO_CTRL1, | ||
135 | MDIO_AN_CTRL1_RESTART, true); | ||
136 | return 0; | ||
137 | } | ||
138 | EXPORT_SYMBOL(mdio45_nway_restart); | ||
139 | |||
140 | static u32 mdio45_get_an(const struct mdio_if_info *mdio, u16 addr) | ||
141 | { | ||
142 | u32 result = 0; | ||
143 | int reg; | ||
144 | |||
145 | reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN, addr); | ||
146 | if (reg & ADVERTISE_10HALF) | ||
147 | result |= ADVERTISED_10baseT_Half; | ||
148 | if (reg & ADVERTISE_10FULL) | ||
149 | result |= ADVERTISED_10baseT_Full; | ||
150 | if (reg & ADVERTISE_100HALF) | ||
151 | result |= ADVERTISED_100baseT_Half; | ||
152 | if (reg & ADVERTISE_100FULL) | ||
153 | result |= ADVERTISED_100baseT_Full; | ||
154 | return result; | ||
155 | } | ||
156 | |||
157 | /** | ||
158 | * mdio45_ethtool_gset_npage - get settings for ETHTOOL_GSET | ||
159 | * @mdio: MDIO interface | ||
160 | * @ecmd: Ethtool request structure | ||
161 | * @npage_adv: Modes currently advertised on next pages | ||
162 | * @npage_lpa: Modes advertised by link partner on next pages | ||
163 | * | ||
164 | * Since the CSRs for auto-negotiation using next pages are not fully | ||
165 | * standardised, this function does not attempt to decode them. The | ||
166 | * caller must pass them in. | ||
167 | */ | ||
168 | void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio, | ||
169 | struct ethtool_cmd *ecmd, | ||
170 | u32 npage_adv, u32 npage_lpa) | ||
171 | { | ||
172 | int reg; | ||
173 | |||
174 | ecmd->transceiver = XCVR_INTERNAL; | ||
175 | ecmd->phy_address = mdio->prtad; | ||
176 | ecmd->mdio_support = | ||
177 | mdio->mode_support & (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22); | ||
178 | |||
179 | reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, | ||
180 | MDIO_CTRL2); | ||
181 | switch (reg & MDIO_PMA_CTRL2_TYPE) { | ||
182 | case MDIO_PMA_CTRL2_10GBT: | ||
183 | case MDIO_PMA_CTRL2_1000BT: | ||
184 | case MDIO_PMA_CTRL2_100BTX: | ||
185 | case MDIO_PMA_CTRL2_10BT: | ||
186 | ecmd->port = PORT_TP; | ||
187 | ecmd->supported = SUPPORTED_TP; | ||
188 | reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, | ||
189 | MDIO_SPEED); | ||
190 | if (reg & MDIO_SPEED_10G) | ||
191 | ecmd->supported |= SUPPORTED_10000baseT_Full; | ||
192 | if (reg & MDIO_PMA_SPEED_1000) | ||
193 | ecmd->supported |= (SUPPORTED_1000baseT_Full | | ||
194 | SUPPORTED_1000baseT_Half); | ||
195 | if (reg & MDIO_PMA_SPEED_100) | ||
196 | ecmd->supported |= (SUPPORTED_100baseT_Full | | ||
197 | SUPPORTED_100baseT_Half); | ||
198 | if (reg & MDIO_PMA_SPEED_10) | ||
199 | ecmd->supported |= (SUPPORTED_10baseT_Full | | ||
200 | SUPPORTED_10baseT_Half); | ||
201 | ecmd->advertising = ADVERTISED_TP; | ||
202 | break; | ||
203 | |||
204 | case MDIO_PMA_CTRL2_10GBCX4: | ||
205 | ecmd->port = PORT_OTHER; | ||
206 | ecmd->supported = 0; | ||
207 | ecmd->advertising = 0; | ||
208 | break; | ||
209 | |||
210 | case MDIO_PMA_CTRL2_10GBKX4: | ||
211 | case MDIO_PMA_CTRL2_10GBKR: | ||
212 | case MDIO_PMA_CTRL2_1000BKX: | ||
213 | ecmd->port = PORT_OTHER; | ||
214 | ecmd->supported = SUPPORTED_Backplane; | ||
215 | reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, | ||
216 | MDIO_PMA_EXTABLE); | ||
217 | if (reg & MDIO_PMA_EXTABLE_10GBKX4) | ||
218 | ecmd->supported |= SUPPORTED_10000baseKX4_Full; | ||
219 | if (reg & MDIO_PMA_EXTABLE_10GBKR) | ||
220 | ecmd->supported |= SUPPORTED_10000baseKR_Full; | ||
221 | if (reg & MDIO_PMA_EXTABLE_1000BKX) | ||
222 | ecmd->supported |= SUPPORTED_1000baseKX_Full; | ||
223 | reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, | ||
224 | MDIO_PMA_10GBR_FECABLE); | ||
225 | if (reg & MDIO_PMA_10GBR_FECABLE_ABLE) | ||
226 | ecmd->supported |= SUPPORTED_10000baseR_FEC; | ||
227 | ecmd->advertising = ADVERTISED_Backplane; | ||
228 | break; | ||
229 | |||
230 | /* All the other defined modes are flavours of optical */ | ||
231 | default: | ||
232 | ecmd->port = PORT_FIBRE; | ||
233 | ecmd->supported = SUPPORTED_FIBRE; | ||
234 | ecmd->advertising = ADVERTISED_FIBRE; | ||
235 | break; | ||
236 | } | ||
237 | |||
238 | if (mdio->mmds & MDIO_DEVS_AN) { | ||
239 | ecmd->supported |= SUPPORTED_Autoneg; | ||
240 | reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN, | ||
241 | MDIO_CTRL1); | ||
242 | if (reg & MDIO_AN_CTRL1_ENABLE) { | ||
243 | ecmd->autoneg = AUTONEG_ENABLE; | ||
244 | ecmd->advertising |= | ||
245 | ADVERTISED_Autoneg | | ||
246 | mdio45_get_an(mdio, MDIO_AN_ADVERTISE) | | ||
247 | npage_adv; | ||
248 | } else { | ||
249 | ecmd->autoneg = AUTONEG_DISABLE; | ||
250 | } | ||
251 | } else { | ||
252 | ecmd->autoneg = AUTONEG_DISABLE; | ||
253 | } | ||
254 | |||
255 | if (ecmd->autoneg) { | ||
256 | u32 modes = 0; | ||
257 | int an_stat = mdio->mdio_read(mdio->dev, mdio->prtad, | ||
258 | MDIO_MMD_AN, MDIO_STAT1); | ||
259 | |||
260 | /* If AN is complete and successful, report best common | ||
261 | * mode, otherwise report best advertised mode. */ | ||
262 | if (an_stat & MDIO_AN_STAT1_COMPLETE) { | ||
263 | ecmd->lp_advertising = | ||
264 | mdio45_get_an(mdio, MDIO_AN_LPA) | npage_lpa; | ||
265 | if (an_stat & MDIO_AN_STAT1_LPABLE) | ||
266 | ecmd->lp_advertising |= ADVERTISED_Autoneg; | ||
267 | modes = ecmd->advertising & ecmd->lp_advertising; | ||
268 | } | ||
269 | if ((modes & ~ADVERTISED_Autoneg) == 0) | ||
270 | modes = ecmd->advertising; | ||
271 | |||
272 | if (modes & (ADVERTISED_10000baseT_Full | | ||
273 | ADVERTISED_10000baseKX4_Full | | ||
274 | ADVERTISED_10000baseKR_Full)) { | ||
275 | ecmd->speed = SPEED_10000; | ||
276 | ecmd->duplex = DUPLEX_FULL; | ||
277 | } else if (modes & (ADVERTISED_1000baseT_Full | | ||
278 | ADVERTISED_1000baseT_Half | | ||
279 | ADVERTISED_1000baseKX_Full)) { | ||
280 | ecmd->speed = SPEED_1000; | ||
281 | ecmd->duplex = !(modes & ADVERTISED_1000baseT_Half); | ||
282 | } else if (modes & (ADVERTISED_100baseT_Full | | ||
283 | ADVERTISED_100baseT_Half)) { | ||
284 | ecmd->speed = SPEED_100; | ||
285 | ecmd->duplex = !!(modes & ADVERTISED_100baseT_Full); | ||
286 | } else { | ||
287 | ecmd->speed = SPEED_10; | ||
288 | ecmd->duplex = !!(modes & ADVERTISED_10baseT_Full); | ||
289 | } | ||
290 | } else { | ||
291 | /* Report forced settings */ | ||
292 | reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, | ||
293 | MDIO_CTRL1); | ||
294 | ecmd->speed = (((reg & MDIO_PMA_CTRL1_SPEED1000) ? 100 : 1) * | ||
295 | ((reg & MDIO_PMA_CTRL1_SPEED100) ? 100 : 10)); | ||
296 | ecmd->duplex = (reg & MDIO_CTRL1_FULLDPLX || | ||
297 | ecmd->speed == SPEED_10000); | ||
298 | } | ||
299 | |||
300 | /* 10GBASE-T MDI/MDI-X */ | ||
301 | if (ecmd->port == PORT_TP && ecmd->speed == SPEED_10000) { | ||
302 | switch (mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, | ||
303 | MDIO_PMA_10GBT_SWAPPOL)) { | ||
304 | case MDIO_PMA_10GBT_SWAPPOL_ABNX | MDIO_PMA_10GBT_SWAPPOL_CDNX: | ||
305 | ecmd->eth_tp_mdix = ETH_TP_MDI; | ||
306 | break; | ||
307 | case 0: | ||
308 | ecmd->eth_tp_mdix = ETH_TP_MDI_X; | ||
309 | break; | ||
310 | default: | ||
311 | /* It's complicated... */ | ||
312 | ecmd->eth_tp_mdix = ETH_TP_MDI_INVALID; | ||
313 | break; | ||
314 | } | ||
315 | } | ||
316 | } | ||
317 | EXPORT_SYMBOL(mdio45_ethtool_gset_npage); | ||
318 | |||
319 | /** | ||
320 | * mdio45_ethtool_spauseparam_an - set auto-negotiated pause parameters | ||
321 | * @mdio: MDIO interface | ||
322 | * @ecmd: Ethtool request structure | ||
323 | * | ||
324 | * This function assumes that the PHY has an auto-negotiation MMD. It | ||
325 | * will enable and disable advertising of flow control as appropriate. | ||
326 | */ | ||
327 | void mdio45_ethtool_spauseparam_an(const struct mdio_if_info *mdio, | ||
328 | const struct ethtool_pauseparam *ecmd) | ||
329 | { | ||
330 | int adv, old_adv; | ||
331 | |||
332 | WARN_ON(!(mdio->mmds & MDIO_DEVS_AN)); | ||
333 | |||
334 | old_adv = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN, | ||
335 | MDIO_AN_ADVERTISE); | ||
336 | adv = old_adv & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM); | ||
337 | if (ecmd->autoneg) | ||
338 | adv |= mii_advertise_flowctrl( | ||
339 | (ecmd->rx_pause ? FLOW_CTRL_RX : 0) | | ||
340 | (ecmd->tx_pause ? FLOW_CTRL_TX : 0)); | ||
341 | if (adv != old_adv) { | ||
342 | mdio->mdio_write(mdio->dev, mdio->prtad, MDIO_MMD_AN, | ||
343 | MDIO_AN_ADVERTISE, adv); | ||
344 | mdio45_nway_restart(mdio); | ||
345 | } | ||
346 | } | ||
347 | EXPORT_SYMBOL(mdio45_ethtool_spauseparam_an); | ||
348 | |||
349 | /** | ||
350 | * mdio_mii_ioctl - MII ioctl interface for MDIO (clause 22 or 45) PHYs | ||
351 | * @mdio: MDIO interface | ||
352 | * @mii_data: MII ioctl data structure | ||
353 | * @cmd: MII ioctl command | ||
354 | * | ||
355 | * Returns 0 on success, negative on error. | ||
356 | */ | ||
357 | int mdio_mii_ioctl(const struct mdio_if_info *mdio, | ||
358 | struct mii_ioctl_data *mii_data, int cmd) | ||
359 | { | ||
360 | int prtad, devad; | ||
361 | u16 addr = mii_data->reg_num; | ||
362 | |||
363 | /* Validate/convert cmd to one of SIOC{G,S}MIIREG */ | ||
364 | switch (cmd) { | ||
365 | case SIOCGMIIPHY: | ||
366 | if (mdio->prtad == MDIO_PRTAD_NONE) | ||
367 | return -EOPNOTSUPP; | ||
368 | mii_data->phy_id = mdio->prtad; | ||
369 | cmd = SIOCGMIIREG; | ||
370 | break; | ||
371 | case SIOCGMIIREG: | ||
372 | break; | ||
373 | case SIOCSMIIREG: | ||
374 | if (!capable(CAP_NET_ADMIN)) | ||
375 | return -EPERM; | ||
376 | break; | ||
377 | default: | ||
378 | return -EOPNOTSUPP; | ||
379 | } | ||
380 | |||
381 | /* Validate/convert phy_id */ | ||
382 | if ((mdio->mode_support & MDIO_SUPPORTS_C45) && | ||
383 | mdio_phy_id_is_c45(mii_data->phy_id)) { | ||
384 | prtad = mdio_phy_id_prtad(mii_data->phy_id); | ||
385 | devad = mdio_phy_id_devad(mii_data->phy_id); | ||
386 | } else if ((mdio->mode_support & MDIO_SUPPORTS_C22) && | ||
387 | mii_data->phy_id < 0x20) { | ||
388 | prtad = mii_data->phy_id; | ||
389 | devad = MDIO_DEVAD_NONE; | ||
390 | addr &= 0x1f; | ||
391 | } else if ((mdio->mode_support & MDIO_EMULATE_C22) && | ||
392 | mdio->prtad != MDIO_PRTAD_NONE && | ||
393 | mii_data->phy_id == mdio->prtad) { | ||
394 | /* Remap commonly-used MII registers. */ | ||
395 | prtad = mdio->prtad; | ||
396 | switch (addr) { | ||
397 | case MII_BMCR: | ||
398 | case MII_BMSR: | ||
399 | case MII_PHYSID1: | ||
400 | case MII_PHYSID2: | ||
401 | devad = __ffs(mdio->mmds); | ||
402 | break; | ||
403 | case MII_ADVERTISE: | ||
404 | case MII_LPA: | ||
405 | if (!(mdio->mmds & MDIO_DEVS_AN)) | ||
406 | return -EINVAL; | ||
407 | devad = MDIO_MMD_AN; | ||
408 | if (addr == MII_ADVERTISE) | ||
409 | addr = MDIO_AN_ADVERTISE; | ||
410 | else | ||
411 | addr = MDIO_AN_LPA; | ||
412 | break; | ||
413 | default: | ||
414 | return -EINVAL; | ||
415 | } | ||
416 | } else { | ||
417 | return -EINVAL; | ||
418 | } | ||
419 | |||
420 | if (cmd == SIOCGMIIREG) { | ||
421 | int rc = mdio->mdio_read(mdio->dev, prtad, devad, addr); | ||
422 | if (rc < 0) | ||
423 | return rc; | ||
424 | mii_data->val_out = rc; | ||
425 | return 0; | ||
426 | } else { | ||
427 | return mdio->mdio_write(mdio->dev, prtad, devad, addr, | ||
428 | mii_data->val_in); | ||
429 | } | ||
430 | } | ||
431 | EXPORT_SYMBOL(mdio_mii_ioctl); | ||