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