mii_physubr.c revision 1.100 1 /* $NetBSD: mii_physubr.c,v 1.100 2022/08/20 11:12:46 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.100 2022/08/20 11:12:46 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 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) {
445 callout_halt(&sc->mii_nway_ch,
446 sc->mii_pdata->mii_media.ifm_lock);
447 }
448 }
449 KASSERT((sc->mii_flags & MIIF_DOINGAUTO) == 0);
450 }
451
452 void
453 mii_phy_status(struct mii_softc *sc)
454 {
455
456 KASSERT(mii_locked(sc->mii_pdata));
457 PHY_STATUS(sc);
458 }
459
460 void
461 mii_phy_update(struct mii_softc *sc, int cmd)
462 {
463 struct mii_data *mii = sc->mii_pdata;
464 u_int mii_media_active;
465 int mii_media_status;
466
467 KASSERT(mii_locked(mii));
468
469 mii_media_active = mii->mii_media_active;
470 mii_media_status = mii->mii_media_status;
471
472 if (sc->mii_media_active != mii_media_active ||
473 sc->mii_media_status != mii_media_status ||
474 cmd == MII_MEDIACHG) {
475 mii_phy_statusmsg(sc);
476 (*mii->mii_statchg)(mii->mii_ifp);
477 sc->mii_media_active = mii_media_active;
478 sc->mii_media_status = mii_media_status;
479 }
480 }
481
482 static void
483 mii_phy_statusmsg(struct mii_softc *sc)
484 {
485 struct mii_data *mii = sc->mii_pdata;
486 struct ifnet *ifp = mii->mii_ifp;
487
488 KASSERT(mii_locked(mii));
489
490 if (mii->mii_media_status & IFM_AVALID) {
491 if (mii->mii_media_status & IFM_ACTIVE)
492 if_link_state_change(ifp, LINK_STATE_UP);
493 else
494 if_link_state_change(ifp, LINK_STATE_DOWN);
495 } else
496 if_link_state_change(ifp, LINK_STATE_UNKNOWN);
497
498 /* XXX NET_MPSAFE */
499 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
500 }
501
502 /*
503 * Initialize generic PHY media based on BMSR, called when a PHY is
504 * attached. We expect to be set up to print a comma-separated list
505 * of media names. Does not print a newline.
506 */
507 void
508 mii_phy_add_media(struct mii_softc *sc)
509 {
510 struct mii_data *mii = sc->mii_pdata;
511 device_t self = sc->mii_dev;
512 const char *sep = "";
513 int fdx = 0;
514
515 aprint_normal_dev(self, "");
516 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
517 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) {
518 aprint_error("no media present\n");
519 goto out;
520 }
521
522 /*
523 * Set the autonegotiation timer for 10/100 media. Gigabit media is
524 * handled below.
525 */
526 mii_lock(mii);
527 sc->mii_anegticks = MII_ANEGTICKS;
528 mii_unlock(mii);
529
530 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
531 #define PRINT(n) aprint_normal("%s%s", sep, (n)); sep = ", "
532
533 /* This flag is static; no need to lock. */
534 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
535 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
536 MII_MEDIA_NONE);
537
538 /*
539 * There are different interpretations for the bits in
540 * HomePNA PHYs. And there is really only one media type
541 * that is supported. This flag is also static, and so
542 * no need to lock.
543 */
544 if (sc->mii_flags & MIIF_IS_HPNA) {
545 if (sc->mii_capabilities & BMSR_10THDX) {
546 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
547 sc->mii_inst),
548 MII_MEDIA_10_T);
549 PRINT("HomePNA1");
550 }
551 goto out;
552 }
553
554 if (sc->mii_capabilities & BMSR_10THDX) {
555 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
556 MII_MEDIA_10_T);
557 PRINT("10baseT");
558 }
559 if (sc->mii_capabilities & BMSR_10TFDX) {
560 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
561 MII_MEDIA_10_T_FDX);
562 PRINT("10baseT-FDX");
563 fdx = 1;
564 }
565 if (sc->mii_capabilities & BMSR_100TXHDX) {
566 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
567 MII_MEDIA_100_TX);
568 PRINT("100baseTX");
569 }
570 if (sc->mii_capabilities & BMSR_100TXFDX) {
571 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
572 MII_MEDIA_100_TX_FDX);
573 PRINT("100baseTX-FDX");
574 fdx = 1;
575 }
576 if (sc->mii_capabilities & BMSR_100T4) {
577 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
578 MII_MEDIA_100_T4);
579 PRINT("100baseT4");
580 }
581
582 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
583 /*
584 * XXX Right now only handle 1000SX and 1000TX. Need
585 * XXX to handle 1000LX and 1000CX some how.
586 *
587 * Note since it can take 5 seconds to auto-negotiate
588 * a gigabit link, we make anegticks 10 seconds for
589 * all the gigabit media types.
590 */
591 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
592 mii_lock(mii);
593 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
594 sc->mii_flags |= MIIF_IS_1000X;
595 mii_unlock(mii);
596 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
597 sc->mii_inst), MII_MEDIA_1000_X);
598 PRINT("1000baseSX");
599 }
600 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
601 mii_lock(mii);
602 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
603 sc->mii_flags |= MIIF_IS_1000X;
604 mii_unlock(mii);
605 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
606 sc->mii_inst), MII_MEDIA_1000_X_FDX);
607 PRINT("1000baseSX-FDX");
608 fdx = 1;
609 }
610
611 /*
612 * 1000baseT media needs to be able to manipulate
613 * master/slave mode. We set IFM_ETH_MASTER in
614 * the "don't care mask" and filter it out when
615 * the media is set.
616 *
617 * All 1000baseT PHYs have a 1000baseT control register.
618 */
619 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
620 mii_lock(mii);
621 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
622 sc->mii_flags |= MIIF_HAVE_GTCR;
623 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
624 mii_unlock(mii);
625 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
626 sc->mii_inst), MII_MEDIA_1000_T);
627 PRINT("1000baseT");
628 }
629 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
630 mii_lock(mii);
631 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
632 sc->mii_flags |= MIIF_HAVE_GTCR;
633 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
634 mii_unlock(mii);
635 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
636 sc->mii_inst), MII_MEDIA_1000_T_FDX);
637 PRINT("1000baseT-FDX");
638 fdx = 1;
639 }
640 }
641
642 if (sc->mii_capabilities & BMSR_ANEG) {
643 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
644 MII_NMEDIA); /* intentionally invalid index */
645 PRINT("auto");
646 }
647 #undef ADD
648 #undef PRINT
649 /* This flag is static; no need to lock. */
650 if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE)) {
651 mii_lock(mii);
652 mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
653 mii_unlock(mii);
654 }
655 out:
656 aprint_normal("\n");
657 if (!pmf_device_register(self, NULL, mii_phy_resume)) {
658 aprint_error_dev(self, "couldn't establish power handler\n");
659 }
660 }
661
662 void
663 mii_phy_delete_media(struct mii_softc *sc)
664 {
665 struct mii_data *mii = sc->mii_pdata;
666
667 ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
668 }
669
670 int
671 mii_phy_activate(device_t self, enum devact act)
672 {
673
674 switch (act) {
675 case DVACT_DEACTIVATE:
676 /* XXX Invalidate parent's media setting? */
677 return 0;
678 default:
679 return EOPNOTSUPP;
680 }
681 }
682
683 /* ARGSUSED1 */
684 int
685 mii_phy_detach(device_t self, int flags)
686 {
687 struct mii_softc *sc = device_private(self);
688
689 /* No mii_lock because mii_flags should be stable by now. */
690 KASSERT((sc->mii_flags & MIIF_DOINGAUTO) == 0);
691
692 if (sc->mii_flags & MIIF_AUTOTSLEEP)
693 cv_destroy(&sc->mii_nway_cv);
694 else
695 callout_destroy(&sc->mii_nway_ch);
696
697 mii_phy_delete_media(sc);
698
699 return 0;
700 }
701
702 const struct mii_phydesc *
703 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
704 {
705
706 for (; mpd->mpd_oui != 0; mpd++) {
707 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
708 MII_MODEL(ma->mii_id2) == mpd->mpd_model)
709 return mpd;
710 }
711 return NULL;
712 }
713
714 /*
715 * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
716 */
717 u_int
718 mii_phy_flowstatus(struct mii_softc *sc)
719 {
720 uint16_t anar, anlpar;
721
722 KASSERT(mii_locked(sc->mii_pdata));
723
724 if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
725 return 0;
726
727 PHY_READ(sc, MII_ANAR, &anar);
728 PHY_READ(sc, MII_ANLPAR, &anlpar);
729
730 /* For 1000baseX, the bits are in a different location. */
731 if (sc->mii_flags & MIIF_IS_1000X) {
732 anar <<= 3;
733 anlpar <<= 3;
734 }
735
736 if ((anar & ANAR_PAUSE_SYM) & (anlpar & ANLPAR_PAUSE_SYM))
737 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
738
739 if ((anar & ANAR_PAUSE_SYM) == 0) {
740 if ((anar & ANAR_PAUSE_ASYM) &&
741 ((anlpar & ANLPAR_PAUSE_TOWARDS) == ANLPAR_PAUSE_TOWARDS))
742 return (IFM_FLOW | IFM_ETH_TXPAUSE);
743 else
744 return 0;
745 }
746
747 if ((anar & ANAR_PAUSE_ASYM) == 0) {
748 if (anlpar & ANLPAR_PAUSE_SYM)
749 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
750 else
751 return 0;
752 }
753
754 switch ((anlpar & ANLPAR_PAUSE_TOWARDS)) {
755 case ANLPAR_PAUSE_NONE:
756 return 0;
757
758 case ANLPAR_PAUSE_ASYM:
759 return (IFM_FLOW | IFM_ETH_RXPAUSE);
760
761 default:
762 return (IFM_FLOW | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
763 }
764 /* NOTREACHED */
765 }
766
767 bool
768 mii_phy_resume(device_t dv, const pmf_qual_t *qual)
769 {
770 struct mii_softc *sc = device_private(dv);
771
772 mii_lock(sc->mii_pdata);
773 PHY_RESET(sc);
774 bool rv = PHY_SERVICE(sc, sc->mii_pdata, MII_MEDIACHG) == 0;
775 mii_unlock(sc->mii_pdata);
776
777 return rv;
778 }
779
780
781 /*
782 * Given an ifmedia_entry, return the corresponding ANAR value.
783 */
784 uint16_t
785 mii_anar(struct ifmedia_entry *ife)
786 {
787
788 #ifdef DIAGNOSTIC
789 if (ife->ifm_data >= MII_NMEDIA)
790 panic("mii_anar");
791 #endif
792
793 return mii_media_table[ife->ifm_data].mm_anar;
794 }
795