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