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