mii_physubr.c revision 1.29 1 /* $NetBSD: mii_physubr.c,v 1.29 2001/08/25 18:04:01 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2000, 2001 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 */
62 const struct mii_media mii_media_table[MII_NMEDIA] = {
63 /* None */
64 { BMCR_ISO, ANAR_CSMA,
65 0, },
66
67 /* 10baseT */
68 { BMCR_S10, ANAR_CSMA|ANAR_10,
69 0, },
70
71 /* 10baseT-FDX */
72 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD,
73 0, },
74
75 /* 100baseT4 */
76 { BMCR_S100, ANAR_CSMA|ANAR_T4,
77 0, },
78
79 /* 100baseTX */
80 { BMCR_S100, ANAR_CSMA|ANAR_TX,
81 0, },
82
83 /* 100baseTX-FDX */
84 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD,
85 0, },
86
87 /* 1000baseX */
88 { BMCR_S1000, ANAR_CSMA,
89 0, },
90
91 /* 1000baseX-FDX */
92 { BMCR_S1000|BMCR_FDX, ANAR_CSMA,
93 0, },
94
95 /* 1000baseT */
96 { BMCR_S1000, ANAR_CSMA,
97 GTCR_ADV_1000THDX },
98
99 /* 1000baseT-FDX */
100 { BMCR_S1000, ANAR_CSMA,
101 GTCR_ADV_1000TFDX },
102 };
103
104 void mii_phy_auto_timeout(void *);
105
106 void
107 mii_phy_setmedia(struct mii_softc *sc)
108 {
109 struct mii_data *mii = sc->mii_pdata;
110 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
111 int bmcr, anar, gtcr;
112
113 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
114 if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
115 (void) mii_phy_auto(sc, 1);
116 return;
117 }
118
119 /*
120 * Table index is stored in the media entry.
121 */
122
123 #ifdef DIAGNOSTIC
124 if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
125 panic("mii_phy_setmedia");
126 #endif
127
128 anar = mii_media_table[ife->ifm_data].mm_anar;
129 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
130 gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
131
132 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
133 switch (IFM_SUBTYPE(ife->ifm_media)) {
134 case IFM_1000_T:
135 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
136 break;
137
138 default:
139 panic("mii_phy_setmedia: MASTER on wrong media");
140 }
141 }
142
143 if (ife->ifm_media & IFM_LOOP)
144 bmcr |= BMCR_LOOP;
145
146 PHY_WRITE(sc, MII_ANAR, anar);
147 PHY_WRITE(sc, MII_BMCR, bmcr);
148 if (sc->mii_flags & MIIF_HAVE_GTCR)
149 PHY_WRITE(sc, MII_100T2CR, gtcr);
150 }
151
152 int
153 mii_phy_auto(struct mii_softc *sc, int waitfor)
154 {
155 int bmsr, i;
156
157 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
158 /*
159 * Check for 1000BASE-X. Autonegotiation is a bit
160 * different on such devices.
161 */
162 if (sc->mii_flags & MIIF_IS_1000X) {
163 uint16_t anar = 0;
164
165 if (sc->mii_extcapabilities & EXTSR_1000XFDX)
166 anar |= ANAR_X_FD;
167 if (sc->mii_extcapabilities & EXTSR_1000XHDX)
168 anar |= ANAR_X_HD;
169
170 if (sc->mii_flags & MIIF_DOPAUSE) {
171 /* XXX Asymmetric vs. symmetric? */
172 anar |= ANLPAR_X_PAUSE_TOWARDS;
173 }
174
175 PHY_WRITE(sc, MII_ANAR, anar);
176 } else {
177 uint16_t anar;
178
179 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
180 ANAR_CSMA;
181 if (sc->mii_flags & MIIF_DOPAUSE)
182 anar |= ANAR_FC;
183 PHY_WRITE(sc, MII_ANAR, anar);
184 if (sc->mii_flags & MIIF_HAVE_GTCR) {
185 uint16_t gtcr = 0;
186
187 if (sc->mii_extcapabilities & EXTSR_1000TFDX)
188 gtcr |= GTCR_ADV_1000TFDX;
189 if (sc->mii_extcapabilities & EXTSR_1000THDX)
190 gtcr |= GTCR_ADV_1000THDX;
191
192 PHY_WRITE(sc, MII_100T2CR, gtcr);
193 }
194 }
195 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
196 }
197
198 if (waitfor) {
199 /* Wait 500ms for it to complete. */
200 for (i = 0; i < 500; i++) {
201 if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
202 return (0);
203 delay(1000);
204 }
205
206 /*
207 * Don't need to worry about clearing MIIF_DOINGAUTO.
208 * If that's set, a timeout is pending, and it will
209 * clear the flag.
210 */
211 return (EIO);
212 }
213
214 /*
215 * Just let it finish asynchronously. This is for the benefit of
216 * the tick handler driving autonegotiation. Don't want 500ms
217 * delays all the time while the system is running!
218 */
219 if (sc->mii_flags & MIIF_AUTOTSLEEP) {
220 sc->mii_flags |= MIIF_DOINGAUTO;
221 tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
222 mii_phy_auto_timeout(sc);
223 } else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
224 sc->mii_flags |= MIIF_DOINGAUTO;
225 callout_reset(&sc->mii_nway_ch, hz >> 1,
226 mii_phy_auto_timeout, sc);
227 }
228 return (EJUSTRETURN);
229 }
230
231 void
232 mii_phy_auto_timeout(void *arg)
233 {
234 struct mii_softc *sc = arg;
235 int s, bmsr;
236
237 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
238 return;
239
240 s = splnet();
241 sc->mii_flags &= ~MIIF_DOINGAUTO;
242 bmsr = PHY_READ(sc, MII_BMSR);
243
244 /* Update the media status. */
245 (void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
246 splx(s);
247 }
248
249 int
250 mii_phy_tick(struct mii_softc *sc)
251 {
252 struct mii_data *mii = sc->mii_pdata;
253 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
254 int reg;
255
256 /* Just bail now if the interface is down. */
257 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
258 return (EJUSTRETURN);
259
260 /*
261 * If we're not doing autonegotiation, we don't need to do
262 * any extra work here. However, we need to check the link
263 * status so we can generate an announcement if the status
264 * changes.
265 */
266 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
267 return (0);
268
269 /* Read the status register twice; BMSR_LINK is latch-low. */
270 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
271 if (reg & BMSR_LINK) {
272 /*
273 * See above.
274 */
275 return (0);
276 }
277
278 /*
279 * Only retry autonegotiation every N seconds.
280 */
281 KASSERT(sc->mii_anegticks != 0);
282 if (++sc->mii_ticks != sc->mii_anegticks)
283 return (EJUSTRETURN);
284
285 sc->mii_ticks = 0;
286 PHY_RESET(sc);
287
288 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
289 return (EJUSTRETURN);
290
291 /*
292 * Might need to generate a status message if autonegotiation
293 * failed.
294 */
295 return (0);
296 }
297
298 void
299 mii_phy_reset(struct mii_softc *sc)
300 {
301 int reg, i;
302
303 if (sc->mii_flags & MIIF_NOISOLATE)
304 reg = BMCR_RESET;
305 else
306 reg = BMCR_RESET | BMCR_ISO;
307 PHY_WRITE(sc, MII_BMCR, reg);
308
309 /* Wait 100ms for it to complete. */
310 for (i = 0; i < 100; i++) {
311 reg = PHY_READ(sc, MII_BMCR);
312 if ((reg & BMCR_RESET) == 0)
313 break;
314 delay(1000);
315 }
316
317 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
318 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
319 }
320
321 void
322 mii_phy_down(struct mii_softc *sc)
323 {
324
325 if (sc->mii_flags & MIIF_DOINGAUTO) {
326 sc->mii_flags &= ~MIIF_DOINGAUTO;
327 callout_stop(&sc->mii_nway_ch);
328 }
329 }
330
331 void
332 mii_phy_status(struct mii_softc *sc)
333 {
334
335 PHY_STATUS(sc);
336 }
337
338 void
339 mii_phy_update(struct mii_softc *sc, int cmd)
340 {
341 struct mii_data *mii = sc->mii_pdata;
342
343 if (sc->mii_media_active != mii->mii_media_active ||
344 sc->mii_media_status != mii->mii_media_status ||
345 cmd == MII_MEDIACHG) {
346 (*mii->mii_statchg)(sc->mii_dev.dv_parent);
347 mii_phy_statusmsg(sc);
348 sc->mii_media_active = mii->mii_media_active;
349 sc->mii_media_status = mii->mii_media_status;
350 }
351 }
352
353 void
354 mii_phy_statusmsg(struct mii_softc *sc)
355 {
356 struct mii_data *mii = sc->mii_pdata;
357 struct ifnet *ifp = mii->mii_ifp;
358 int s, baudrate, link_state, announce = 0;
359
360 if (mii->mii_media_status & IFM_AVALID) {
361 if (mii->mii_media_status & IFM_ACTIVE)
362 link_state = LINK_STATE_UP;
363 else
364 link_state = LINK_STATE_DOWN;
365 } else
366 link_state = LINK_STATE_UNKNOWN;
367
368 baudrate = ifmedia_baudrate(mii->mii_media_active);
369
370 if (link_state != ifp->if_link_state) {
371 ifp->if_link_state = link_state;
372 /*
373 * XXX Right here we'd like to notify protocols
374 * XXX that the link status has changed, so that
375 * XXX e.g. Duplicate Address Detection can restart.
376 */
377 announce = 1;
378 }
379
380 if (baudrate != ifp->if_baudrate) {
381 ifp->if_baudrate = baudrate;
382 announce = 1;
383 }
384
385 if (announce) {
386 s = splnet();
387 rt_ifmsg(ifp);
388 splx(s);
389 }
390 }
391
392 /*
393 * Initialize generic PHY media based on BMSR, called when a PHY is
394 * attached. We expect to be set up to print a comma-separated list
395 * of media names. Does not print a newline.
396 */
397 void
398 mii_phy_add_media(struct mii_softc *sc)
399 {
400 struct mii_data *mii = sc->mii_pdata;
401 const char *sep = "";
402
403 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
404 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
405
406 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
407 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
408 MII_MEDIA_NONE);
409
410 /*
411 * There are different interpretations for the bits in
412 * HomePNA PHYs. And there is really only one media type
413 * that is supported.
414 */
415 if (sc->mii_flags & MIIF_IS_HPNA) {
416 if (sc->mii_capabilities & BMSR_10THDX) {
417 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
418 sc->mii_inst),
419 MII_MEDIA_10_T);
420 PRINT("HomePNA1");
421 }
422 return;
423 }
424
425 if (sc->mii_capabilities & BMSR_10THDX) {
426 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
427 MII_MEDIA_10_T);
428 PRINT("10baseT");
429 }
430 if (sc->mii_capabilities & BMSR_10TFDX) {
431 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
432 MII_MEDIA_10_T_FDX);
433 PRINT("10baseT-FDX");
434 }
435 if (sc->mii_capabilities & BMSR_100TXHDX) {
436 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
437 MII_MEDIA_100_TX);
438 PRINT("100baseTX");
439 }
440 if (sc->mii_capabilities & BMSR_100TXFDX) {
441 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
442 MII_MEDIA_100_TX_FDX);
443 PRINT("100baseTX-FDX");
444 }
445 if (sc->mii_capabilities & BMSR_100T4) {
446 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
447 MII_MEDIA_100_T4);
448 PRINT("100baseT4");
449 }
450
451 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
452 /*
453 * XXX Right now only handle 1000SX and 1000TX. Need
454 * XXX to handle 1000LX and 1000CX some how.
455 *
456 * Note since it can take 5 seconds to auto-negotiate
457 * a gigabit link, we make anegticks 10 seconds for
458 * all the gigabit media types.
459 */
460 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
461 sc->mii_anegticks = 10;
462 sc->mii_flags |= MIIF_IS_1000X;
463 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
464 sc->mii_inst), MII_MEDIA_1000_X);
465 PRINT("1000baseSX");
466 }
467 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
468 sc->mii_anegticks = 10;
469 sc->mii_flags |= MIIF_IS_1000X;
470 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
471 sc->mii_inst), MII_MEDIA_1000_X_FDX);
472 PRINT("1000baseSX-FDX");
473 }
474
475 /*
476 * 1000baseT media needs to be able to manipulate
477 * master/slave mode. We set IFM_ETH_MASTER in
478 * the "don't care mask" and filter it out when
479 * the media is set.
480 *
481 * All 1000baseT PHYs have a 1000baseT control register.
482 */
483 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
484 sc->mii_anegticks = 10;
485 sc->mii_flags |= MIIF_HAVE_GTCR;
486 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
487 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
488 sc->mii_inst), MII_MEDIA_1000_T);
489 PRINT("1000baseT");
490 }
491 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
492 sc->mii_anegticks = 10;
493 sc->mii_flags |= MIIF_HAVE_GTCR;
494 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
495 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
496 sc->mii_inst), MII_MEDIA_1000_T_FDX);
497 PRINT("1000baseT-FDX");
498 }
499 }
500
501 if (sc->mii_capabilities & BMSR_ANEG) {
502 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
503 MII_NMEDIA); /* intentionally invalid index */
504 PRINT("auto");
505 }
506 #undef ADD
507 #undef PRINT
508 }
509
510 void
511 mii_phy_delete_media(struct mii_softc *sc)
512 {
513 struct mii_data *mii = sc->mii_pdata;
514
515 ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
516 }
517
518 int
519 mii_phy_activate(struct device *self, enum devact act)
520 {
521 int rv = 0;
522
523 switch (act) {
524 case DVACT_ACTIVATE:
525 rv = EOPNOTSUPP;
526 break;
527
528 case DVACT_DEACTIVATE:
529 /* Nothing special to do. */
530 break;
531 }
532
533 return (rv);
534 }
535
536 int
537 mii_phy_detach(struct device *self, int flags)
538 {
539 struct mii_softc *sc = (void *) self;
540
541 if (sc->mii_flags & MIIF_DOINGAUTO)
542 callout_stop(&sc->mii_nway_ch);
543
544 mii_phy_delete_media(sc);
545
546 return (0);
547 }
548
549 const struct mii_phydesc *
550 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
551 {
552
553 for (; mpd->mpd_name != NULL; mpd++) {
554 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
555 MII_MODEL(ma->mii_id2) == mpd->mpd_model)
556 return (mpd);
557 }
558 return (NULL);
559 }
560