1 /* $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * MII bus layer, glues MII-capable network interface drivers to sharable
38 * PHY drivers. This exports an interface compatible with BSD/OS 3.0's,
39 * plus some NetBSD extensions.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/socket.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
50 #include <net/if_media.h>
52 #include <dev/mii/mii.h>
53 #include <dev/mii/miivar.h>
55 MODULE_VERSION(miibus, 1);
57 #include "miibus_if.h"
59 static device_attach_t miibus_attach;
60 static bus_child_location_str_t miibus_child_location_str;
61 static bus_child_pnpinfo_str_t miibus_child_pnpinfo_str;
62 static device_detach_t miibus_detach;
63 static bus_hinted_child_t miibus_hinted_child;
64 static bus_print_child_t miibus_print_child;
65 static device_probe_t miibus_probe;
66 static bus_read_ivar_t miibus_read_ivar;
67 static miibus_readreg_t miibus_readreg;
68 static miibus_statchg_t miibus_statchg;
69 static miibus_writereg_t miibus_writereg;
70 static miibus_linkchg_t miibus_linkchg;
71 static miibus_mediainit_t miibus_mediainit;
73 static unsigned char mii_bitreverse(unsigned char x);
75 static device_method_t miibus_methods[] = {
76 /* device interface */
77 DEVMETHOD(device_probe, miibus_probe),
78 DEVMETHOD(device_attach, miibus_attach),
79 DEVMETHOD(device_detach, miibus_detach),
80 DEVMETHOD(device_shutdown, bus_generic_shutdown),
83 DEVMETHOD(bus_print_child, miibus_print_child),
84 DEVMETHOD(bus_read_ivar, miibus_read_ivar),
85 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
86 DEVMETHOD(bus_child_location_str, miibus_child_location_str),
87 DEVMETHOD(bus_hinted_child, miibus_hinted_child),
90 DEVMETHOD(miibus_readreg, miibus_readreg),
91 DEVMETHOD(miibus_writereg, miibus_writereg),
92 DEVMETHOD(miibus_statchg, miibus_statchg),
93 DEVMETHOD(miibus_linkchg, miibus_linkchg),
94 DEVMETHOD(miibus_mediainit, miibus_mediainit),
99 devclass_t miibus_devclass;
101 driver_t miibus_driver = {
104 sizeof(struct mii_data)
107 struct miibus_ivars {
109 ifm_change_cb_t ifmedia_upd;
110 ifm_stat_cb_t ifmedia_sts;
116 miibus_probe(device_t dev)
119 device_set_desc(dev, "MII bus");
121 return (BUS_PROBE_SPECIFIC);
125 miibus_attach(device_t dev)
127 struct miibus_ivars *ivars;
128 struct mii_attach_args *ma;
129 struct mii_data *mii;
133 mii = device_get_softc(dev);
134 if (device_get_children(dev, &children, &nchildren) == 0) {
135 for (i = 0; i < nchildren; i++) {
136 ma = device_get_ivars(children[i]);
139 free(children, M_TEMP);
141 if (nchildren == 0) {
142 device_printf(dev, "cannot get children\n");
145 ivars = device_get_ivars(dev);
146 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
148 mii->mii_ifp = ivars->ifp;
149 mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE;
150 mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE;
151 LIST_INIT(&mii->mii_phys);
153 return (bus_generic_attach(dev));
157 miibus_detach(device_t dev)
159 struct mii_data *mii;
161 bus_generic_detach(dev);
162 mii = device_get_softc(dev);
163 ifmedia_removeall(&mii->mii_media);
170 miibus_print_child(device_t dev, device_t child)
172 struct mii_attach_args *ma;
175 ma = device_get_ivars(child);
176 retval = bus_print_child_header(dev, child);
177 retval += printf(" PHY %d", ma->mii_phyno);
178 retval += bus_print_child_footer(dev, child);
184 miibus_read_ivar(device_t dev, device_t child __unused, int which,
187 struct miibus_ivars *ivars;
190 * NB: this uses the instance variables of the miibus rather than
193 ivars = device_get_ivars(dev);
195 case MIIBUS_IVAR_FLAGS:
196 *result = ivars->mii_flags;
205 miibus_child_pnpinfo_str(device_t dev __unused, device_t child, char *buf,
208 struct mii_attach_args *ma;
210 ma = device_get_ivars(child);
211 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
212 MII_OUI(ma->mii_id1, ma->mii_id2),
213 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
218 miibus_child_location_str(device_t dev __unused, device_t child, char *buf,
221 struct mii_attach_args *ma;
223 ma = device_get_ivars(child);
224 snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
229 miibus_hinted_child(device_t dev, const char *name, int unit)
231 struct miibus_ivars *ivars;
232 struct mii_attach_args *args, *ma;
233 device_t *children, phy;
237 if (resource_int_value(name, unit, "phyno", &val) != 0)
239 if (device_get_children(dev, &children, &nchildren) != 0)
242 for (i = 0; i < nchildren; i++) {
243 args = device_get_ivars(children[i]);
244 if (args->mii_phyno == val) {
249 free(children, M_TEMP);
252 * Don't add a PHY that was automatically identified by having media
253 * in its BMSR twice, only allow to alter its attach arguments.
256 ma = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
260 phy = device_add_child(dev, name, unit);
265 ivars = device_get_ivars(dev);
267 ma->mii_offset = ivars->mii_offset++;
270 ma->mii_capmask = BMSR_DEFCAPMASK;
271 device_set_ivars(phy, ma);
274 if (resource_int_value(name, unit, "id1", &val) == 0)
276 if (resource_int_value(name, unit, "id2", &val) == 0)
278 if (resource_int_value(name, unit, "capmask", &val) == 0)
279 ma->mii_capmask = val;
283 miibus_readreg(device_t dev, int phy, int reg)
287 parent = device_get_parent(dev);
288 return (MIIBUS_READREG(parent, phy, reg));
292 miibus_writereg(device_t dev, int phy, int reg, int data)
296 parent = device_get_parent(dev);
297 return (MIIBUS_WRITEREG(parent, phy, reg, data));
301 miibus_statchg(device_t dev)
304 struct mii_data *mii;
306 parent = device_get_parent(dev);
307 MIIBUS_STATCHG(parent);
309 mii = device_get_softc(dev);
310 mii->mii_ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
314 miibus_linkchg(device_t dev)
316 struct mii_data *mii;
320 parent = device_get_parent(dev);
321 MIIBUS_LINKCHG(parent);
323 mii = device_get_softc(dev);
325 if (mii->mii_media_status & IFM_AVALID) {
326 if (mii->mii_media_status & IFM_ACTIVE)
327 link_state = LINK_STATE_UP;
329 link_state = LINK_STATE_DOWN;
331 link_state = LINK_STATE_UNKNOWN;
332 if_link_state_change(mii->mii_ifp, link_state);
336 miibus_mediainit(device_t dev)
338 struct mii_data *mii;
339 struct ifmedia_entry *m;
342 /* Poke the parent in case it has any media of its own to add. */
343 MIIBUS_MEDIAINIT(device_get_parent(dev));
345 mii = device_get_softc(dev);
346 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
347 media = m->ifm_media;
348 if (media == (IFM_ETHER | IFM_AUTO))
352 ifmedia_set(&mii->mii_media, media);
356 * Helper function used by network interface drivers, attaches the miibus and
357 * the PHYs to the network interface driver parent.
360 mii_attach(device_t dev, device_t *miibus, struct ifnet *ifp,
361 ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
362 int phyloc, int offloc, int flags)
364 struct miibus_ivars *ivars;
365 struct mii_attach_args *args, ma;
366 device_t *children, phy;
367 int bmsr, first, i, nchildren, phymax, phymin, rv;
370 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
371 printf("%s: phyloc and offloc specified\n", __func__);
375 if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
376 printf("%s: invalid offloc %d\n", __func__, offloc);
380 if (phyloc == MII_PHY_ANY) {
382 phymax = MII_NPHY - 1;
384 if (phyloc < 0 || phyloc >= MII_NPHY) {
385 printf("%s: invalid phyloc %d\n", __func__, phyloc);
388 phymin = phymax = phyloc;
392 if (*miibus == NULL) {
394 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
398 ivars->ifmedia_upd = ifmedia_upd;
399 ivars->ifmedia_sts = ifmedia_sts;
400 ivars->mii_flags = flags;
401 *miibus = device_add_child(dev, "miibus", -1);
402 if (*miibus == NULL) {
406 device_set_ivars(*miibus, ivars);
408 ivars = device_get_ivars(*miibus);
409 if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd ||
410 ivars->ifmedia_sts != ifmedia_sts ||
411 ivars->mii_flags != flags) {
412 printf("%s: non-matching invariant\n", __func__);
416 * Assignment of the attach arguments mii_data for the first
417 * pass is done in miibus_attach(), i.e. once the miibus softc
418 * has been allocated.
420 ma.mii_data = device_get_softc(*miibus);
423 ma.mii_capmask = capmask;
425 if (resource_int_value(device_get_name(*miibus),
426 device_get_unit(*miibus), "phymask", &phymask) != 0)
427 phymask = 0xffffffff;
429 if (device_get_children(*miibus, &children, &nchildren) != 0) {
433 ivars->mii_offset = 0;
434 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
436 * Make sure we haven't already configured a PHY at this
437 * address. This allows mii_attach() to be called
440 for (i = 0; i < nchildren; i++) {
441 args = device_get_ivars(children[i]);
442 if (args->mii_phyno == ma.mii_phyno) {
444 * Yes, there is already something
445 * configured at this address.
452 * Check to see if there is a PHY at this address. Note,
453 * many braindead PHYs report 0/0 in their ID registers,
454 * so we test for media in the BMSR.
456 bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR);
457 if (bmsr == 0 || bmsr == 0xffff ||
458 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
459 /* Assume no PHY at this address. */
464 * There is a PHY at this address. If we were given an
465 * `offset' locator, skip this PHY if it doesn't match.
467 if (offloc != MII_OFFSET_ANY && offloc != ivars->mii_offset)
471 * Skip this PHY if it's not included in the phymask hint.
473 if ((phymask & (1 << ma.mii_phyno)) == 0)
477 * Extract the IDs. Braindead PHYs will be handled by
478 * the `ukphy' driver, as we have no ID information to
481 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
482 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
484 ma.mii_offset = ivars->mii_offset;
485 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
489 bcopy((char *)&ma, (char *)args, sizeof(ma));
490 phy = device_add_child(*miibus, NULL, -1);
492 free(args, M_DEVBUF);
495 device_set_ivars(phy, args);
499 free(children, M_TEMP);
502 rv = device_set_driver(*miibus, &miibus_driver);
505 bus_enumerate_hinted_children(*miibus);
506 rv = device_get_children(*miibus, &children, &nchildren);
509 free(children, M_TEMP);
510 if (nchildren == 0) {
514 rv = bus_generic_attach(dev);
518 /* Attaching of the PHY drivers is done in miibus_attach(). */
521 rv = bus_generic_attach(*miibus);
529 device_delete_child(dev, *miibus);
530 free(ivars, M_DEVBUF);
537 * Media changed; notify all PHYs.
540 mii_mediachg(struct mii_data *mii)
542 struct mii_softc *child;
543 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
546 mii->mii_media_status = 0;
547 mii->mii_media_active = IFM_NONE;
549 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
551 * If the media indicates a different PHY instance,
554 if (IFM_INST(ife->ifm_media) != child->mii_inst) {
555 if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
556 device_printf(child->mii_dev, "%s: "
557 "can't handle non-zero PHY instance %d\n",
558 __func__, child->mii_inst);
561 PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
565 rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
573 * Call the PHY tick routines, used during autonegotiation.
576 mii_tick(struct mii_data *mii)
578 struct mii_softc *child;
579 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
581 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
583 * If this PHY instance isn't currently selected, just skip
586 if (IFM_INST(ife->ifm_media) != child->mii_inst)
588 (void)PHY_SERVICE(child, mii, MII_TICK);
593 * Get media status from PHYs.
596 mii_pollstat(struct mii_data *mii)
598 struct mii_softc *child;
599 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
601 mii->mii_media_status = 0;
602 mii->mii_media_active = IFM_NONE;
604 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
606 * If we're not polling this PHY instance, just skip it.
608 if (IFM_INST(ife->ifm_media) != child->mii_inst)
610 (void)PHY_SERVICE(child, mii, MII_POLLSTAT);
615 * Inform the PHYs that the interface is down.
618 mii_down(struct mii_data *mii)
620 struct mii_softc *child;
622 LIST_FOREACH(child, &mii->mii_phys, mii_list)
627 mii_bitreverse(unsigned char x)
629 static unsigned const char nibbletab[16] = {
630 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
633 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
637 mii_oui(u_int id1, u_int id2)
641 h = (id1 << 6) | (id2 >> 10);
643 return ((mii_bitreverse(h >> 16) << 16) |
644 (mii_bitreverse((h >> 8) & 0xff) << 8) |
645 mii_bitreverse(h & 0xff));