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