mii_physubr.c revision 1.60 1 /* $NetBSD: mii_physubr.c,v 1.60 2008/05/04 17:06:09 xtraeme 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Subroutines common to all PHYs.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: mii_physubr.c,v 1.60 2008/05/04 17:06:09 xtraeme Exp $");
39
40 #include <sys/param.h>
41 #include <sys/device.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/socket.h>
45 #include <sys/errno.h>
46 #include <sys/proc.h>
47
48 #include <net/if.h>
49 #include <net/if_media.h>
50 #include <net/route.h>
51
52 #include <dev/mii/mii.h>
53 #include <dev/mii/miivar.h>
54
55 static void mii_phy_statusmsg(struct mii_softc *);
56
57 /*
58 * Media to register setting conversion table. Order matters.
59 */
60 static const struct mii_media mii_media_table[MII_NMEDIA] = {
61 /* None */
62 { BMCR_ISO, ANAR_CSMA,
63 0, },
64
65 /* 10baseT */
66 { BMCR_S10, ANAR_CSMA|ANAR_10,
67 0, },
68
69 /* 10baseT-FDX */
70 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD,
71 0, },
72
73 /* 100baseT4 */
74 { BMCR_S100, ANAR_CSMA|ANAR_T4,
75 0, },
76
77 /* 100baseTX */
78 { BMCR_S100, ANAR_CSMA|ANAR_TX,
79 0, },
80
81 /* 100baseTX-FDX */
82 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD,
83 0, },
84
85 /* 1000baseX */
86 { BMCR_S1000, ANAR_CSMA,
87 0, },
88
89 /* 1000baseX-FDX */
90 { BMCR_S1000|BMCR_FDX, ANAR_CSMA,
91 0, },
92
93 /* 1000baseT */
94 { BMCR_S1000, ANAR_CSMA,
95 GTCR_ADV_1000THDX },
96
97 /* 1000baseT-FDX */
98 { BMCR_S1000, ANAR_CSMA,
99 GTCR_ADV_1000TFDX },
100 };
101
102 static void mii_phy_auto_timeout(void *);
103
104 void
105 mii_phy_setmedia(struct mii_softc *sc)
106 {
107 struct mii_data *mii = sc->mii_pdata;
108 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
109 int bmcr, anar, gtcr;
110
111 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
112 /*
113 * Force renegotiation if MIIF_DOPAUSE.
114 *
115 * XXX This is only necessary because many NICs don't
116 * XXX advertise PAUSE capabilities at boot time. Maybe
117 * XXX we should force this only once?
118 */
119 if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0 ||
120 (sc->mii_flags & (MIIF_FORCEANEG|MIIF_DOPAUSE)))
121 (void) mii_phy_auto(sc, 1);
122 return;
123 }
124
125 /*
126 * Table index is stored in the media entry.
127 */
128
129 #ifdef DIAGNOSTIC
130 if (/* ife->ifm_data < 0 || */ ife->ifm_data >= MII_NMEDIA)
131 panic("mii_phy_setmedia");
132 #endif
133
134 anar = mii_media_table[ife->ifm_data].mm_anar;
135 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
136 gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
137
138 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
139 switch (IFM_SUBTYPE(ife->ifm_media)) {
140 case IFM_1000_T:
141 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
142 break;
143
144 default:
145 panic("mii_phy_setmedia: MASTER on wrong media");
146 }
147 }
148
149 if (mii->mii_media.ifm_media & IFM_FLOW) {
150 if (sc->mii_flags & MIIF_IS_1000X)
151 anar |= ANAR_X_PAUSE_SYM | ANAR_X_PAUSE_ASYM;
152 else {
153 anar |= ANAR_FC;
154 /* XXX Only 1000BASE-T has PAUSE_ASYM? */
155 if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
156 (sc->mii_extcapabilities &
157 (EXTSR_1000THDX|EXTSR_1000TFDX)))
158 anar |= ANAR_X_PAUSE_ASYM;
159 }
160 }
161
162 if (ife->ifm_media & IFM_LOOP)
163 bmcr |= BMCR_LOOP;
164
165 PHY_WRITE(sc, MII_ANAR, anar);
166 PHY_WRITE(sc, MII_BMCR, bmcr);
167 if (sc->mii_flags & MIIF_HAVE_GTCR)
168 PHY_WRITE(sc, MII_100T2CR, gtcr);
169 }
170
171 int
172 mii_phy_auto(struct mii_softc *sc, int waitfor)
173 {
174 int i;
175
176 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
177 /*
178 * Check for 1000BASE-X. Autonegotiation is a bit
179 * different on such devices.
180 */
181 if (sc->mii_flags & MIIF_IS_1000X) {
182 uint16_t anar = 0;
183
184 if (sc->mii_extcapabilities & EXTSR_1000XFDX)
185 anar |= ANAR_X_FD;
186 if (sc->mii_extcapabilities & EXTSR_1000XHDX)
187 anar |= ANAR_X_HD;
188
189 if (sc->mii_flags & MIIF_DOPAUSE) {
190 /* XXX Asymmetric vs. symmetric? */
191 anar |= ANLPAR_X_PAUSE_TOWARDS;
192 }
193
194 PHY_WRITE(sc, MII_ANAR, anar);
195 } else {
196 uint16_t anar;
197
198 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
199 ANAR_CSMA;
200 if (sc->mii_flags & MIIF_DOPAUSE) {
201 anar |= ANAR_FC;
202 /* XXX Only 1000BASE-T has PAUSE_ASYM? */
203 if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
204 (sc->mii_extcapabilities &
205 (EXTSR_1000THDX|EXTSR_1000TFDX)))
206 anar |= ANAR_X_PAUSE_ASYM;
207 }
208 PHY_WRITE(sc, MII_ANAR, anar);
209 if (sc->mii_flags & MIIF_HAVE_GTCR) {
210 uint16_t gtcr = 0;
211
212 if (sc->mii_extcapabilities & EXTSR_1000TFDX)
213 gtcr |= GTCR_ADV_1000TFDX;
214 if (sc->mii_extcapabilities & EXTSR_1000THDX)
215 gtcr |= GTCR_ADV_1000THDX;
216
217 PHY_WRITE(sc, MII_100T2CR, gtcr);
218 }
219 }
220 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
221 }
222
223 if (waitfor) {
224 /* Wait 500ms for it to complete. */
225 for (i = 0; i < 500; i++) {
226 if (PHY_READ(sc, MII_BMSR) & BMSR_ACOMP)
227 return (0);
228 delay(1000);
229 }
230
231 /*
232 * Don't need to worry about clearing MIIF_DOINGAUTO.
233 * If that's set, a timeout is pending, and it will
234 * clear the flag.
235 */
236 return (EIO);
237 }
238
239 /*
240 * Just let it finish asynchronously. This is for the benefit of
241 * the tick handler driving autonegotiation. Don't want 500ms
242 * delays all the time while the system is running!
243 */
244 if (sc->mii_flags & MIIF_AUTOTSLEEP) {
245 sc->mii_flags |= MIIF_DOINGAUTO;
246 tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
247 mii_phy_auto_timeout(sc);
248 } else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
249 sc->mii_flags |= MIIF_DOINGAUTO;
250 callout_reset(&sc->mii_nway_ch, hz >> 1,
251 mii_phy_auto_timeout, sc);
252 }
253 return (EJUSTRETURN);
254 }
255
256 static void
257 mii_phy_auto_timeout(void *arg)
258 {
259 struct mii_softc *sc = arg;
260 int s;
261
262 if (!device_is_active(sc->mii_dev))
263 return;
264
265 s = splnet();
266 sc->mii_flags &= ~MIIF_DOINGAUTO;
267
268 /* Update the media status. */
269 (void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
270 splx(s);
271 }
272
273 int
274 mii_phy_tick(struct mii_softc *sc)
275 {
276 struct mii_data *mii = sc->mii_pdata;
277 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
278 int reg;
279
280 /* Just bail now if the interface is down. */
281 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
282 return (EJUSTRETURN);
283
284 /*
285 * If we're not doing autonegotiation, we don't need to do
286 * any extra work here. However, we need to check the link
287 * status so we can generate an announcement if the status
288 * changes.
289 */
290 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
291 return (0);
292
293 /* Read the status register twice; BMSR_LINK is latch-low. */
294 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
295 if (reg & BMSR_LINK) {
296 /*
297 * See above.
298 */
299 return (0);
300 }
301
302 /*
303 * Only retry autonegotiation every N seconds.
304 */
305 KASSERT(sc->mii_anegticks != 0);
306 if (++sc->mii_ticks <= sc->mii_anegticks)
307 return (EJUSTRETURN);
308
309 sc->mii_ticks = 0;
310 PHY_RESET(sc);
311
312 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
313 return (EJUSTRETURN);
314
315 /*
316 * Might need to generate a status message if autonegotiation
317 * failed.
318 */
319 return (0);
320 }
321
322 void
323 mii_phy_reset(struct mii_softc *sc)
324 {
325 int reg, i;
326
327 if (sc->mii_flags & MIIF_NOISOLATE)
328 reg = BMCR_RESET;
329 else
330 reg = BMCR_RESET | BMCR_ISO;
331 PHY_WRITE(sc, MII_BMCR, reg);
332
333 /* Wait another 100ms for it to complete. */
334 for (i = 0; i < 100; i++) {
335 reg = PHY_READ(sc, MII_BMCR);
336 if ((reg & BMCR_RESET) == 0)
337 break;
338 delay(1000);
339 }
340
341 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
342 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
343 }
344
345 void
346 mii_phy_down(struct mii_softc *sc)
347 {
348
349 if (sc->mii_flags & MIIF_DOINGAUTO) {
350 sc->mii_flags &= ~MIIF_DOINGAUTO;
351 callout_stop(&sc->mii_nway_ch);
352 }
353 }
354
355 void
356 mii_phy_status(struct mii_softc *sc)
357 {
358
359 PHY_STATUS(sc);
360 }
361
362 void
363 mii_phy_update(struct mii_softc *sc, int cmd)
364 {
365 struct mii_data *mii = sc->mii_pdata;
366
367 if (sc->mii_media_active != mii->mii_media_active ||
368 sc->mii_media_status != mii->mii_media_status ||
369 cmd == MII_MEDIACHG) {
370 mii_phy_statusmsg(sc);
371 (*mii->mii_statchg)(device_parent(sc->mii_dev));
372 sc->mii_media_active = mii->mii_media_active;
373 sc->mii_media_status = mii->mii_media_status;
374 }
375 }
376
377 static void
378 mii_phy_statusmsg(struct mii_softc *sc)
379 {
380 struct mii_data *mii = sc->mii_pdata;
381 struct ifnet *ifp = mii->mii_ifp;
382 int s;
383
384 s = splnet();
385 if (mii->mii_media_status & IFM_AVALID) {
386 if (mii->mii_media_status & IFM_ACTIVE)
387 if_link_state_change(ifp, LINK_STATE_UP);
388 else
389 if_link_state_change(ifp, LINK_STATE_DOWN);
390 } else
391 if_link_state_change(ifp, LINK_STATE_UNKNOWN);
392 splx(s);
393
394 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
395 }
396
397 /*
398 * Initialize generic PHY media based on BMSR, called when a PHY is
399 * attached. We expect to be set up to print a comma-separated list
400 * of media names. Does not print a newline.
401 */
402 void
403 mii_phy_add_media(struct mii_softc *sc)
404 {
405 struct mii_data *mii = sc->mii_pdata;
406 const char *sep = "";
407 int fdx = 0;
408
409 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
410 #define PRINT(n) aprint_normal("%s%s", sep, (n)); sep = ", "
411
412 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
413 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
414 MII_MEDIA_NONE);
415
416 /*
417 * There are different interpretations for the bits in
418 * HomePNA PHYs. And there is really only one media type
419 * that is supported.
420 */
421 if (sc->mii_flags & MIIF_IS_HPNA) {
422 if (sc->mii_capabilities & BMSR_10THDX) {
423 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
424 sc->mii_inst),
425 MII_MEDIA_10_T);
426 PRINT("HomePNA1");
427 }
428 return;
429 }
430
431 if (sc->mii_capabilities & BMSR_10THDX) {
432 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
433 MII_MEDIA_10_T);
434 PRINT("10baseT");
435 }
436 if (sc->mii_capabilities & BMSR_10TFDX) {
437 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
438 MII_MEDIA_10_T_FDX);
439 PRINT("10baseT-FDX");
440 fdx = 1;
441 }
442 if (sc->mii_capabilities & BMSR_100TXHDX) {
443 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
444 MII_MEDIA_100_TX);
445 PRINT("100baseTX");
446 }
447 if (sc->mii_capabilities & BMSR_100TXFDX) {
448 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
449 MII_MEDIA_100_TX_FDX);
450 PRINT("100baseTX-FDX");
451 fdx = 1;
452 }
453 if (sc->mii_capabilities & BMSR_100T4) {
454 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
455 MII_MEDIA_100_T4);
456 PRINT("100baseT4");
457 }
458
459 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
460 /*
461 * XXX Right now only handle 1000SX and 1000TX. Need
462 * XXX to handle 1000LX and 1000CX some how.
463 *
464 * Note since it can take 5 seconds to auto-negotiate
465 * a gigabit link, we make anegticks 10 seconds for
466 * all the gigabit media types.
467 */
468 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
469 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
470 sc->mii_flags |= MIIF_IS_1000X;
471 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
472 sc->mii_inst), MII_MEDIA_1000_X);
473 PRINT("1000baseSX");
474 }
475 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
476 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
477 sc->mii_flags |= MIIF_IS_1000X;
478 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
479 sc->mii_inst), MII_MEDIA_1000_X_FDX);
480 PRINT("1000baseSX-FDX");
481 fdx = 1;
482 }
483
484 /*
485 * 1000baseT media needs to be able to manipulate
486 * master/slave mode. We set IFM_ETH_MASTER in
487 * the "don't care mask" and filter it out when
488 * the media is set.
489 *
490 * All 1000baseT PHYs have a 1000baseT control register.
491 */
492 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
493 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
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, 0,
497 sc->mii_inst), MII_MEDIA_1000_T);
498 PRINT("1000baseT");
499 }
500 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
501 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
502 sc->mii_flags |= MIIF_HAVE_GTCR;
503 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
504 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
505 sc->mii_inst), MII_MEDIA_1000_T_FDX);
506 PRINT("1000baseT-FDX");
507 fdx = 1;
508 }
509 }
510
511 if (sc->mii_capabilities & BMSR_ANEG) {
512 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
513 MII_NMEDIA); /* intentionally invalid index */
514 PRINT("auto");
515 }
516 #undef ADD
517 #undef PRINT
518 if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE))
519 mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
520 }
521
522 void
523 mii_phy_delete_media(struct mii_softc *sc)
524 {
525 struct mii_data *mii = sc->mii_pdata;
526
527 ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
528 }
529
530 int
531 mii_phy_activate(struct device *self, enum devact act)
532 {
533 int rv = 0;
534
535 switch (act) {
536 case DVACT_ACTIVATE:
537 rv = EOPNOTSUPP;
538 break;
539
540 case DVACT_DEACTIVATE:
541 /* Nothing special to do. */
542 break;
543 }
544
545 return (rv);
546 }
547
548 /* ARGSUSED1 */
549 int
550 mii_phy_detach(struct device *self, int flags)
551 {
552 struct mii_softc *sc = device_private(self);
553
554 if (sc->mii_flags & MIIF_DOINGAUTO)
555 callout_stop(&sc->mii_nway_ch);
556
557 mii_phy_delete_media(sc);
558 LIST_REMOVE(sc, mii_list);
559
560 return (0);
561 }
562
563 const struct mii_phydesc *
564 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
565 {
566
567 for (; mpd->mpd_name != NULL; mpd++) {
568 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
569 MII_MODEL(ma->mii_id2) == mpd->mpd_model)
570 return (mpd);
571 }
572 return (NULL);
573 }
574
575 /*
576 * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
577 */
578 u_int
579 mii_phy_flowstatus(struct mii_softc *sc)
580 {
581 u_int anar, anlpar;
582
583 if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
584 return (0);
585
586 anar = PHY_READ(sc, MII_ANAR);
587 anlpar = PHY_READ(sc, MII_ANLPAR);
588
589 if ((anar & ANAR_X_PAUSE_SYM) & (anlpar & ANLPAR_X_PAUSE_SYM))
590 return (IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE);
591
592 if ((anar & ANAR_X_PAUSE_SYM) == 0) {
593 if ((anar & ANAR_X_PAUSE_ASYM) &&
594 ((anlpar &
595 ANLPAR_X_PAUSE_TOWARDS) == ANLPAR_X_PAUSE_TOWARDS))
596 return (IFM_FLOW|IFM_ETH_TXPAUSE);
597 else
598 return (0);
599 }
600
601 if ((anar & ANAR_X_PAUSE_ASYM) == 0) {
602 if (anlpar & ANLPAR_X_PAUSE_SYM)
603 return (IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE);
604 else
605 return (0);
606 }
607
608 switch ((anlpar & ANLPAR_X_PAUSE_TOWARDS)) {
609 case ANLPAR_X_PAUSE_NONE:
610 return (0);
611
612 case ANLPAR_X_PAUSE_ASYM:
613 return (IFM_FLOW|IFM_ETH_RXPAUSE);
614
615 default:
616 return (IFM_FLOW|IFM_ETH_RXPAUSE|IFM_ETH_TXPAUSE);
617 }
618 /* NOTREACHED */
619 }
620
621 bool
622 mii_phy_resume(device_t dv PMF_FN_ARGS)
623 {
624 struct mii_softc *sc = device_private(dv);
625
626 PHY_RESET(sc);
627 return PHY_SERVICE(sc, sc->mii_pdata, MII_MEDIACHG) == 0;
628 }
629