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