mii_physubr.c revision 1.102 1 /* $NetBSD: mii_physubr.c,v 1.102 2023/02/22 08:09:09 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.102 2023/02/22 08:09:09 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/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 KASSERT((sc->mii_flags & MIIF_DOINGAUTO) == 0);
286 cv_broadcast(&sc->mii_nway_cv);
287 } else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
288 sc->mii_flags |= MIIF_DOINGAUTO;
289 callout_reset(&sc->mii_nway_ch, hz >> 1,
290 mii_phy_auto_timeout, sc);
291 }
292 return EJUSTRETURN;
293 }
294
295 /* Just restart autonegotiation without changing any setting */
296 int
297 mii_phy_auto_restart(struct mii_softc *sc)
298 {
299 uint16_t reg;
300
301 PHY_READ(sc, MII_BMCR, ®);
302 reg |= BMCR_STARTNEG;
303 PHY_WRITE(sc, MII_BMCR, reg);
304 sc->mii_ticks = 0;
305
306 return EJUSTRETURN;
307 }
308
309 static void
310 mii_phy_auto_timeout_locked(struct mii_softc *sc)
311 {
312
313 KASSERT(mii_locked(sc->mii_pdata));
314 KASSERT(sc->mii_flags & MIIF_DOINGAUTO);
315
316 if (!device_is_active(sc->mii_dev))
317 return;
318
319 sc->mii_flags &= ~MIIF_DOINGAUTO;
320
321 /* Update the media status. */
322 (void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
323 }
324
325 static void
326 mii_phy_auto_timeout(void *arg)
327 {
328 struct mii_softc *sc = arg;
329
330 KASSERT((sc->mii_flags & MIIF_AUTOTSLEEP) == 0);
331
332 if (!device_is_active(sc->mii_dev))
333 return;
334
335 mii_lock(sc->mii_pdata);
336 mii_phy_auto_timeout_locked(sc);
337 mii_unlock(sc->mii_pdata);
338 }
339
340 int
341 mii_phy_tick(struct mii_softc *sc)
342 {
343 struct mii_data *mii = sc->mii_pdata;
344 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
345 uint16_t reg;
346
347 KASSERT(mii_locked(mii));
348
349 /* Just bail now if the interface is down. */
350 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
351 return EJUSTRETURN;
352
353 /*
354 * If we're not doing autonegotiation, we don't need to do any extra
355 * work here. However, we need to check the link status so we can
356 * generate an announcement by returning with 0 if the status changes.
357 */
358 if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
359 (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
360 /*
361 * Reset autonegotiation timer to 0 just to make sure
362 * the future autonegotiation start with 0.
363 */
364 sc->mii_ticks = 0;
365 return 0;
366 }
367
368 /* Read the status register twice; BMSR_LINK is latch-low. */
369 PHY_READ(sc, MII_BMSR, ®);
370 PHY_READ(sc, MII_BMSR, ®);
371 if (reg & BMSR_LINK) {
372 /*
373 * Reset autonegotiation timer to 0 in case the link
374 * goes down in the next tick.
375 */
376 sc->mii_ticks = 0;
377 /* See above. */
378 return 0;
379 }
380
381 /*
382 * mii_ticks == 0 means it's the first tick after changing the media or
383 * the link became down since the last tick (see above), so return with
384 * 0 to update the status.
385 */
386 if (sc->mii_ticks++ == 0)
387 return 0;
388
389 /*
390 * Only retry autonegotiation every N seconds.
391 */
392 KASSERT(sc->mii_anegticks != 0);
393 if (sc->mii_ticks < sc->mii_anegticks)
394 return EJUSTRETURN;
395
396 if (mii_phy_auto_restart(sc) == EJUSTRETURN)
397 return EJUSTRETURN;
398
399 /*
400 * Might need to generate a status message if autonegotiation
401 * failed.
402 */
403 return 0;
404 }
405
406 void
407 mii_phy_reset(struct mii_softc *sc)
408 {
409 int i;
410 uint16_t reg;
411
412 KASSERT(mii_locked(sc->mii_pdata));
413
414 if (sc->mii_flags & MIIF_NOISOLATE)
415 reg = BMCR_RESET;
416 else
417 reg = BMCR_RESET | BMCR_ISO;
418 PHY_WRITE(sc, MII_BMCR, reg);
419
420 /* Wait another 500ms for it to complete. */
421 for (i = 0; i < 500; i++) {
422 PHY_READ(sc, MII_BMCR, ®);
423 if ((reg & BMCR_RESET) == 0)
424 break;
425 delay(1000);
426 }
427
428 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
429 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
430 }
431
432 void
433 mii_phy_down(struct mii_softc *sc)
434 {
435
436 KASSERT(mii_locked(sc->mii_pdata));
437
438 if (sc->mii_flags & MIIF_AUTOTSLEEP) {
439 while (sc->mii_flags & MIIF_DOINGAUTO) {
440 cv_wait(&sc->mii_nway_cv,
441 sc->mii_pdata->mii_media.ifm_lock);
442 }
443 } else {
444 if ((sc->mii_flags & MIIF_DOINGAUTO) != 0 &&
445 callout_halt(&sc->mii_nway_ch,
446 sc->mii_pdata->mii_media.ifm_lock) == 0) {
447 /*
448 * The callout was scheduled, and we prevented
449 * it from running before it expired, so we are
450 * now responsible for clearing the flag.
451 */
452 sc->mii_flags &= ~MIIF_DOINGAUTO;
453 }
454 }
455 KASSERT((sc->mii_flags & MIIF_DOINGAUTO) == 0);
456 }
457
458 void
459 mii_phy_status(struct mii_softc *sc)
460 {
461
462 KASSERT(mii_locked(sc->mii_pdata));
463 PHY_STATUS(sc);
464 }
465
466 void
467 mii_phy_update(struct mii_softc *sc, int cmd)
468 {
469 struct mii_data *mii = sc->mii_pdata;
470 u_int mii_media_active;
471 int mii_media_status;
472
473 KASSERT(mii_locked(mii));
474
475 mii_media_active = mii->mii_media_active;
476 mii_media_status = mii->mii_media_status;
477
478 if (sc->mii_media_active != mii_media_active ||
479 sc->mii_media_status != mii_media_status ||
480 cmd == MII_MEDIACHG) {
481 mii_phy_statusmsg(sc);
482 (*mii->mii_statchg)(mii->mii_ifp);
483 sc->mii_media_active = mii_media_active;
484 sc->mii_media_status = mii_media_status;
485 }
486 }
487
488 static void
489 mii_phy_statusmsg(struct mii_softc *sc)
490 {
491 struct mii_data *mii = sc->mii_pdata;
492 struct ifnet *ifp = mii->mii_ifp;
493
494 KASSERT(mii_locked(mii));
495
496 if (mii->mii_media_status & IFM_AVALID) {
497 if (mii->mii_media_status & IFM_ACTIVE)
498 if_link_state_change(ifp, LINK_STATE_UP);
499 else
500 if_link_state_change(ifp, LINK_STATE_DOWN);
501 } else
502 if_link_state_change(ifp, LINK_STATE_UNKNOWN);
503
504 /* XXX NET_MPSAFE */
505 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
506 }
507
508 /*
509 * Initialize generic PHY media based on BMSR, called when a PHY is
510 * attached. We expect to be set up to print a comma-separated list
511 * of media names. Does not print a newline.
512 */
513 void
514 mii_phy_add_media(struct mii_softc *sc)
515 {
516 struct mii_data *mii = sc->mii_pdata;
517 device_t self = sc->mii_dev;
518 const char *sep = "";
519 int fdx = 0;
520
521 aprint_normal_dev(self, "");
522 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
523 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) {
524 aprint_error("no media present\n");
525 goto out;
526 }
527
528 /*
529 * Set the autonegotiation timer for 10/100 media. Gigabit media is
530 * handled below.
531 */
532 mii_lock(mii);
533 sc->mii_anegticks = MII_ANEGTICKS;
534 mii_unlock(mii);
535
536 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
537 #define PRINT(n) aprint_normal("%s%s", sep, (n)); sep = ", "
538
539 /* This flag is static; no need to lock. */
540 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
541 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
542 MII_MEDIA_NONE);
543
544 /*
545 * There are different interpretations for the bits in
546 * HomePNA PHYs. And there is really only one media type
547 * that is supported. This flag is also static, and so
548 * no need to lock.
549 */
550 if (sc->mii_flags & MIIF_IS_HPNA) {
551 if (sc->mii_capabilities & BMSR_10THDX) {
552 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
553 sc->mii_inst),
554 MII_MEDIA_10_T);
555 PRINT("HomePNA1");
556 }
557 goto out;
558 }
559
560 if (sc->mii_capabilities & BMSR_10THDX) {
561 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
562 MII_MEDIA_10_T);
563 PRINT("10baseT");
564 }
565 if (sc->mii_capabilities & BMSR_10TFDX) {
566 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
567 MII_MEDIA_10_T_FDX);
568 PRINT("10baseT-FDX");
569 fdx = 1;
570 }
571 if (sc->mii_capabilities & BMSR_100TXHDX) {
572 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
573 MII_MEDIA_100_TX);
574 PRINT("100baseTX");
575 }
576 if (sc->mii_capabilities & BMSR_100TXFDX) {
577 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
578 MII_MEDIA_100_TX_FDX);
579 PRINT("100baseTX-FDX");
580 fdx = 1;
581 }
582 if (sc->mii_capabilities & BMSR_100T4) {
583 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
584 MII_MEDIA_100_T4);
585 PRINT("100baseT4");
586 }
587
588 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
589 /*
590 * XXX Right now only handle 1000SX and 1000TX. Need
591 * XXX to handle 1000LX and 1000CX some how.
592 *
593 * Note since it can take 5 seconds to auto-negotiate
594 * a gigabit link, we make anegticks 10 seconds for
595 * all the gigabit media types.
596 */
597 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
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, 0,
603 sc->mii_inst), MII_MEDIA_1000_X);
604 PRINT("1000baseSX");
605 }
606 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
607 mii_lock(mii);
608 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
609 sc->mii_flags |= MIIF_IS_1000X;
610 mii_unlock(mii);
611 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
612 sc->mii_inst), MII_MEDIA_1000_X_FDX);
613 PRINT("1000baseSX-FDX");
614 fdx = 1;
615 }
616
617 /*
618 * 1000baseT media needs to be able to manipulate
619 * master/slave mode. We set IFM_ETH_MASTER in
620 * the "don't care mask" and filter it out when
621 * the media is set.
622 *
623 * All 1000baseT PHYs have a 1000baseT control register.
624 */
625 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
626 mii_lock(mii);
627 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
628 sc->mii_flags |= MIIF_HAVE_GTCR;
629 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
630 mii_unlock(mii);
631 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
632 sc->mii_inst), MII_MEDIA_1000_T);
633 PRINT("1000baseT");
634 }
635 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
636 mii_lock(mii);
637 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
638 sc->mii_flags |= MIIF_HAVE_GTCR;
639 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
640 mii_unlock(mii);
641 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
642 sc->mii_inst), MII_MEDIA_1000_T_FDX);
643 PRINT("1000baseT-FDX");
644 fdx = 1;
645 }
646 }
647
648 if (sc->mii_capabilities & BMSR_ANEG) {
649 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
650 MII_NMEDIA); /* intentionally invalid index */
651 PRINT("auto");
652 }
653 #undef ADD
654 #undef PRINT
655 /* This flag is static; no need to lock. */
656 if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE)) {
657 mii_lock(mii);
658 mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
659 mii_unlock(mii);
660 }
661 out:
662 aprint_normal("\n");
663 if (!pmf_device_register(self, NULL, mii_phy_resume)) {
664 aprint_error_dev(self, "couldn't establish power handler\n");
665 }
666 }
667
668 void
669 mii_phy_delete_media(struct mii_softc *sc)
670 {
671 struct mii_data *mii = sc->mii_pdata;
672
673 ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
674 }
675
676 int
677 mii_phy_activate(device_t self, enum devact act)
678 {
679
680 switch (act) {
681 case DVACT_DEACTIVATE:
682 /* XXX Invalidate parent's media setting? */
683 return 0;
684 default:
685 return EOPNOTSUPP;
686 }
687 }
688
689 /* ARGSUSED1 */
690 int
691 mii_phy_detach(device_t self, int flags)
692 {
693 struct mii_softc *sc = device_private(self);
694
695 /* No mii_lock because mii_flags should be stable by now. */
696 KASSERT((sc->mii_flags & MIIF_DOINGAUTO) == 0);
697
698 if (sc->mii_flags & MIIF_AUTOTSLEEP)
699 cv_destroy(&sc->mii_nway_cv);
700 else
701 callout_destroy(&sc->mii_nway_ch);
702
703 mii_phy_delete_media(sc);
704
705 return 0;
706 }
707
708 const struct mii_phydesc *
709 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
710 {
711
712 for (; mpd->mpd_oui != 0; mpd++) {
713 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
714 MII_MODEL(ma->mii_id2) == mpd->mpd_model)
715 return mpd;
716 }
717 return NULL;
718 }
719
720 /*
721 * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
722 */
723 u_int
724 mii_phy_flowstatus(struct mii_softc *sc)
725 {
726 uint16_t anar, anlpar;
727
728 KASSERT(mii_locked(sc->mii_pdata));
729
730 if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
731 return 0;
732
733 PHY_READ(sc, MII_ANAR, &anar);
734 PHY_READ(sc, MII_ANLPAR, &anlpar);
735
736 /* For 1000baseX, the bits are in a different location. */
737 if (sc->mii_flags & MIIF_IS_1000X) {
738 anar <<= 3;
739 anlpar <<= 3;
740 }
741
742 if ((anar & ANAR_PAUSE_SYM) & (anlpar & ANLPAR_PAUSE_SYM))
743 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
744
745 if ((anar & ANAR_PAUSE_SYM) == 0) {
746 if ((anar & ANAR_PAUSE_ASYM) &&
747 ((anlpar & ANLPAR_PAUSE_TOWARDS) == ANLPAR_PAUSE_TOWARDS))
748 return (IFM_FLOW | IFM_ETH_TXPAUSE);
749 else
750 return 0;
751 }
752
753 if ((anar & ANAR_PAUSE_ASYM) == 0) {
754 if (anlpar & ANLPAR_PAUSE_SYM)
755 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
756 else
757 return 0;
758 }
759
760 switch ((anlpar & ANLPAR_PAUSE_TOWARDS)) {
761 case ANLPAR_PAUSE_NONE:
762 return 0;
763
764 case ANLPAR_PAUSE_ASYM:
765 return (IFM_FLOW | IFM_ETH_RXPAUSE);
766
767 default:
768 return (IFM_FLOW | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
769 }
770 /* NOTREACHED */
771 }
772
773 bool
774 mii_phy_resume(device_t dv, const pmf_qual_t *qual)
775 {
776 struct mii_softc *sc = device_private(dv);
777
778 mii_lock(sc->mii_pdata);
779 PHY_RESET(sc);
780 bool rv = PHY_SERVICE(sc, sc->mii_pdata, MII_MEDIACHG) == 0;
781 mii_unlock(sc->mii_pdata);
782
783 return rv;
784 }
785
786
787 /*
788 * Given an ifmedia_entry, return the corresponding ANAR value.
789 */
790 uint16_t
791 mii_anar(struct ifmedia_entry *ife)
792 {
793
794 #ifdef DIAGNOSTIC
795 if (ife->ifm_data >= MII_NMEDIA)
796 panic("mii_anar");
797 #endif
798
799 return mii_media_table[ife->ifm_data].mm_anar;
800 }
801