1 /* $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
11 * NASA Ames Research Center.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * MII bus layer, glues MII-capable network interface drivers to sharable
40 * PHY drivers. This exports an interface compatible with BSD/OS 3.0's,
41 * plus some NetBSD extensions.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/socket.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
53 #include <net/if_var.h>
54 #include <net/if_media.h>
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
59 MODULE_VERSION(miibus, 1);
61 #include "miibus_if.h"
63 static device_attach_t miibus_attach;
64 static bus_child_detached_t miibus_child_detached;
65 static bus_child_location_t miibus_child_location;
66 static bus_child_pnpinfo_t miibus_child_pnpinfo;
67 static device_detach_t miibus_detach;
68 static bus_hinted_child_t miibus_hinted_child;
69 static bus_print_child_t miibus_print_child;
70 static device_probe_t miibus_probe;
71 static bus_read_ivar_t miibus_read_ivar;
72 static miibus_readreg_t miibus_readreg;
73 static miibus_statchg_t miibus_statchg;
74 static miibus_writereg_t miibus_writereg;
75 static miibus_linkchg_t miibus_linkchg;
76 static miibus_mediainit_t miibus_mediainit;
78 static unsigned char mii_bitreverse(unsigned char x);
80 static device_method_t miibus_methods[] = {
81 /* device interface */
82 DEVMETHOD(device_probe, miibus_probe),
83 DEVMETHOD(device_attach, miibus_attach),
84 DEVMETHOD(device_detach, miibus_detach),
85 DEVMETHOD(device_shutdown, bus_generic_shutdown),
88 DEVMETHOD(bus_print_child, miibus_print_child),
89 DEVMETHOD(bus_read_ivar, miibus_read_ivar),
90 DEVMETHOD(bus_child_detached, miibus_child_detached),
91 DEVMETHOD(bus_child_pnpinfo, miibus_child_pnpinfo),
92 DEVMETHOD(bus_child_location, miibus_child_location),
93 DEVMETHOD(bus_hinted_child, miibus_hinted_child),
96 DEVMETHOD(miibus_readreg, miibus_readreg),
97 DEVMETHOD(miibus_writereg, miibus_writereg),
98 DEVMETHOD(miibus_statchg, miibus_statchg),
99 DEVMETHOD(miibus_linkchg, miibus_linkchg),
100 DEVMETHOD(miibus_mediainit, miibus_mediainit),
105 devclass_t miibus_devclass;
107 driver_t miibus_driver = {
110 sizeof(struct mii_data)
113 struct miibus_ivars {
115 ifm_change_cb_t ifmedia_upd;
116 ifm_stat_cb_t ifmedia_sts;
122 miibus_probe(device_t dev)
125 device_set_desc(dev, "MII bus");
127 return (BUS_PROBE_SPECIFIC);
131 miibus_attach(device_t dev)
133 struct miibus_ivars *ivars;
134 struct mii_attach_args *ma;
135 struct mii_data *mii;
139 mii = device_get_softc(dev);
140 if (device_get_children(dev, &children, &nchildren) == 0) {
141 for (i = 0; i < nchildren; i++) {
142 ma = device_get_ivars(children[i]);
145 free(children, M_TEMP);
147 if (nchildren == 0) {
148 device_printf(dev, "cannot get children\n");
151 ivars = device_get_ivars(dev);
152 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
154 mii->mii_ifp = ivars->ifp;
155 if_setcapabilitiesbit(mii->mii_ifp, IFCAP_LINKSTATE, 0);
156 if_setcapenablebit(mii->mii_ifp, IFCAP_LINKSTATE, 0);
157 LIST_INIT(&mii->mii_phys);
159 return (bus_generic_attach(dev));
163 miibus_detach(device_t dev)
165 struct mii_data *mii;
166 struct miibus_ivars *ivars;
168 ivars = device_get_ivars(dev);
169 bus_generic_detach(dev);
170 mii = device_get_softc(dev);
171 ifmedia_removeall(&mii->mii_media);
172 free(ivars, M_DEVBUF);
179 miibus_child_detached(device_t dev, device_t child)
181 struct mii_attach_args *args;
183 args = device_get_ivars(child);
184 free(args, M_DEVBUF);
188 miibus_print_child(device_t dev, device_t child)
190 struct mii_attach_args *ma;
193 ma = device_get_ivars(child);
194 retval = bus_print_child_header(dev, child);
195 retval += printf(" PHY %d", ma->mii_phyno);
196 retval += bus_print_child_footer(dev, child);
202 miibus_read_ivar(device_t dev, device_t child __unused, int which,
205 struct miibus_ivars *ivars;
208 * NB: this uses the instance variables of the miibus rather than
211 ivars = device_get_ivars(dev);
213 case MIIBUS_IVAR_FLAGS:
214 *result = ivars->mii_flags;
223 miibus_child_pnpinfo(device_t dev __unused, device_t child, struct sbuf *sb)
225 struct mii_attach_args *ma;
227 ma = device_get_ivars(child);
228 sbuf_printf(sb, "oui=0x%x model=0x%x rev=0x%x",
229 MII_OUI(ma->mii_id1, ma->mii_id2),
230 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
235 miibus_child_location(device_t dev __unused, device_t child, struct sbuf *sb)
237 struct mii_attach_args *ma;
239 ma = device_get_ivars(child);
240 sbuf_printf(sb, "phyno=%d", ma->mii_phyno);
245 miibus_hinted_child(device_t dev, const char *name, int unit)
247 struct miibus_ivars *ivars;
248 struct mii_attach_args *args, *ma;
249 device_t *children, phy;
253 if (resource_int_value(name, unit, "phyno", &val) != 0)
255 if (device_get_children(dev, &children, &nchildren) != 0)
258 for (i = 0; i < nchildren; i++) {
259 args = device_get_ivars(children[i]);
260 if (args->mii_phyno == val) {
265 free(children, M_TEMP);
268 * Don't add a PHY that was automatically identified by having media
269 * in its BMSR twice, only allow to alter its attach arguments.
272 ma = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
276 phy = device_add_child(dev, name, unit);
281 ivars = device_get_ivars(dev);
283 ma->mii_offset = ivars->mii_offset++;
286 ma->mii_capmask = BMSR_DEFCAPMASK;
287 device_set_ivars(phy, ma);
290 if (resource_int_value(name, unit, "id1", &val) == 0)
292 if (resource_int_value(name, unit, "id2", &val) == 0)
294 if (resource_int_value(name, unit, "capmask", &val) == 0)
295 ma->mii_capmask = val;
299 miibus_readreg(device_t dev, int phy, int reg)
303 parent = device_get_parent(dev);
304 return (MIIBUS_READREG(parent, phy, reg));
308 miibus_writereg(device_t dev, int phy, int reg, int data)
312 parent = device_get_parent(dev);
313 return (MIIBUS_WRITEREG(parent, phy, reg, data));
317 miibus_statchg(device_t dev)
320 struct mii_data *mii;
322 parent = device_get_parent(dev);
323 MIIBUS_STATCHG(parent);
325 mii = device_get_softc(dev);
326 if_setbaudrate(mii->mii_ifp, ifmedia_baudrate(mii->mii_media_active));
330 miibus_linkchg(device_t dev)
332 struct mii_data *mii;
336 parent = device_get_parent(dev);
337 MIIBUS_LINKCHG(parent);
339 mii = device_get_softc(dev);
341 if (mii->mii_media_status & IFM_AVALID) {
342 if (mii->mii_media_status & IFM_ACTIVE)
343 link_state = LINK_STATE_UP;
345 link_state = LINK_STATE_DOWN;
347 link_state = LINK_STATE_UNKNOWN;
348 if_link_state_change(mii->mii_ifp, link_state);
352 miibus_mediainit(device_t dev)
354 struct mii_data *mii;
355 struct ifmedia_entry *m;
358 /* Poke the parent in case it has any media of its own to add. */
359 MIIBUS_MEDIAINIT(device_get_parent(dev));
361 mii = device_get_softc(dev);
362 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
363 media = m->ifm_media;
364 if (media == (IFM_ETHER | IFM_AUTO))
368 ifmedia_set(&mii->mii_media, media);
372 * Helper function used by network interface drivers, attaches the miibus and
373 * the PHYs to the network interface driver parent.
376 mii_attach(device_t dev, device_t *miibus, if_t ifp,
377 ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
378 int phyloc, int offloc, int flags)
380 struct miibus_ivars *ivars;
381 struct mii_attach_args *args, ma;
382 device_t *children, phy;
383 int bmsr, first, i, nchildren, phymax, phymin, rv;
386 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
387 printf("%s: phyloc and offloc specified\n", __func__);
391 if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
392 printf("%s: invalid offloc %d\n", __func__, offloc);
396 if (phyloc == MII_PHY_ANY) {
398 phymax = MII_NPHY - 1;
400 if (phyloc < 0 || phyloc >= MII_NPHY) {
401 printf("%s: invalid phyloc %d\n", __func__, phyloc);
404 phymin = phymax = phyloc;
408 if (*miibus == NULL) {
410 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
414 ivars->ifmedia_upd = ifmedia_upd;
415 ivars->ifmedia_sts = ifmedia_sts;
416 ivars->mii_flags = flags;
417 *miibus = device_add_child(dev, "miibus", -1);
418 if (*miibus == NULL) {
422 device_set_ivars(*miibus, ivars);
424 ivars = device_get_ivars(*miibus);
425 if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd ||
426 ivars->ifmedia_sts != ifmedia_sts ||
427 ivars->mii_flags != flags) {
428 printf("%s: non-matching invariant\n", __func__);
432 * Assignment of the attach arguments mii_data for the first
433 * pass is done in miibus_attach(), i.e. once the miibus softc
434 * has been allocated.
436 ma.mii_data = device_get_softc(*miibus);
439 ma.mii_capmask = capmask;
441 if (resource_int_value(device_get_name(*miibus),
442 device_get_unit(*miibus), "phymask", &phymask) != 0)
443 phymask = 0xffffffff;
445 if (device_get_children(*miibus, &children, &nchildren) != 0) {
449 ivars->mii_offset = 0;
450 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
452 * Make sure we haven't already configured a PHY at this
453 * address. This allows mii_attach() to be called
456 for (i = 0; i < nchildren; i++) {
457 args = device_get_ivars(children[i]);
458 if (args->mii_phyno == ma.mii_phyno) {
460 * Yes, there is already something
461 * configured at this address.
468 * Check to see if there is a PHY at this address. Note,
469 * many braindead PHYs report 0/0 in their ID registers,
470 * so we test for media in the BMSR.
472 bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR);
473 if (bmsr == 0 || bmsr == 0xffff ||
474 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
475 /* Assume no PHY at this address. */
480 * There is a PHY at this address. If we were given an
481 * `offset' locator, skip this PHY if it doesn't match.
483 if (offloc != MII_OFFSET_ANY && offloc != ivars->mii_offset)
487 * Skip this PHY if it's not included in the phymask hint.
489 if ((phymask & (1 << ma.mii_phyno)) == 0)
493 * Extract the IDs. Braindead PHYs will be handled by
494 * the `ukphy' driver, as we have no ID information to
497 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
498 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
500 ma.mii_offset = ivars->mii_offset;
501 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
505 bcopy((char *)&ma, (char *)args, sizeof(ma));
506 phy = device_add_child(*miibus, NULL, -1);
508 free(args, M_DEVBUF);
511 device_set_ivars(phy, args);
515 free(children, M_TEMP);
518 rv = device_set_driver(*miibus, &miibus_driver);
521 bus_enumerate_hinted_children(*miibus);
522 rv = device_get_children(*miibus, &children, &nchildren);
525 free(children, M_TEMP);
526 if (nchildren == 0) {
530 rv = bus_generic_attach(dev);
534 /* Attaching of the PHY drivers is done in miibus_attach(). */
537 rv = bus_generic_attach(*miibus);
545 device_delete_child(dev, *miibus);
546 free(ivars, M_DEVBUF);
553 * Media changed; notify all PHYs.
556 mii_mediachg(struct mii_data *mii)
558 struct mii_softc *child;
559 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
562 mii->mii_media_status = 0;
563 mii->mii_media_active = IFM_NONE;
565 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
567 * If the media indicates a different PHY instance,
570 if (IFM_INST(ife->ifm_media) != child->mii_inst) {
571 if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
572 device_printf(child->mii_dev, "%s: "
573 "can't handle non-zero PHY instance %d\n",
574 __func__, child->mii_inst);
577 PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
581 rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
589 * Call the PHY tick routines, used during autonegotiation.
592 mii_tick(struct mii_data *mii)
594 struct mii_softc *child;
595 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
597 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
599 * If this PHY instance isn't currently selected, just skip
602 if (IFM_INST(ife->ifm_media) != child->mii_inst)
604 (void)PHY_SERVICE(child, mii, MII_TICK);
609 * Get media status from PHYs.
612 mii_pollstat(struct mii_data *mii)
614 struct mii_softc *child;
615 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
617 mii->mii_media_status = 0;
618 mii->mii_media_active = IFM_NONE;
620 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
622 * If we're not polling this PHY instance, just skip it.
624 if (IFM_INST(ife->ifm_media) != child->mii_inst)
626 (void)PHY_SERVICE(child, mii, MII_POLLSTAT);
631 mii_bitreverse(unsigned char x)
633 static unsigned const char nibbletab[16] = {
634 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
637 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
641 mii_oui(u_int id1, u_int id2)
645 h = (id1 << 6) | (id2 >> 10);
647 return ((mii_bitreverse(h >> 16) << 16) |
648 (mii_bitreverse((h >> 8) & 0xff) << 8) |
649 mii_bitreverse(h & 0xff));
653 mii_phy_mac_match(struct mii_softc *mii, const char *name)
656 return (strcmp(device_get_name(device_get_parent(mii->mii_dev)),
661 mii_dev_mac_match(device_t parent, const char *name)
664 return (strcmp(device_get_name(device_get_parent(
665 device_get_parent(parent))), name) == 0);
669 mii_phy_mac_softc(struct mii_softc *mii)
672 return (device_get_softc(device_get_parent(mii->mii_dev)));
676 mii_dev_mac_softc(device_t parent)
679 return (device_get_softc(device_get_parent(device_get_parent(parent))));