mii_physubr.c revision 1.90 1 1.90 thorpej /* $NetBSD: mii_physubr.c,v 1.90 2020/03/15 23:04:50 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.23 thorpej * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej *
20 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.1 thorpej
33 1.1 thorpej /*
34 1.1 thorpej * Subroutines common to all PHYs.
35 1.1 thorpej */
36 1.30 lukem
37 1.30 lukem #include <sys/cdefs.h>
38 1.90 thorpej __KERNEL_RCSID(0, "$NetBSD: mii_physubr.c,v 1.90 2020/03/15 23:04:50 thorpej Exp $");
39 1.1 thorpej
40 1.1 thorpej #include <sys/param.h>
41 1.1 thorpej #include <sys/device.h>
42 1.1 thorpej #include <sys/systm.h>
43 1.3 thorpej #include <sys/kernel.h>
44 1.1 thorpej #include <sys/socket.h>
45 1.3 thorpej #include <sys/errno.h>
46 1.69 martin #include <sys/module.h>
47 1.19 augustss #include <sys/proc.h>
48 1.1 thorpej
49 1.1 thorpej #include <net/if.h>
50 1.1 thorpej #include <net/if_media.h>
51 1.15 thorpej #include <net/route.h>
52 1.1 thorpej
53 1.1 thorpej #include <dev/mii/mii.h>
54 1.1 thorpej #include <dev/mii/miivar.h>
55 1.1 thorpej
56 1.69 martin const char *(*mii_get_descr)(int, int) = mii_get_descr_stub;
57 1.69 martin
58 1.70 pgoyette int mii_verbose_loaded = 0;
59 1.70 pgoyette
60 1.85 msaitoh const char *
61 1.85 msaitoh mii_get_descr_stub(int oui, int model)
62 1.69 martin {
63 1.70 pgoyette mii_load_verbose();
64 1.70 pgoyette if (mii_verbose_loaded)
65 1.70 pgoyette return mii_get_descr(oui, model);
66 1.70 pgoyette else
67 1.70 pgoyette return NULL;
68 1.69 martin }
69 1.69 martin
70 1.83 msaitoh /*
71 1.70 pgoyette * Routine to load the miiverbose kernel module as needed
72 1.69 martin */
73 1.85 msaitoh void
74 1.85 msaitoh mii_load_verbose(void)
75 1.69 martin {
76 1.72 pgoyette if (mii_verbose_loaded == 0)
77 1.71 pgoyette module_autoload("miiverbose", MODULE_CLASS_MISC);
78 1.83 msaitoh }
79 1.69 martin
80 1.46 joerg static void mii_phy_statusmsg(struct mii_softc *);
81 1.46 joerg
82 1.6 thorpej /*
83 1.6 thorpej * Media to register setting conversion table. Order matters.
84 1.6 thorpej */
85 1.43 thorpej static const struct mii_media mii_media_table[MII_NMEDIA] = {
86 1.23 thorpej /* None */
87 1.23 thorpej { BMCR_ISO, ANAR_CSMA,
88 1.23 thorpej 0, },
89 1.23 thorpej
90 1.23 thorpej /* 10baseT */
91 1.86 msaitoh { BMCR_S10, ANAR_CSMA | ANAR_10,
92 1.23 thorpej 0, },
93 1.23 thorpej
94 1.23 thorpej /* 10baseT-FDX */
95 1.86 msaitoh { BMCR_S10|BMCR_FDX, ANAR_CSMA | ANAR_10_FD,
96 1.23 thorpej 0, },
97 1.23 thorpej
98 1.23 thorpej /* 100baseT4 */
99 1.86 msaitoh { BMCR_S100, ANAR_CSMA | ANAR_T4,
100 1.23 thorpej 0, },
101 1.23 thorpej
102 1.23 thorpej /* 100baseTX */
103 1.86 msaitoh { BMCR_S100, ANAR_CSMA | ANAR_TX,
104 1.23 thorpej 0, },
105 1.23 thorpej
106 1.23 thorpej /* 100baseTX-FDX */
107 1.86 msaitoh { BMCR_S100|BMCR_FDX, ANAR_CSMA | ANAR_TX_FD,
108 1.23 thorpej 0, },
109 1.23 thorpej
110 1.23 thorpej /* 1000baseX */
111 1.23 thorpej { BMCR_S1000, ANAR_CSMA,
112 1.23 thorpej 0, },
113 1.23 thorpej
114 1.23 thorpej /* 1000baseX-FDX */
115 1.23 thorpej { BMCR_S1000|BMCR_FDX, ANAR_CSMA,
116 1.23 thorpej 0, },
117 1.23 thorpej
118 1.23 thorpej /* 1000baseT */
119 1.23 thorpej { BMCR_S1000, ANAR_CSMA,
120 1.23 thorpej GTCR_ADV_1000THDX },
121 1.23 thorpej
122 1.23 thorpej /* 1000baseT-FDX */
123 1.23 thorpej { BMCR_S1000, ANAR_CSMA,
124 1.23 thorpej GTCR_ADV_1000TFDX },
125 1.6 thorpej };
126 1.6 thorpej
127 1.43 thorpej static void mii_phy_auto_timeout(void *);
128 1.90 thorpej static void mii_phy_auto_timeout_locked(struct mii_softc *);
129 1.3 thorpej
130 1.6 thorpej void
131 1.29 thorpej mii_phy_setmedia(struct mii_softc *sc)
132 1.6 thorpej {
133 1.6 thorpej struct mii_data *mii = sc->mii_pdata;
134 1.6 thorpej struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
135 1.84 msaitoh uint16_t bmcr, anar, gtcr;
136 1.12 thorpej
137 1.90 thorpej KASSERT(mii_locked(mii));
138 1.90 thorpej
139 1.13 thorpej if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
140 1.41 thorpej /*
141 1.41 thorpej * Force renegotiation if MIIF_DOPAUSE.
142 1.41 thorpej *
143 1.41 thorpej * XXX This is only necessary because many NICs don't
144 1.41 thorpej * XXX advertise PAUSE capabilities at boot time. Maybe
145 1.41 thorpej * XXX we should force this only once?
146 1.41 thorpej */
147 1.84 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
148 1.84 msaitoh if ((bmcr & BMCR_AUTOEN) == 0 ||
149 1.86 msaitoh (sc->mii_flags & (MIIF_FORCEANEG | MIIF_DOPAUSE)))
150 1.14 thorpej (void) mii_phy_auto(sc, 1);
151 1.13 thorpej return;
152 1.13 thorpej }
153 1.6 thorpej
154 1.85 msaitoh /* Table index is stored in the media entry. */
155 1.6 thorpej
156 1.6 thorpej #ifdef DIAGNOSTIC
157 1.50 christos if (/* ife->ifm_data < 0 || */ ife->ifm_data >= MII_NMEDIA)
158 1.6 thorpej panic("mii_phy_setmedia");
159 1.6 thorpej #endif
160 1.6 thorpej
161 1.6 thorpej anar = mii_media_table[ife->ifm_data].mm_anar;
162 1.6 thorpej bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
163 1.23 thorpej gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
164 1.23 thorpej
165 1.23 thorpej if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
166 1.23 thorpej switch (IFM_SUBTYPE(ife->ifm_media)) {
167 1.25 bjh21 case IFM_1000_T:
168 1.86 msaitoh gtcr |= GTCR_MAN_MS | GTCR_ADV_MS;
169 1.23 thorpej break;
170 1.23 thorpej
171 1.23 thorpej default:
172 1.23 thorpej panic("mii_phy_setmedia: MASTER on wrong media");
173 1.23 thorpej }
174 1.23 thorpej }
175 1.6 thorpej
176 1.38 thorpej if (mii->mii_media.ifm_media & IFM_FLOW) {
177 1.38 thorpej if (sc->mii_flags & MIIF_IS_1000X)
178 1.38 thorpej anar |= ANAR_X_PAUSE_SYM | ANAR_X_PAUSE_ASYM;
179 1.38 thorpej else {
180 1.38 thorpej anar |= ANAR_FC;
181 1.38 thorpej /* XXX Only 1000BASE-T has PAUSE_ASYM? */
182 1.38 thorpej if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
183 1.38 thorpej (sc->mii_extcapabilities &
184 1.79 msaitoh (EXTSR_1000THDX | EXTSR_1000TFDX)))
185 1.79 msaitoh anar |= ANAR_PAUSE_ASYM;
186 1.38 thorpej }
187 1.38 thorpej }
188 1.38 thorpej
189 1.6 thorpej if (ife->ifm_media & IFM_LOOP)
190 1.6 thorpej bmcr |= BMCR_LOOP;
191 1.6 thorpej
192 1.6 thorpej PHY_WRITE(sc, MII_ANAR, anar);
193 1.23 thorpej if (sc->mii_flags & MIIF_HAVE_GTCR)
194 1.23 thorpej PHY_WRITE(sc, MII_100T2CR, gtcr);
195 1.85 msaitoh if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T)
196 1.73 buhrow mii_phy_auto(sc, 0);
197 1.85 msaitoh else
198 1.73 buhrow PHY_WRITE(sc, MII_BMCR, bmcr);
199 1.23 thorpej }
200 1.23 thorpej
201 1.1 thorpej int
202 1.29 thorpej mii_phy_auto(struct mii_softc *sc, int waitfor)
203 1.1 thorpej {
204 1.31 uwe int i;
205 1.73 buhrow struct mii_data *mii = sc->mii_pdata;
206 1.73 buhrow struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
207 1.1 thorpej
208 1.90 thorpej KASSERT(mii_locked(mii));
209 1.90 thorpej
210 1.76 msaitoh sc->mii_ticks = 0;
211 1.6 thorpej if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
212 1.26 thorpej /*
213 1.26 thorpej * Check for 1000BASE-X. Autonegotiation is a bit
214 1.26 thorpej * different on such devices.
215 1.26 thorpej */
216 1.27 thorpej if (sc->mii_flags & MIIF_IS_1000X) {
217 1.27 thorpej uint16_t anar = 0;
218 1.27 thorpej
219 1.27 thorpej if (sc->mii_extcapabilities & EXTSR_1000XFDX)
220 1.27 thorpej anar |= ANAR_X_FD;
221 1.27 thorpej if (sc->mii_extcapabilities & EXTSR_1000XHDX)
222 1.27 thorpej anar |= ANAR_X_HD;
223 1.27 thorpej
224 1.27 thorpej if (sc->mii_flags & MIIF_DOPAUSE) {
225 1.27 thorpej /* XXX Asymmetric vs. symmetric? */
226 1.27 thorpej anar |= ANLPAR_X_PAUSE_TOWARDS;
227 1.27 thorpej }
228 1.27 thorpej
229 1.27 thorpej PHY_WRITE(sc, MII_ANAR, anar);
230 1.27 thorpej } else {
231 1.27 thorpej uint16_t anar;
232 1.27 thorpej
233 1.27 thorpej anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
234 1.27 thorpej ANAR_CSMA;
235 1.38 thorpej if (sc->mii_flags & MIIF_DOPAUSE) {
236 1.27 thorpej anar |= ANAR_FC;
237 1.38 thorpej /* XXX Only 1000BASE-T has PAUSE_ASYM? */
238 1.38 thorpej if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
239 1.38 thorpej (sc->mii_extcapabilities &
240 1.79 msaitoh (EXTSR_1000THDX | EXTSR_1000TFDX)))
241 1.79 msaitoh anar |= ANAR_PAUSE_ASYM;
242 1.38 thorpej }
243 1.73 buhrow
244 1.73 buhrow /*
245 1.81 msaitoh * For 1000-base-T, autonegotiation must be enabled,
246 1.81 msaitoh * but if we're not set to auto, only advertise
247 1.81 msaitoh * 1000-base-T with the link partner.
248 1.73 buhrow */
249 1.73 buhrow if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
250 1.85 msaitoh anar &= ~(ANAR_T4 | ANAR_TX_FD | ANAR_TX |
251 1.85 msaitoh ANAR_10_FD | ANAR_10);
252 1.73 buhrow }
253 1.83 msaitoh
254 1.27 thorpej PHY_WRITE(sc, MII_ANAR, anar);
255 1.27 thorpej if (sc->mii_flags & MIIF_HAVE_GTCR) {
256 1.27 thorpej uint16_t gtcr = 0;
257 1.27 thorpej
258 1.27 thorpej if (sc->mii_extcapabilities & EXTSR_1000TFDX)
259 1.27 thorpej gtcr |= GTCR_ADV_1000TFDX;
260 1.27 thorpej if (sc->mii_extcapabilities & EXTSR_1000THDX)
261 1.27 thorpej gtcr |= GTCR_ADV_1000THDX;
262 1.26 thorpej
263 1.27 thorpej PHY_WRITE(sc, MII_100T2CR, gtcr);
264 1.27 thorpej }
265 1.26 thorpej }
266 1.6 thorpej PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
267 1.3 thorpej }
268 1.3 thorpej
269 1.3 thorpej if (waitfor) {
270 1.3 thorpej /* Wait 500ms for it to complete. */
271 1.3 thorpej for (i = 0; i < 500; i++) {
272 1.84 msaitoh uint16_t bmsr;
273 1.84 msaitoh
274 1.84 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
275 1.84 msaitoh if (bmsr & BMSR_ACOMP)
276 1.85 msaitoh return 0;
277 1.3 thorpej delay(1000);
278 1.3 thorpej }
279 1.3 thorpej
280 1.3 thorpej /*
281 1.85 msaitoh * Don't need to worry about clearing MIIF_DOINGAUTO. If that's
282 1.85 msaitoh * set, a timeout is pending, and it will clear the flag.
283 1.3 thorpej */
284 1.85 msaitoh return EIO;
285 1.1 thorpej }
286 1.3 thorpej
287 1.3 thorpej /*
288 1.3 thorpej * Just let it finish asynchronously. This is for the benefit of
289 1.3 thorpej * the tick handler driving autonegotiation. Don't want 500ms
290 1.3 thorpej * delays all the time while the system is running!
291 1.3 thorpej */
292 1.19 augustss if (sc->mii_flags & MIIF_AUTOTSLEEP) {
293 1.90 thorpej ASSERT_SLEEPABLE();
294 1.19 augustss sc->mii_flags |= MIIF_DOINGAUTO;
295 1.90 thorpej kpause("miiaut", false, hz >> 1, mii->mii_media.ifm_lock);
296 1.90 thorpej mii_phy_auto_timeout_locked(sc);
297 1.19 augustss } else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
298 1.6 thorpej sc->mii_flags |= MIIF_DOINGAUTO;
299 1.17 thorpej callout_reset(&sc->mii_nway_ch, hz >> 1,
300 1.17 thorpej mii_phy_auto_timeout, sc);
301 1.3 thorpej }
302 1.85 msaitoh return EJUSTRETURN;
303 1.3 thorpej }
304 1.3 thorpej
305 1.43 thorpej static void
306 1.90 thorpej mii_phy_auto_timeout_locked(struct mii_softc *sc)
307 1.3 thorpej {
308 1.3 thorpej
309 1.60 xtraeme if (!device_is_active(sc->mii_dev))
310 1.9 thorpej return;
311 1.90 thorpej
312 1.6 thorpej sc->mii_flags &= ~MIIF_DOINGAUTO;
313 1.3 thorpej
314 1.3 thorpej /* Update the media status. */
315 1.18 thorpej (void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
316 1.90 thorpej }
317 1.90 thorpej
318 1.90 thorpej static void
319 1.90 thorpej mii_phy_auto_timeout(void *arg)
320 1.90 thorpej {
321 1.90 thorpej struct mii_softc *sc = arg;
322 1.90 thorpej
323 1.90 thorpej if (!device_is_active(sc->mii_dev))
324 1.90 thorpej return;
325 1.90 thorpej
326 1.90 thorpej mii_lock(sc->mii_pdata);
327 1.90 thorpej mii_phy_auto_timeout_locked(sc);
328 1.90 thorpej mii_unlock(sc->mii_pdata);
329 1.2 thorpej }
330 1.2 thorpej
331 1.15 thorpej int
332 1.29 thorpej mii_phy_tick(struct mii_softc *sc)
333 1.15 thorpej {
334 1.15 thorpej struct mii_data *mii = sc->mii_pdata;
335 1.15 thorpej struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
336 1.84 msaitoh uint16_t reg;
337 1.15 thorpej
338 1.90 thorpej KASSERT(mii_locked(mii));
339 1.90 thorpej
340 1.15 thorpej /* Just bail now if the interface is down. */
341 1.15 thorpej if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
342 1.85 msaitoh return EJUSTRETURN;
343 1.15 thorpej
344 1.15 thorpej /*
345 1.85 msaitoh * If we're not doing autonegotiation, we don't need to do any extra
346 1.85 msaitoh * work here. However, we need to check the link status so we can
347 1.85 msaitoh * generate an announcement by returning with 0 if the status changes.
348 1.15 thorpej */
349 1.73 buhrow if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
350 1.77 msaitoh (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
351 1.77 msaitoh /*
352 1.77 msaitoh * Reset autonegotiation timer to 0 just to make sure
353 1.77 msaitoh * the future autonegotiation start with 0.
354 1.77 msaitoh */
355 1.77 msaitoh sc->mii_ticks = 0;
356 1.85 msaitoh return 0;
357 1.77 msaitoh }
358 1.15 thorpej
359 1.15 thorpej /* Read the status register twice; BMSR_LINK is latch-low. */
360 1.84 msaitoh PHY_READ(sc, MII_BMSR, ®);
361 1.84 msaitoh PHY_READ(sc, MII_BMSR, ®);
362 1.15 thorpej if (reg & BMSR_LINK) {
363 1.15 thorpej /*
364 1.77 msaitoh * Reset autonegotiation timer to 0 in case the link
365 1.77 msaitoh * goes down in the next tick.
366 1.15 thorpej */
367 1.77 msaitoh sc->mii_ticks = 0;
368 1.77 msaitoh /* See above. */
369 1.85 msaitoh return 0;
370 1.15 thorpej }
371 1.15 thorpej
372 1.15 thorpej /*
373 1.78 msaitoh * mii_ticks == 0 means it's the first tick after changing the media or
374 1.77 msaitoh * the link became down since the last tick (see above), so return with
375 1.77 msaitoh * 0 to update the status.
376 1.77 msaitoh */
377 1.78 msaitoh if (sc->mii_ticks++ == 0)
378 1.85 msaitoh return 0;
379 1.77 msaitoh
380 1.77 msaitoh /*
381 1.22 thorpej * Only retry autonegotiation every N seconds.
382 1.15 thorpej */
383 1.22 thorpej KASSERT(sc->mii_anegticks != 0);
384 1.77 msaitoh if (sc->mii_ticks <= sc->mii_anegticks)
385 1.85 msaitoh return EJUSTRETURN;
386 1.15 thorpej
387 1.18 thorpej PHY_RESET(sc);
388 1.15 thorpej
389 1.15 thorpej if (mii_phy_auto(sc, 0) == EJUSTRETURN)
390 1.85 msaitoh return EJUSTRETURN;
391 1.15 thorpej
392 1.15 thorpej /*
393 1.15 thorpej * Might need to generate a status message if autonegotiation
394 1.15 thorpej * failed.
395 1.15 thorpej */
396 1.85 msaitoh return 0;
397 1.15 thorpej }
398 1.15 thorpej
399 1.2 thorpej void
400 1.29 thorpej mii_phy_reset(struct mii_softc *sc)
401 1.2 thorpej {
402 1.84 msaitoh int i;
403 1.84 msaitoh uint16_t reg;
404 1.2 thorpej
405 1.90 thorpej KASSERT(mii_locked(sc->mii_pdata));
406 1.90 thorpej
407 1.6 thorpej if (sc->mii_flags & MIIF_NOISOLATE)
408 1.2 thorpej reg = BMCR_RESET;
409 1.2 thorpej else
410 1.2 thorpej reg = BMCR_RESET | BMCR_ISO;
411 1.6 thorpej PHY_WRITE(sc, MII_BMCR, reg);
412 1.2 thorpej
413 1.82 msaitoh /* Wait another 500ms for it to complete. */
414 1.82 msaitoh for (i = 0; i < 500; i++) {
415 1.84 msaitoh PHY_READ(sc, MII_BMCR, ®);
416 1.2 thorpej if ((reg & BMCR_RESET) == 0)
417 1.2 thorpej break;
418 1.2 thorpej delay(1000);
419 1.2 thorpej }
420 1.2 thorpej
421 1.6 thorpej if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
422 1.6 thorpej PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
423 1.8 thorpej }
424 1.8 thorpej
425 1.8 thorpej void
426 1.29 thorpej mii_phy_down(struct mii_softc *sc)
427 1.8 thorpej {
428 1.8 thorpej
429 1.90 thorpej KASSERT(mii_locked(sc->mii_pdata));
430 1.90 thorpej
431 1.8 thorpej if (sc->mii_flags & MIIF_DOINGAUTO) {
432 1.8 thorpej sc->mii_flags &= ~MIIF_DOINGAUTO;
433 1.17 thorpej callout_stop(&sc->mii_nway_ch);
434 1.8 thorpej }
435 1.11 thorpej }
436 1.11 thorpej
437 1.11 thorpej void
438 1.29 thorpej mii_phy_status(struct mii_softc *sc)
439 1.11 thorpej {
440 1.11 thorpej
441 1.90 thorpej KASSERT(mii_locked(sc->mii_pdata));
442 1.18 thorpej PHY_STATUS(sc);
443 1.15 thorpej }
444 1.15 thorpej
445 1.15 thorpej void
446 1.29 thorpej mii_phy_update(struct mii_softc *sc, int cmd)
447 1.15 thorpej {
448 1.15 thorpej struct mii_data *mii = sc->mii_pdata;
449 1.15 thorpej
450 1.90 thorpej KASSERT(mii_locked(mii));
451 1.90 thorpej
452 1.15 thorpej if (sc->mii_media_active != mii->mii_media_active ||
453 1.15 thorpej sc->mii_media_status != mii->mii_media_status ||
454 1.15 thorpej cmd == MII_MEDIACHG) {
455 1.46 joerg mii_phy_statusmsg(sc);
456 1.74 matt (*mii->mii_statchg)(mii->mii_ifp);
457 1.15 thorpej sc->mii_media_active = mii->mii_media_active;
458 1.15 thorpej sc->mii_media_status = mii->mii_media_status;
459 1.15 thorpej }
460 1.15 thorpej }
461 1.15 thorpej
462 1.46 joerg static void
463 1.29 thorpej mii_phy_statusmsg(struct mii_softc *sc)
464 1.15 thorpej {
465 1.15 thorpej struct mii_data *mii = sc->mii_pdata;
466 1.15 thorpej struct ifnet *ifp = mii->mii_ifp;
467 1.15 thorpej
468 1.90 thorpej KASSERT(mii_locked(mii));
469 1.90 thorpej
470 1.15 thorpej if (mii->mii_media_status & IFM_AVALID) {
471 1.15 thorpej if (mii->mii_media_status & IFM_ACTIVE)
472 1.46 joerg if_link_state_change(ifp, LINK_STATE_UP);
473 1.15 thorpej else
474 1.46 joerg if_link_state_change(ifp, LINK_STATE_DOWN);
475 1.15 thorpej } else
476 1.46 joerg if_link_state_change(ifp, LINK_STATE_UNKNOWN);
477 1.15 thorpej
478 1.90 thorpej /* XXX NET_MPSAFE */
479 1.46 joerg ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
480 1.5 drochner }
481 1.5 drochner
482 1.5 drochner /*
483 1.5 drochner * Initialize generic PHY media based on BMSR, called when a PHY is
484 1.5 drochner * attached. We expect to be set up to print a comma-separated list
485 1.5 drochner * of media names. Does not print a newline.
486 1.5 drochner */
487 1.5 drochner void
488 1.29 thorpej mii_phy_add_media(struct mii_softc *sc)
489 1.5 drochner {
490 1.6 thorpej struct mii_data *mii = sc->mii_pdata;
491 1.61 dyoung device_t self = sc->mii_dev;
492 1.5 drochner const char *sep = "";
493 1.38 thorpej int fdx = 0;
494 1.5 drochner
495 1.89 msaitoh aprint_normal_dev(self, "");
496 1.89 msaitoh if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
497 1.89 msaitoh (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) {
498 1.89 msaitoh aprint_error("no media present\n");
499 1.89 msaitoh goto out;
500 1.89 msaitoh }
501 1.89 msaitoh
502 1.89 msaitoh /*
503 1.89 msaitoh * Set the autonegotiation timer for 10/100 media. Gigabit media is
504 1.89 msaitoh * handled below.
505 1.89 msaitoh */
506 1.90 thorpej mii_lock(mii);
507 1.89 msaitoh sc->mii_anegticks = MII_ANEGTICKS;
508 1.90 thorpej mii_unlock(mii);
509 1.89 msaitoh
510 1.5 drochner #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
511 1.36 thorpej #define PRINT(n) aprint_normal("%s%s", sep, (n)); sep = ", "
512 1.5 drochner
513 1.90 thorpej /* This flag is static; no need to lock. */
514 1.6 thorpej if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
515 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
516 1.6 thorpej MII_MEDIA_NONE);
517 1.28 thorpej
518 1.28 thorpej /*
519 1.28 thorpej * There are different interpretations for the bits in
520 1.28 thorpej * HomePNA PHYs. And there is really only one media type
521 1.90 thorpej * that is supported. This flag is also static, and so
522 1.90 thorpej * no need to lock.
523 1.28 thorpej */
524 1.28 thorpej if (sc->mii_flags & MIIF_IS_HPNA) {
525 1.28 thorpej if (sc->mii_capabilities & BMSR_10THDX) {
526 1.28 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
527 1.28 thorpej sc->mii_inst),
528 1.28 thorpej MII_MEDIA_10_T);
529 1.28 thorpej PRINT("HomePNA1");
530 1.28 thorpej }
531 1.61 dyoung goto out;
532 1.28 thorpej }
533 1.6 thorpej
534 1.6 thorpej if (sc->mii_capabilities & BMSR_10THDX) {
535 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
536 1.6 thorpej MII_MEDIA_10_T);
537 1.5 drochner PRINT("10baseT");
538 1.5 drochner }
539 1.6 thorpej if (sc->mii_capabilities & BMSR_10TFDX) {
540 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
541 1.6 thorpej MII_MEDIA_10_T_FDX);
542 1.5 drochner PRINT("10baseT-FDX");
543 1.38 thorpej fdx = 1;
544 1.5 drochner }
545 1.6 thorpej if (sc->mii_capabilities & BMSR_100TXHDX) {
546 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
547 1.6 thorpej MII_MEDIA_100_TX);
548 1.5 drochner PRINT("100baseTX");
549 1.5 drochner }
550 1.6 thorpej if (sc->mii_capabilities & BMSR_100TXFDX) {
551 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
552 1.6 thorpej MII_MEDIA_100_TX_FDX);
553 1.5 drochner PRINT("100baseTX-FDX");
554 1.38 thorpej fdx = 1;
555 1.5 drochner }
556 1.6 thorpej if (sc->mii_capabilities & BMSR_100T4) {
557 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
558 1.6 thorpej MII_MEDIA_100_T4);
559 1.5 drochner PRINT("100baseT4");
560 1.5 drochner }
561 1.21 thorpej
562 1.21 thorpej if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
563 1.21 thorpej /*
564 1.21 thorpej * XXX Right now only handle 1000SX and 1000TX. Need
565 1.22 thorpej * XXX to handle 1000LX and 1000CX some how.
566 1.22 thorpej *
567 1.22 thorpej * Note since it can take 5 seconds to auto-negotiate
568 1.22 thorpej * a gigabit link, we make anegticks 10 seconds for
569 1.22 thorpej * all the gigabit media types.
570 1.21 thorpej */
571 1.21 thorpej if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
572 1.90 thorpej mii_lock(mii);
573 1.53 christos sc->mii_anegticks = MII_ANEGTICKS_GIGE;
574 1.27 thorpej sc->mii_flags |= MIIF_IS_1000X;
575 1.90 thorpej mii_unlock(mii);
576 1.21 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
577 1.23 thorpej sc->mii_inst), MII_MEDIA_1000_X);
578 1.21 thorpej PRINT("1000baseSX");
579 1.21 thorpej }
580 1.21 thorpej if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
581 1.90 thorpej mii_lock(mii);
582 1.53 christos sc->mii_anegticks = MII_ANEGTICKS_GIGE;
583 1.27 thorpej sc->mii_flags |= MIIF_IS_1000X;
584 1.90 thorpej mii_unlock(mii);
585 1.21 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
586 1.23 thorpej sc->mii_inst), MII_MEDIA_1000_X_FDX);
587 1.21 thorpej PRINT("1000baseSX-FDX");
588 1.38 thorpej fdx = 1;
589 1.21 thorpej }
590 1.23 thorpej
591 1.23 thorpej /*
592 1.23 thorpej * 1000baseT media needs to be able to manipulate
593 1.23 thorpej * master/slave mode. We set IFM_ETH_MASTER in
594 1.23 thorpej * the "don't care mask" and filter it out when
595 1.23 thorpej * the media is set.
596 1.23 thorpej *
597 1.23 thorpej * All 1000baseT PHYs have a 1000baseT control register.
598 1.23 thorpej */
599 1.21 thorpej if (sc->mii_extcapabilities & EXTSR_1000THDX) {
600 1.90 thorpej mii_lock(mii);
601 1.53 christos sc->mii_anegticks = MII_ANEGTICKS_GIGE;
602 1.23 thorpej sc->mii_flags |= MIIF_HAVE_GTCR;
603 1.23 thorpej mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
604 1.90 thorpej mii_unlock(mii);
605 1.25 bjh21 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
606 1.23 thorpej sc->mii_inst), MII_MEDIA_1000_T);
607 1.25 bjh21 PRINT("1000baseT");
608 1.21 thorpej }
609 1.21 thorpej if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
610 1.90 thorpej mii_lock(mii);
611 1.53 christos sc->mii_anegticks = MII_ANEGTICKS_GIGE;
612 1.23 thorpej sc->mii_flags |= MIIF_HAVE_GTCR;
613 1.23 thorpej mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
614 1.90 thorpej mii_unlock(mii);
615 1.25 bjh21 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
616 1.23 thorpej sc->mii_inst), MII_MEDIA_1000_T_FDX);
617 1.25 bjh21 PRINT("1000baseT-FDX");
618 1.38 thorpej fdx = 1;
619 1.21 thorpej }
620 1.21 thorpej }
621 1.21 thorpej
622 1.6 thorpej if (sc->mii_capabilities & BMSR_ANEG) {
623 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
624 1.6 thorpej MII_NMEDIA); /* intentionally invalid index */
625 1.5 drochner PRINT("auto");
626 1.5 drochner }
627 1.5 drochner #undef ADD
628 1.5 drochner #undef PRINT
629 1.90 thorpej /* This flag is static; no need to lock. */
630 1.90 thorpej if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE)) {
631 1.90 thorpej mii_lock(mii);
632 1.38 thorpej mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
633 1.90 thorpej mii_unlock(mii);
634 1.90 thorpej }
635 1.61 dyoung out:
636 1.89 msaitoh aprint_normal("\n");
637 1.61 dyoung if (!pmf_device_register(self, NULL, mii_phy_resume)) {
638 1.89 msaitoh aprint_error_dev(self, "couldn't establish power handler\n");
639 1.61 dyoung }
640 1.9 thorpej }
641 1.9 thorpej
642 1.9 thorpej void
643 1.29 thorpej mii_phy_delete_media(struct mii_softc *sc)
644 1.9 thorpej {
645 1.9 thorpej struct mii_data *mii = sc->mii_pdata;
646 1.9 thorpej
647 1.9 thorpej ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
648 1.9 thorpej }
649 1.9 thorpej
650 1.9 thorpej int
651 1.64 cegger mii_phy_activate(device_t self, enum devact act)
652 1.9 thorpej {
653 1.86 msaitoh
654 1.9 thorpej switch (act) {
655 1.9 thorpej case DVACT_DEACTIVATE:
656 1.65 dyoung /* XXX Invalidate parent's media setting? */
657 1.65 dyoung return 0;
658 1.65 dyoung default:
659 1.65 dyoung return EOPNOTSUPP;
660 1.9 thorpej }
661 1.9 thorpej }
662 1.9 thorpej
663 1.31 uwe /* ARGSUSED1 */
664 1.9 thorpej int
665 1.64 cegger mii_phy_detach(device_t self, int flags)
666 1.9 thorpej {
667 1.49 thorpej struct mii_softc *sc = device_private(self);
668 1.9 thorpej
669 1.90 thorpej mii_lock(sc->mii_pdata);
670 1.90 thorpej if (sc->mii_flags & MIIF_DOINGAUTO) {
671 1.90 thorpej callout_halt(&sc->mii_nway_ch,
672 1.90 thorpej sc->mii_pdata->mii_media.ifm_lock);
673 1.90 thorpej }
674 1.90 thorpej mii_unlock(sc->mii_pdata);
675 1.9 thorpej
676 1.63 dyoung callout_destroy(&sc->mii_nway_ch);
677 1.63 dyoung
678 1.10 thorpej mii_phy_delete_media(sc);
679 1.9 thorpej
680 1.85 msaitoh return 0;
681 1.24 thorpej }
682 1.24 thorpej
683 1.24 thorpej const struct mii_phydesc *
684 1.24 thorpej mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
685 1.24 thorpej {
686 1.24 thorpej
687 1.24 thorpej for (; mpd->mpd_name != NULL; mpd++) {
688 1.24 thorpej if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
689 1.24 thorpej MII_MODEL(ma->mii_id2) == mpd->mpd_model)
690 1.85 msaitoh return mpd;
691 1.24 thorpej }
692 1.85 msaitoh return NULL;
693 1.1 thorpej }
694 1.38 thorpej
695 1.38 thorpej /*
696 1.38 thorpej * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
697 1.38 thorpej */
698 1.38 thorpej u_int
699 1.40 thorpej mii_phy_flowstatus(struct mii_softc *sc)
700 1.38 thorpej {
701 1.84 msaitoh uint16_t anar, anlpar;
702 1.38 thorpej
703 1.90 thorpej KASSERT(mii_locked(sc->mii_pdata));
704 1.90 thorpej
705 1.39 thorpej if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
706 1.85 msaitoh return 0;
707 1.39 thorpej
708 1.84 msaitoh PHY_READ(sc, MII_ANAR, &anar);
709 1.84 msaitoh PHY_READ(sc, MII_ANLPAR, &anlpar);
710 1.38 thorpej
711 1.79 msaitoh /* For 1000baseX, the bits are in a different location. */
712 1.79 msaitoh if (sc->mii_flags & MIIF_IS_1000X) {
713 1.79 msaitoh anar <<= 3;
714 1.79 msaitoh anlpar <<= 3;
715 1.79 msaitoh }
716 1.79 msaitoh
717 1.79 msaitoh if ((anar & ANAR_PAUSE_SYM) & (anlpar & ANLPAR_PAUSE_SYM))
718 1.85 msaitoh return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
719 1.42 thorpej
720 1.79 msaitoh if ((anar & ANAR_PAUSE_SYM) == 0) {
721 1.79 msaitoh if ((anar & ANAR_PAUSE_ASYM) &&
722 1.79 msaitoh ((anlpar & ANLPAR_PAUSE_TOWARDS) == ANLPAR_PAUSE_TOWARDS))
723 1.85 msaitoh return (IFM_FLOW | IFM_ETH_TXPAUSE);
724 1.38 thorpej else
725 1.85 msaitoh return 0;
726 1.38 thorpej }
727 1.38 thorpej
728 1.79 msaitoh if ((anar & ANAR_PAUSE_ASYM) == 0) {
729 1.79 msaitoh if (anlpar & ANLPAR_PAUSE_SYM)
730 1.85 msaitoh return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
731 1.38 thorpej else
732 1.85 msaitoh return 0;
733 1.38 thorpej }
734 1.38 thorpej
735 1.79 msaitoh switch ((anlpar & ANLPAR_PAUSE_TOWARDS)) {
736 1.79 msaitoh case ANLPAR_PAUSE_NONE:
737 1.85 msaitoh return 0;
738 1.44 perry
739 1.79 msaitoh case ANLPAR_PAUSE_ASYM:
740 1.85 msaitoh return (IFM_FLOW | IFM_ETH_RXPAUSE);
741 1.44 perry
742 1.38 thorpej default:
743 1.85 msaitoh return (IFM_FLOW | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
744 1.38 thorpej }
745 1.38 thorpej /* NOTREACHED */
746 1.38 thorpej }
747 1.54 jmcneill
748 1.54 jmcneill bool
749 1.67 dyoung mii_phy_resume(device_t dv, const pmf_qual_t *qual)
750 1.54 jmcneill {
751 1.54 jmcneill struct mii_softc *sc = device_private(dv);
752 1.54 jmcneill
753 1.90 thorpej mii_lock(sc->mii_pdata);
754 1.54 jmcneill PHY_RESET(sc);
755 1.90 thorpej bool rv = PHY_SERVICE(sc, sc->mii_pdata, MII_MEDIACHG) == 0;
756 1.90 thorpej mii_unlock(sc->mii_pdata);
757 1.90 thorpej
758 1.90 thorpej return rv;
759 1.54 jmcneill }
760 1.62 cegger
761 1.62 cegger
762 1.62 cegger /*
763 1.88 msaitoh * Given an ifmedia_entry, return the corresponding ANAR value.
764 1.62 cegger */
765 1.87 msaitoh uint16_t
766 1.88 msaitoh mii_anar(struct ifmedia_entry *ife)
767 1.62 cegger {
768 1.62 cegger
769 1.75 mlelstv #ifdef DIAGNOSTIC
770 1.88 msaitoh if (ife->ifm_data >= MII_NMEDIA)
771 1.75 mlelstv panic("mii_anar");
772 1.75 mlelstv #endif
773 1.75 mlelstv
774 1.88 msaitoh return mii_media_table[ife->ifm_data].mm_anar;
775 1.62 cegger }
776