mii_physubr.c revision 1.21 1 /* $NetBSD: mii_physubr.c,v 1.21 2001/04/30 19:49:08 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Subroutines common to all PHYs.
42 */
43
44 #include <sys/param.h>
45 #include <sys/device.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/errno.h>
50 #include <sys/proc.h>
51
52 #include <net/if.h>
53 #include <net/if_media.h>
54 #include <net/route.h>
55
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
58
59 /*
60 * Media to register setting conversion table. Order matters.
61 * XXX 802.3 doesn't specify ANAR or ANLPAR bits for 1000base.
62 */
63 const struct mii_media mii_media_table[] = {
64 { BMCR_ISO, ANAR_CSMA }, /* None */
65 { BMCR_S10, ANAR_CSMA|ANAR_10 }, /* 10baseT */
66 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD }, /* 10baseT-FDX */
67 { BMCR_S100, ANAR_CSMA|ANAR_T4 }, /* 100baseT4 */
68 { BMCR_S100, ANAR_CSMA|ANAR_TX }, /* 100baseTX */
69 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD }, /* 100baseTX-FDX */
70 { BMCR_S1000, ANAR_CSMA }, /* 1000base */
71 { BMCR_S1000|BMCR_FDX, ANAR_CSMA }, /* 1000base-FDX */
72 };
73
74 void mii_phy_auto_timeout __P((void *));
75
76 void
77 mii_phy_setmedia(sc)
78 struct mii_softc *sc;
79 {
80 struct mii_data *mii = sc->mii_pdata;
81 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
82 int bmcr, anar;
83
84 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
85 if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
86 (void) mii_phy_auto(sc, 1);
87 return;
88 }
89
90 /*
91 * Table index is stored in the media entry.
92 */
93
94 #ifdef DIAGNOSTIC
95 if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
96 panic("mii_phy_setmedia");
97 #endif
98
99 anar = mii_media_table[ife->ifm_data].mm_anar;
100 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
101
102 if (ife->ifm_media & IFM_LOOP)
103 bmcr |= BMCR_LOOP;
104
105 PHY_WRITE(sc, MII_ANAR, anar);
106 PHY_WRITE(sc, MII_BMCR, bmcr);
107 }
108
109 int
110 mii_phy_auto(sc, waitfor)
111 struct mii_softc *sc;
112 int waitfor;
113 {
114 int bmsr, i;
115
116 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
117 PHY_WRITE(sc, MII_ANAR,
118 BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA);
119 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
120 }
121
122 if (waitfor) {
123 /* Wait 500ms for it to complete. */
124 for (i = 0; i < 500; i++) {
125 if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
126 return (0);
127 delay(1000);
128 }
129
130 /*
131 * Don't need to worry about clearing MIIF_DOINGAUTO.
132 * If that's set, a timeout is pending, and it will
133 * clear the flag.
134 */
135 return (EIO);
136 }
137
138 /*
139 * Just let it finish asynchronously. This is for the benefit of
140 * the tick handler driving autonegotiation. Don't want 500ms
141 * delays all the time while the system is running!
142 */
143 if (sc->mii_flags & MIIF_AUTOTSLEEP) {
144 sc->mii_flags |= MIIF_DOINGAUTO;
145 tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
146 mii_phy_auto_timeout(sc);
147 } else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
148 sc->mii_flags |= MIIF_DOINGAUTO;
149 callout_reset(&sc->mii_nway_ch, hz >> 1,
150 mii_phy_auto_timeout, sc);
151 }
152 return (EJUSTRETURN);
153 }
154
155 void
156 mii_phy_auto_timeout(arg)
157 void *arg;
158 {
159 struct mii_softc *sc = arg;
160 int s, bmsr;
161
162 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
163 return;
164
165 s = splnet();
166 sc->mii_flags &= ~MIIF_DOINGAUTO;
167 bmsr = PHY_READ(sc, MII_BMSR);
168
169 /* Update the media status. */
170 (void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
171 splx(s);
172 }
173
174 int
175 mii_phy_tick(sc)
176 struct mii_softc *sc;
177 {
178 struct mii_data *mii = sc->mii_pdata;
179 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
180 int reg;
181
182 /* Just bail now if the interface is down. */
183 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
184 return (EJUSTRETURN);
185
186 /*
187 * If we're not doing autonegotiation, we don't need to do
188 * any extra work here. However, we need to check the link
189 * status so we can generate an announcement if the status
190 * changes.
191 */
192 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
193 return (0);
194
195 /* Read the status register twice; BMSR_LINK is latch-low. */
196 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
197 if (reg & BMSR_LINK) {
198 /*
199 * See above.
200 */
201 return (0);
202 }
203
204 /*
205 * Only retry autonegotiation every 5 seconds.
206 */
207 if (++sc->mii_ticks != 5)
208 return (EJUSTRETURN);
209
210 sc->mii_ticks = 0;
211 PHY_RESET(sc);
212
213 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
214 return (EJUSTRETURN);
215
216 /*
217 * Might need to generate a status message if autonegotiation
218 * failed.
219 */
220 return (0);
221 }
222
223 void
224 mii_phy_reset(sc)
225 struct mii_softc *sc;
226 {
227 int reg, i;
228
229 if (sc->mii_flags & MIIF_NOISOLATE)
230 reg = BMCR_RESET;
231 else
232 reg = BMCR_RESET | BMCR_ISO;
233 PHY_WRITE(sc, MII_BMCR, reg);
234
235 /* Wait 100ms for it to complete. */
236 for (i = 0; i < 100; i++) {
237 reg = PHY_READ(sc, MII_BMCR);
238 if ((reg & BMCR_RESET) == 0)
239 break;
240 delay(1000);
241 }
242
243 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
244 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
245 }
246
247 void
248 mii_phy_down(sc)
249 struct mii_softc *sc;
250 {
251
252 if (sc->mii_flags & MIIF_DOINGAUTO) {
253 sc->mii_flags &= ~MIIF_DOINGAUTO;
254 callout_stop(&sc->mii_nway_ch);
255 }
256 }
257
258 void
259 mii_phy_status(sc)
260 struct mii_softc *sc;
261 {
262
263 PHY_STATUS(sc);
264 }
265
266 void
267 mii_phy_update(sc, cmd)
268 struct mii_softc *sc;
269 int cmd;
270 {
271 struct mii_data *mii = sc->mii_pdata;
272
273 if (sc->mii_media_active != mii->mii_media_active ||
274 sc->mii_media_status != mii->mii_media_status ||
275 cmd == MII_MEDIACHG) {
276 (*mii->mii_statchg)(sc->mii_dev.dv_parent);
277 mii_phy_statusmsg(sc);
278 sc->mii_media_active = mii->mii_media_active;
279 sc->mii_media_status = mii->mii_media_status;
280 }
281 }
282
283 void
284 mii_phy_statusmsg(sc)
285 struct mii_softc *sc;
286 {
287 struct mii_data *mii = sc->mii_pdata;
288 struct ifnet *ifp = mii->mii_ifp;
289 int s, baudrate, link_state, announce = 0;
290
291 if (mii->mii_media_status & IFM_AVALID) {
292 if (mii->mii_media_status & IFM_ACTIVE)
293 link_state = LINK_STATE_UP;
294 else
295 link_state = LINK_STATE_DOWN;
296 } else
297 link_state = LINK_STATE_UNKNOWN;
298
299 baudrate = ifmedia_baudrate(mii->mii_media_active);
300
301 if (link_state != ifp->if_link_state) {
302 ifp->if_link_state = link_state;
303 /*
304 * XXX Right here we'd like to notify protocols
305 * XXX that the link status has changed, so that
306 * XXX e.g. Duplicate Address Detection can restart.
307 */
308 announce = 1;
309 }
310
311 if (baudrate != ifp->if_baudrate) {
312 ifp->if_baudrate = baudrate;
313 announce = 1;
314 }
315
316 if (announce) {
317 s = splnet();
318 rt_ifmsg(ifp);
319 splx(s);
320 }
321 }
322
323 /*
324 * Initialize generic PHY media based on BMSR, called when a PHY is
325 * attached. We expect to be set up to print a comma-separated list
326 * of media names. Does not print a newline.
327 */
328 void
329 mii_phy_add_media(sc)
330 struct mii_softc *sc;
331 {
332 struct mii_data *mii = sc->mii_pdata;
333 const char *sep = "";
334
335 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
336 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
337
338 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
339 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
340 MII_MEDIA_NONE);
341
342 if (sc->mii_capabilities & BMSR_10THDX) {
343 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
344 MII_MEDIA_10_T);
345 PRINT("10baseT");
346 }
347 if (sc->mii_capabilities & BMSR_10TFDX) {
348 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
349 MII_MEDIA_10_T_FDX);
350 PRINT("10baseT-FDX");
351 }
352 if (sc->mii_capabilities & BMSR_100TXHDX) {
353 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
354 MII_MEDIA_100_TX);
355 PRINT("100baseTX");
356 }
357 if (sc->mii_capabilities & BMSR_100TXFDX) {
358 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
359 MII_MEDIA_100_TX_FDX);
360 PRINT("100baseTX-FDX");
361 }
362 if (sc->mii_capabilities & BMSR_100T4) {
363 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
364 MII_MEDIA_100_T4);
365 PRINT("100baseT4");
366 }
367
368 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
369 /*
370 * XXX Right now only handle 1000SX and 1000TX. Need
371 * XXX to hnalde 1000LX and 1000CX some how.
372 */
373 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
374 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
375 sc->mii_inst), MII_MEDIA_1000);
376 PRINT("1000baseSX");
377 }
378 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
379 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
380 sc->mii_inst), MII_MEDIA_1000_FDX);
381 PRINT("1000baseSX-FDX");
382 }
383 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
384 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0,
385 sc->mii_inst), MII_MEDIA_1000);
386 PRINT("1000baseTX");
387 }
388 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
389 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX,
390 sc->mii_inst), MII_MEDIA_1000_FDX);
391 PRINT("1000baseTX-FDX");
392 }
393 }
394
395 if (sc->mii_capabilities & BMSR_ANEG) {
396 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
397 MII_NMEDIA); /* intentionally invalid index */
398 PRINT("auto");
399 }
400 #undef ADD
401 #undef PRINT
402 }
403
404 void
405 mii_phy_delete_media(sc)
406 struct mii_softc *sc;
407 {
408 struct mii_data *mii = sc->mii_pdata;
409
410 ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
411 }
412
413 int
414 mii_phy_activate(self, act)
415 struct device *self;
416 enum devact act;
417 {
418 int rv = 0;
419
420 switch (act) {
421 case DVACT_ACTIVATE:
422 rv = EOPNOTSUPP;
423 break;
424
425 case DVACT_DEACTIVATE:
426 /* Nothing special to do. */
427 break;
428 }
429
430 return (rv);
431 }
432
433 int
434 mii_phy_detach(self, flags)
435 struct device *self;
436 int flags;
437 {
438 struct mii_softc *sc = (void *) self;
439
440 if (sc->mii_flags & MIIF_DOINGAUTO)
441 callout_stop(&sc->mii_nway_ch);
442
443 mii_phy_delete_media(sc);
444
445 return (0);
446 }
447