mii_physubr.c revision 1.84 1 /* $NetBSD: mii_physubr.c,v 1.84 2019/01/22 03:42:27 msaitoh 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.84 2019/01/22 03:42:27 msaitoh 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/module.h>
47 #include <sys/proc.h>
48
49 #include <net/if.h>
50 #include <net/if_media.h>
51 #include <net/route.h>
52
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
55
56 const char *(*mii_get_descr)(int, int) = mii_get_descr_stub;
57
58 int mii_verbose_loaded = 0;
59
60 const char *mii_get_descr_stub(int oui, int model)
61 {
62 mii_load_verbose();
63 if (mii_verbose_loaded)
64 return mii_get_descr(oui, model);
65 else
66 return NULL;
67 }
68
69 /*
70 * Routine to load the miiverbose kernel module as needed
71 */
72 void mii_load_verbose(void)
73 {
74 if (mii_verbose_loaded == 0)
75 module_autoload("miiverbose", MODULE_CLASS_MISC);
76 }
77
78 static void mii_phy_statusmsg(struct mii_softc *);
79
80 /*
81 * Media to register setting conversion table. Order matters.
82 */
83 static const struct mii_media mii_media_table[MII_NMEDIA] = {
84 /* None */
85 { BMCR_ISO, ANAR_CSMA,
86 0, },
87
88 /* 10baseT */
89 { BMCR_S10, ANAR_CSMA|ANAR_10,
90 0, },
91
92 /* 10baseT-FDX */
93 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD,
94 0, },
95
96 /* 100baseT4 */
97 { BMCR_S100, ANAR_CSMA|ANAR_T4,
98 0, },
99
100 /* 100baseTX */
101 { BMCR_S100, ANAR_CSMA|ANAR_TX,
102 0, },
103
104 /* 100baseTX-FDX */
105 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD,
106 0, },
107
108 /* 1000baseX */
109 { BMCR_S1000, ANAR_CSMA,
110 0, },
111
112 /* 1000baseX-FDX */
113 { BMCR_S1000|BMCR_FDX, ANAR_CSMA,
114 0, },
115
116 /* 1000baseT */
117 { BMCR_S1000, ANAR_CSMA,
118 GTCR_ADV_1000THDX },
119
120 /* 1000baseT-FDX */
121 { BMCR_S1000, ANAR_CSMA,
122 GTCR_ADV_1000TFDX },
123 };
124
125 static void mii_phy_auto_timeout(void *);
126
127 void
128 mii_phy_setmedia(struct mii_softc *sc)
129 {
130 struct mii_data *mii = sc->mii_pdata;
131 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
132 uint16_t bmcr, anar, gtcr;
133
134 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
135 /*
136 * Force renegotiation if MIIF_DOPAUSE.
137 *
138 * XXX This is only necessary because many NICs don't
139 * XXX advertise PAUSE capabilities at boot time. Maybe
140 * XXX we should force this only once?
141 */
142 PHY_READ(sc, MII_BMCR, &bmcr);
143 if ((bmcr & BMCR_AUTOEN) == 0 ||
144 (sc->mii_flags & (MIIF_FORCEANEG|MIIF_DOPAUSE)))
145 (void) mii_phy_auto(sc, 1);
146 return;
147 }
148
149 /*
150 * Table index is stored in the media entry.
151 */
152
153 #ifdef DIAGNOSTIC
154 if (/* ife->ifm_data < 0 || */ ife->ifm_data >= MII_NMEDIA)
155 panic("mii_phy_setmedia");
156 #endif
157
158 anar = mii_media_table[ife->ifm_data].mm_anar;
159 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
160 gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
161
162 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
163 switch (IFM_SUBTYPE(ife->ifm_media)) {
164 case IFM_1000_T:
165 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
166 break;
167
168 default:
169 panic("mii_phy_setmedia: MASTER on wrong media");
170 }
171 }
172
173 if (mii->mii_media.ifm_media & IFM_FLOW) {
174 if (sc->mii_flags & MIIF_IS_1000X)
175 anar |= ANAR_X_PAUSE_SYM | ANAR_X_PAUSE_ASYM;
176 else {
177 anar |= ANAR_FC;
178 /* XXX Only 1000BASE-T has PAUSE_ASYM? */
179 if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
180 (sc->mii_extcapabilities &
181 (EXTSR_1000THDX | EXTSR_1000TFDX)))
182 anar |= ANAR_PAUSE_ASYM;
183 }
184 }
185
186 if (ife->ifm_media & IFM_LOOP)
187 bmcr |= BMCR_LOOP;
188
189 PHY_WRITE(sc, MII_ANAR, anar);
190 if (sc->mii_flags & MIIF_HAVE_GTCR)
191 PHY_WRITE(sc, MII_100T2CR, gtcr);
192 if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
193 mii_phy_auto(sc, 0);
194 } else {
195 PHY_WRITE(sc, MII_BMCR, bmcr);
196 }
197 }
198
199 int
200 mii_phy_auto(struct mii_softc *sc, int waitfor)
201 {
202 int i;
203 struct mii_data *mii = sc->mii_pdata;
204 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
205
206 sc->mii_ticks = 0;
207 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
208 /*
209 * Check for 1000BASE-X. Autonegotiation is a bit
210 * different on such devices.
211 */
212 if (sc->mii_flags & MIIF_IS_1000X) {
213 uint16_t anar = 0;
214
215 if (sc->mii_extcapabilities & EXTSR_1000XFDX)
216 anar |= ANAR_X_FD;
217 if (sc->mii_extcapabilities & EXTSR_1000XHDX)
218 anar |= ANAR_X_HD;
219
220 if (sc->mii_flags & MIIF_DOPAUSE) {
221 /* XXX Asymmetric vs. symmetric? */
222 anar |= ANLPAR_X_PAUSE_TOWARDS;
223 }
224
225 PHY_WRITE(sc, MII_ANAR, anar);
226 } else {
227 uint16_t anar;
228
229 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
230 ANAR_CSMA;
231 if (sc->mii_flags & MIIF_DOPAUSE) {
232 anar |= ANAR_FC;
233 /* XXX Only 1000BASE-T has PAUSE_ASYM? */
234 if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
235 (sc->mii_extcapabilities &
236 (EXTSR_1000THDX | EXTSR_1000TFDX)))
237 anar |= ANAR_PAUSE_ASYM;
238 }
239
240 /*
241 * For 1000-base-T, autonegotiation must be enabled,
242 * but if we're not set to auto, only advertise
243 * 1000-base-T with the link partner.
244 */
245 if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
246 anar &= ~(ANAR_T4|ANAR_TX_FD|ANAR_TX|ANAR_10_FD|ANAR_10);
247 }
248
249 PHY_WRITE(sc, MII_ANAR, anar);
250 if (sc->mii_flags & MIIF_HAVE_GTCR) {
251 uint16_t gtcr = 0;
252
253 if (sc->mii_extcapabilities & EXTSR_1000TFDX)
254 gtcr |= GTCR_ADV_1000TFDX;
255 if (sc->mii_extcapabilities & EXTSR_1000THDX)
256 gtcr |= GTCR_ADV_1000THDX;
257
258 PHY_WRITE(sc, MII_100T2CR, gtcr);
259 }
260 }
261 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
262 }
263
264 if (waitfor) {
265 /* Wait 500ms for it to complete. */
266 for (i = 0; i < 500; i++) {
267 uint16_t bmsr;
268
269 PHY_READ(sc, MII_BMSR, &bmsr);
270 if (bmsr & BMSR_ACOMP)
271 return (0);
272 delay(1000);
273 }
274
275 /*
276 * Don't need to worry about clearing MIIF_DOINGAUTO.
277 * If that's set, a timeout is pending, and it will
278 * clear the flag.
279 */
280 return (EIO);
281 }
282
283 /*
284 * Just let it finish asynchronously. This is for the benefit of
285 * the tick handler driving autonegotiation. Don't want 500ms
286 * delays all the time while the system is running!
287 */
288 if (sc->mii_flags & MIIF_AUTOTSLEEP) {
289 sc->mii_flags |= MIIF_DOINGAUTO;
290 tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
291 mii_phy_auto_timeout(sc);
292 } else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
293 sc->mii_flags |= MIIF_DOINGAUTO;
294 callout_reset(&sc->mii_nway_ch, hz >> 1,
295 mii_phy_auto_timeout, sc);
296 }
297 return (EJUSTRETURN);
298 }
299
300 static void
301 mii_phy_auto_timeout(void *arg)
302 {
303 struct mii_softc *sc = arg;
304 int s;
305
306 if (!device_is_active(sc->mii_dev))
307 return;
308
309 s = splnet();
310 sc->mii_flags &= ~MIIF_DOINGAUTO;
311
312 /* Update the media status. */
313 (void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
314 splx(s);
315 }
316
317 int
318 mii_phy_tick(struct mii_softc *sc)
319 {
320 struct mii_data *mii = sc->mii_pdata;
321 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
322 uint16_t reg;
323
324 /* Just bail now if the interface is down. */
325 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
326 return (EJUSTRETURN);
327
328 /*
329 * If we're not doing autonegotiation, we don't need to do
330 * any extra work here. However, we need to check the link
331 * status so we can generate an announcement by returning
332 * with 0 if the status changes.
333 */
334 if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
335 (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
336 /*
337 * Reset autonegotiation timer to 0 just to make sure
338 * the future autonegotiation start with 0.
339 */
340 sc->mii_ticks = 0;
341 return (0);
342 }
343
344 /* Read the status register twice; BMSR_LINK is latch-low. */
345 PHY_READ(sc, MII_BMSR, ®);
346 PHY_READ(sc, MII_BMSR, ®);
347 if (reg & BMSR_LINK) {
348 /*
349 * Reset autonegotiation timer to 0 in case the link
350 * goes down in the next tick.
351 */
352 sc->mii_ticks = 0;
353 /* See above. */
354 return (0);
355 }
356
357 /*
358 * mii_ticks == 0 means it's the first tick after changing the media or
359 * the link became down since the last tick (see above), so return with
360 * 0 to update the status.
361 */
362 if (sc->mii_ticks++ == 0)
363 return (0);
364
365 /*
366 * Only retry autonegotiation every N seconds.
367 */
368 KASSERT(sc->mii_anegticks != 0);
369 if (sc->mii_ticks <= sc->mii_anegticks)
370 return (EJUSTRETURN);
371
372 PHY_RESET(sc);
373
374 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
375 return (EJUSTRETURN);
376
377 /*
378 * Might need to generate a status message if autonegotiation
379 * failed.
380 */
381 return (0);
382 }
383
384 void
385 mii_phy_reset(struct mii_softc *sc)
386 {
387 int i;
388 uint16_t reg;
389
390 if (sc->mii_flags & MIIF_NOISOLATE)
391 reg = BMCR_RESET;
392 else
393 reg = BMCR_RESET | BMCR_ISO;
394 PHY_WRITE(sc, MII_BMCR, reg);
395
396 /* Wait another 500ms for it to complete. */
397 for (i = 0; i < 500; i++) {
398 PHY_READ(sc, MII_BMCR, ®);
399 if ((reg & BMCR_RESET) == 0)
400 break;
401 delay(1000);
402 }
403
404 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
405 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
406 }
407
408 void
409 mii_phy_down(struct mii_softc *sc)
410 {
411
412 if (sc->mii_flags & MIIF_DOINGAUTO) {
413 sc->mii_flags &= ~MIIF_DOINGAUTO;
414 callout_stop(&sc->mii_nway_ch);
415 }
416 }
417
418 void
419 mii_phy_status(struct mii_softc *sc)
420 {
421
422 PHY_STATUS(sc);
423 }
424
425 void
426 mii_phy_update(struct mii_softc *sc, int cmd)
427 {
428 struct mii_data *mii = sc->mii_pdata;
429
430 if (sc->mii_media_active != mii->mii_media_active ||
431 sc->mii_media_status != mii->mii_media_status ||
432 cmd == MII_MEDIACHG) {
433 mii_phy_statusmsg(sc);
434 (*mii->mii_statchg)(mii->mii_ifp);
435 sc->mii_media_active = mii->mii_media_active;
436 sc->mii_media_status = mii->mii_media_status;
437 }
438 }
439
440 static void
441 mii_phy_statusmsg(struct mii_softc *sc)
442 {
443 struct mii_data *mii = sc->mii_pdata;
444 struct ifnet *ifp = mii->mii_ifp;
445
446 if (mii->mii_media_status & IFM_AVALID) {
447 if (mii->mii_media_status & IFM_ACTIVE)
448 if_link_state_change(ifp, LINK_STATE_UP);
449 else
450 if_link_state_change(ifp, LINK_STATE_DOWN);
451 } else
452 if_link_state_change(ifp, LINK_STATE_UNKNOWN);
453
454 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
455 }
456
457 /*
458 * Initialize generic PHY media based on BMSR, called when a PHY is
459 * attached. We expect to be set up to print a comma-separated list
460 * of media names. Does not print a newline.
461 */
462 void
463 mii_phy_add_media(struct mii_softc *sc)
464 {
465 struct mii_data *mii = sc->mii_pdata;
466 device_t self = sc->mii_dev;
467 const char *sep = "";
468 int fdx = 0;
469
470 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
471 #define PRINT(n) aprint_normal("%s%s", sep, (n)); sep = ", "
472
473 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
474 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
475 MII_MEDIA_NONE);
476
477 /*
478 * There are different interpretations for the bits in
479 * HomePNA PHYs. And there is really only one media type
480 * that is supported.
481 */
482 if (sc->mii_flags & MIIF_IS_HPNA) {
483 if (sc->mii_capabilities & BMSR_10THDX) {
484 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
485 sc->mii_inst),
486 MII_MEDIA_10_T);
487 PRINT("HomePNA1");
488 }
489 goto out;
490 }
491
492 if (sc->mii_capabilities & BMSR_10THDX) {
493 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
494 MII_MEDIA_10_T);
495 PRINT("10baseT");
496 }
497 if (sc->mii_capabilities & BMSR_10TFDX) {
498 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
499 MII_MEDIA_10_T_FDX);
500 PRINT("10baseT-FDX");
501 fdx = 1;
502 }
503 if (sc->mii_capabilities & BMSR_100TXHDX) {
504 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
505 MII_MEDIA_100_TX);
506 PRINT("100baseTX");
507 }
508 if (sc->mii_capabilities & BMSR_100TXFDX) {
509 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
510 MII_MEDIA_100_TX_FDX);
511 PRINT("100baseTX-FDX");
512 fdx = 1;
513 }
514 if (sc->mii_capabilities & BMSR_100T4) {
515 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
516 MII_MEDIA_100_T4);
517 PRINT("100baseT4");
518 }
519
520 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
521 /*
522 * XXX Right now only handle 1000SX and 1000TX. Need
523 * XXX to handle 1000LX and 1000CX some how.
524 *
525 * Note since it can take 5 seconds to auto-negotiate
526 * a gigabit link, we make anegticks 10 seconds for
527 * all the gigabit media types.
528 */
529 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
530 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
531 sc->mii_flags |= MIIF_IS_1000X;
532 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
533 sc->mii_inst), MII_MEDIA_1000_X);
534 PRINT("1000baseSX");
535 }
536 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
537 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
538 sc->mii_flags |= MIIF_IS_1000X;
539 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
540 sc->mii_inst), MII_MEDIA_1000_X_FDX);
541 PRINT("1000baseSX-FDX");
542 fdx = 1;
543 }
544
545 /*
546 * 1000baseT media needs to be able to manipulate
547 * master/slave mode. We set IFM_ETH_MASTER in
548 * the "don't care mask" and filter it out when
549 * the media is set.
550 *
551 * All 1000baseT PHYs have a 1000baseT control register.
552 */
553 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
554 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
555 sc->mii_flags |= MIIF_HAVE_GTCR;
556 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
557 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
558 sc->mii_inst), MII_MEDIA_1000_T);
559 PRINT("1000baseT");
560 }
561 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
562 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
563 sc->mii_flags |= MIIF_HAVE_GTCR;
564 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
565 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
566 sc->mii_inst), MII_MEDIA_1000_T_FDX);
567 PRINT("1000baseT-FDX");
568 fdx = 1;
569 }
570 }
571
572 if (sc->mii_capabilities & BMSR_ANEG) {
573 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
574 MII_NMEDIA); /* intentionally invalid index */
575 PRINT("auto");
576 }
577 #undef ADD
578 #undef PRINT
579 if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE))
580 mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
581 out:
582 if (!pmf_device_register(self, NULL, mii_phy_resume)) {
583 aprint_normal("\n");
584 aprint_error_dev(self, "couldn't establish power handler");
585 }
586 }
587
588 void
589 mii_phy_delete_media(struct mii_softc *sc)
590 {
591 struct mii_data *mii = sc->mii_pdata;
592
593 ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
594 }
595
596 int
597 mii_phy_activate(device_t self, enum devact act)
598 {
599 switch (act) {
600 case DVACT_DEACTIVATE:
601 /* XXX Invalidate parent's media setting? */
602 return 0;
603 default:
604 return EOPNOTSUPP;
605 }
606 }
607
608 /* ARGSUSED1 */
609 int
610 mii_phy_detach(device_t self, int flags)
611 {
612 struct mii_softc *sc = device_private(self);
613
614 /* XXX Invalidate parent's media setting? */
615
616 if (sc->mii_flags & MIIF_DOINGAUTO)
617 callout_halt(&sc->mii_nway_ch, NULL);
618
619 callout_destroy(&sc->mii_nway_ch);
620
621 mii_phy_delete_media(sc);
622 LIST_REMOVE(sc, mii_list);
623
624 return (0);
625 }
626
627 const struct mii_phydesc *
628 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
629 {
630
631 for (; mpd->mpd_name != NULL; mpd++) {
632 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
633 MII_MODEL(ma->mii_id2) == mpd->mpd_model)
634 return (mpd);
635 }
636 return (NULL);
637 }
638
639 /*
640 * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
641 */
642 u_int
643 mii_phy_flowstatus(struct mii_softc *sc)
644 {
645 uint16_t anar, anlpar;
646
647 if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
648 return (0);
649
650 PHY_READ(sc, MII_ANAR, &anar);
651 PHY_READ(sc, MII_ANLPAR, &anlpar);
652
653 /* For 1000baseX, the bits are in a different location. */
654 if (sc->mii_flags & MIIF_IS_1000X) {
655 anar <<= 3;
656 anlpar <<= 3;
657 }
658
659 if ((anar & ANAR_PAUSE_SYM) & (anlpar & ANLPAR_PAUSE_SYM))
660 return (IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE);
661
662 if ((anar & ANAR_PAUSE_SYM) == 0) {
663 if ((anar & ANAR_PAUSE_ASYM) &&
664 ((anlpar & ANLPAR_PAUSE_TOWARDS) == ANLPAR_PAUSE_TOWARDS))
665 return (IFM_FLOW|IFM_ETH_TXPAUSE);
666 else
667 return (0);
668 }
669
670 if ((anar & ANAR_PAUSE_ASYM) == 0) {
671 if (anlpar & ANLPAR_PAUSE_SYM)
672 return (IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE);
673 else
674 return (0);
675 }
676
677 switch ((anlpar & ANLPAR_PAUSE_TOWARDS)) {
678 case ANLPAR_PAUSE_NONE:
679 return (0);
680
681 case ANLPAR_PAUSE_ASYM:
682 return (IFM_FLOW|IFM_ETH_RXPAUSE);
683
684 default:
685 return (IFM_FLOW|IFM_ETH_RXPAUSE|IFM_ETH_TXPAUSE);
686 }
687 /* NOTREACHED */
688 }
689
690 bool
691 mii_phy_resume(device_t dv, const pmf_qual_t *qual)
692 {
693 struct mii_softc *sc = device_private(dv);
694
695 PHY_RESET(sc);
696 return PHY_SERVICE(sc, sc->mii_pdata, MII_MEDIACHG) == 0;
697 }
698
699
700 /*
701 * Given an ifmedia word, return the corresponding ANAR value.
702 */
703 int
704 mii_anar(int media)
705 {
706 int rv;
707
708 #ifdef DIAGNOSTIC
709 if (/* media < 0 || */ media >= MII_NMEDIA)
710 panic("mii_anar");
711 #endif
712
713 rv = mii_media_table[media].mm_anar;
714
715 return rv;
716 }
717