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