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