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_var.h>
51 #include <net/if_media.h>
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
56 MODULE_VERSION(miibus, 1);
58 #include "miibus_if.h"
60 static device_attach_t miibus_attach;
61 static bus_child_location_str_t miibus_child_location_str;
62 static bus_child_pnpinfo_str_t miibus_child_pnpinfo_str;
63 static device_detach_t miibus_detach;
64 static bus_hinted_child_t miibus_hinted_child;
65 static bus_print_child_t miibus_print_child;
66 static device_probe_t miibus_probe;
67 static bus_read_ivar_t miibus_read_ivar;
68 static miibus_readreg_t miibus_readreg;
69 static miibus_statchg_t miibus_statchg;
70 static miibus_writereg_t miibus_writereg;
71 static miibus_linkchg_t miibus_linkchg;
72 static miibus_mediainit_t miibus_mediainit;
74 static unsigned char mii_bitreverse(unsigned char x);
76 static device_method_t miibus_methods[] = {
77 /* device interface */
78 DEVMETHOD(device_probe, miibus_probe),
79 DEVMETHOD(device_attach, miibus_attach),
80 DEVMETHOD(device_detach, miibus_detach),
81 DEVMETHOD(device_shutdown, bus_generic_shutdown),
84 DEVMETHOD(bus_print_child, miibus_print_child),
85 DEVMETHOD(bus_read_ivar, miibus_read_ivar),
86 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
87 DEVMETHOD(bus_child_location_str, miibus_child_location_str),
88 DEVMETHOD(bus_hinted_child, miibus_hinted_child),
91 DEVMETHOD(miibus_readreg, miibus_readreg),
92 DEVMETHOD(miibus_writereg, miibus_writereg),
93 DEVMETHOD(miibus_statchg, miibus_statchg),
94 DEVMETHOD(miibus_linkchg, miibus_linkchg),
95 DEVMETHOD(miibus_mediainit, miibus_mediainit),
100 devclass_t miibus_devclass;
102 driver_t miibus_driver = {
105 sizeof(struct mii_data)
108 struct miibus_ivars {
110 ifm_change_cb_t ifmedia_upd;
111 ifm_stat_cb_t ifmedia_sts;
117 miibus_probe(device_t dev)
120 device_set_desc(dev, "MII bus");
122 return (BUS_PROBE_SPECIFIC);
126 miibus_attach(device_t dev)
128 struct miibus_ivars *ivars;
129 struct mii_attach_args *ma;
130 struct mii_data *mii;
134 mii = device_get_softc(dev);
135 if (device_get_children(dev, &children, &nchildren) == 0) {
136 for (i = 0; i < nchildren; i++) {
137 ma = device_get_ivars(children[i]);
140 free(children, M_TEMP);
142 if (nchildren == 0) {
143 device_printf(dev, "cannot get children\n");
146 ivars = device_get_ivars(dev);
147 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
149 mii->mii_ifp = ivars->ifp;
150 if_setcapabilitiesbit(mii->mii_ifp, IFCAP_LINKSTATE, 0);
151 if_setcapenablebit(mii->mii_ifp, IFCAP_LINKSTATE, 0);
152 LIST_INIT(&mii->mii_phys);
154 return (bus_generic_attach(dev));
158 miibus_detach(device_t dev)
160 struct mii_data *mii;
162 bus_generic_detach(dev);
163 mii = device_get_softc(dev);
164 ifmedia_removeall(&mii->mii_media);
171 miibus_print_child(device_t dev, device_t child)
173 struct mii_attach_args *ma;
176 ma = device_get_ivars(child);
177 retval = bus_print_child_header(dev, child);
178 retval += printf(" PHY %d", ma->mii_phyno);
179 retval += bus_print_child_footer(dev, child);
185 miibus_read_ivar(device_t dev, device_t child __unused, int which,
188 struct miibus_ivars *ivars;
191 * NB: this uses the instance variables of the miibus rather than
194 ivars = device_get_ivars(dev);
196 case MIIBUS_IVAR_FLAGS:
197 *result = ivars->mii_flags;
206 miibus_child_pnpinfo_str(device_t dev __unused, device_t child, char *buf,
209 struct mii_attach_args *ma;
211 ma = device_get_ivars(child);
212 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
213 MII_OUI(ma->mii_id1, ma->mii_id2),
214 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
219 miibus_child_location_str(device_t dev __unused, device_t child, char *buf,
222 struct mii_attach_args *ma;
224 ma = device_get_ivars(child);
225 snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
230 miibus_hinted_child(device_t dev, const char *name, int unit)
232 struct miibus_ivars *ivars;
233 struct mii_attach_args *args, *ma;
234 device_t *children, phy;
238 if (resource_int_value(name, unit, "phyno", &val) != 0)
240 if (device_get_children(dev, &children, &nchildren) != 0)
243 for (i = 0; i < nchildren; i++) {
244 args = device_get_ivars(children[i]);
245 if (args->mii_phyno == val) {
250 free(children, M_TEMP);
253 * Don't add a PHY that was automatically identified by having media
254 * in its BMSR twice, only allow to alter its attach arguments.
257 ma = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
261 phy = device_add_child(dev, name, unit);
266 ivars = device_get_ivars(dev);
268 ma->mii_offset = ivars->mii_offset++;
271 ma->mii_capmask = BMSR_DEFCAPMASK;
272 device_set_ivars(phy, ma);
275 if (resource_int_value(name, unit, "id1", &val) == 0)
277 if (resource_int_value(name, unit, "id2", &val) == 0)
279 if (resource_int_value(name, unit, "capmask", &val) == 0)
280 ma->mii_capmask = val;
284 miibus_readreg(device_t dev, int phy, int reg)
288 parent = device_get_parent(dev);
289 return (MIIBUS_READREG(parent, phy, reg));
293 miibus_writereg(device_t dev, int phy, int reg, int data)
297 parent = device_get_parent(dev);
298 return (MIIBUS_WRITEREG(parent, phy, reg, data));
302 miibus_statchg(device_t dev)
305 struct mii_data *mii;
307 parent = device_get_parent(dev);
308 MIIBUS_STATCHG(parent);
310 mii = device_get_softc(dev);
311 if_setbaudrate(mii->mii_ifp, ifmedia_baudrate(mii->mii_media_active));
315 miibus_linkchg(device_t dev)
317 struct mii_data *mii;
321 parent = device_get_parent(dev);
322 MIIBUS_LINKCHG(parent);
324 mii = device_get_softc(dev);
326 if (mii->mii_media_status & IFM_AVALID) {
327 if (mii->mii_media_status & IFM_ACTIVE)
328 link_state = LINK_STATE_UP;
330 link_state = LINK_STATE_DOWN;
332 link_state = LINK_STATE_UNKNOWN;
333 if_link_state_change(mii->mii_ifp, link_state);
337 miibus_mediainit(device_t dev)
339 struct mii_data *mii;
340 struct ifmedia_entry *m;
343 /* Poke the parent in case it has any media of its own to add. */
344 MIIBUS_MEDIAINIT(device_get_parent(dev));
346 mii = device_get_softc(dev);
347 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
348 media = m->ifm_media;
349 if (media == (IFM_ETHER | IFM_AUTO))
353 ifmedia_set(&mii->mii_media, media);
357 * Helper function used by network interface drivers, attaches the miibus and
358 * the PHYs to the network interface driver parent.
361 mii_attach(device_t dev, device_t *miibus, if_t ifp,
362 ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
363 int phyloc, int offloc, int flags)
365 struct miibus_ivars *ivars;
366 struct mii_attach_args *args, ma;
367 device_t *children, phy;
368 int bmsr, first, i, nchildren, phymax, phymin, rv;
371 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
372 printf("%s: phyloc and offloc specified\n", __func__);
376 if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
377 printf("%s: invalid offloc %d\n", __func__, offloc);
381 if (phyloc == MII_PHY_ANY) {
383 phymax = MII_NPHY - 1;
385 if (phyloc < 0 || phyloc >= MII_NPHY) {
386 printf("%s: invalid phyloc %d\n", __func__, phyloc);
389 phymin = phymax = phyloc;
393 if (*miibus == NULL) {
395 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
399 ivars->ifmedia_upd = ifmedia_upd;
400 ivars->ifmedia_sts = ifmedia_sts;
401 ivars->mii_flags = flags;
402 *miibus = device_add_child(dev, "miibus", -1);
403 if (*miibus == NULL) {
407 device_set_ivars(*miibus, ivars);
409 ivars = device_get_ivars(*miibus);
410 if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd ||
411 ivars->ifmedia_sts != ifmedia_sts ||
412 ivars->mii_flags != flags) {
413 printf("%s: non-matching invariant\n", __func__);
417 * Assignment of the attach arguments mii_data for the first
418 * pass is done in miibus_attach(), i.e. once the miibus softc
419 * has been allocated.
421 ma.mii_data = device_get_softc(*miibus);
424 ma.mii_capmask = capmask;
426 if (resource_int_value(device_get_name(*miibus),
427 device_get_unit(*miibus), "phymask", &phymask) != 0)
428 phymask = 0xffffffff;
430 if (device_get_children(*miibus, &children, &nchildren) != 0) {
434 ivars->mii_offset = 0;
435 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
437 * Make sure we haven't already configured a PHY at this
438 * address. This allows mii_attach() to be called
441 for (i = 0; i < nchildren; i++) {
442 args = device_get_ivars(children[i]);
443 if (args->mii_phyno == ma.mii_phyno) {
445 * Yes, there is already something
446 * configured at this address.
453 * Check to see if there is a PHY at this address. Note,
454 * many braindead PHYs report 0/0 in their ID registers,
455 * so we test for media in the BMSR.
457 bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR);
458 if (bmsr == 0 || bmsr == 0xffff ||
459 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
460 /* Assume no PHY at this address. */
465 * There is a PHY at this address. If we were given an
466 * `offset' locator, skip this PHY if it doesn't match.
468 if (offloc != MII_OFFSET_ANY && offloc != ivars->mii_offset)
472 * Skip this PHY if it's not included in the phymask hint.
474 if ((phymask & (1 << ma.mii_phyno)) == 0)
478 * Extract the IDs. Braindead PHYs will be handled by
479 * the `ukphy' driver, as we have no ID information to
482 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
483 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
485 ma.mii_offset = ivars->mii_offset;
486 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
490 bcopy((char *)&ma, (char *)args, sizeof(ma));
491 phy = device_add_child(*miibus, NULL, -1);
493 free(args, M_DEVBUF);
496 device_set_ivars(phy, args);
500 free(children, M_TEMP);
503 rv = device_set_driver(*miibus, &miibus_driver);
506 bus_enumerate_hinted_children(*miibus);
507 rv = device_get_children(*miibus, &children, &nchildren);
510 free(children, M_TEMP);
511 if (nchildren == 0) {
515 rv = bus_generic_attach(dev);
519 /* Attaching of the PHY drivers is done in miibus_attach(). */
522 rv = bus_generic_attach(*miibus);
530 device_delete_child(dev, *miibus);
531 free(ivars, M_DEVBUF);
538 * Media changed; notify all PHYs.
541 mii_mediachg(struct mii_data *mii)
543 struct mii_softc *child;
544 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
547 mii->mii_media_status = 0;
548 mii->mii_media_active = IFM_NONE;
550 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
552 * If the media indicates a different PHY instance,
555 if (IFM_INST(ife->ifm_media) != child->mii_inst) {
556 if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
557 device_printf(child->mii_dev, "%s: "
558 "can't handle non-zero PHY instance %d\n",
559 __func__, child->mii_inst);
562 PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
566 rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
574 * Call the PHY tick routines, used during autonegotiation.
577 mii_tick(struct mii_data *mii)
579 struct mii_softc *child;
580 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
582 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
584 * If this PHY instance isn't currently selected, just skip
587 if (IFM_INST(ife->ifm_media) != child->mii_inst)
589 (void)PHY_SERVICE(child, mii, MII_TICK);
594 * Get media status from PHYs.
597 mii_pollstat(struct mii_data *mii)
599 struct mii_softc *child;
600 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
602 mii->mii_media_status = 0;
603 mii->mii_media_active = IFM_NONE;
605 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
607 * If we're not polling this PHY instance, just skip it.
609 if (IFM_INST(ife->ifm_media) != child->mii_inst)
611 (void)PHY_SERVICE(child, mii, MII_POLLSTAT);
616 mii_bitreverse(unsigned char x)
618 static unsigned const char nibbletab[16] = {
619 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
622 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
626 mii_oui(u_int id1, u_int id2)
630 h = (id1 << 6) | (id2 >> 10);
632 return ((mii_bitreverse(h >> 16) << 16) |
633 (mii_bitreverse((h >> 8) & 0xff) << 8) |
634 mii_bitreverse(h & 0xff));
638 mii_phy_mac_match(struct mii_softc *mii, const char *name)
641 return (strcmp(device_get_name(device_get_parent(mii->mii_dev)),
646 mii_dev_mac_match(device_t parent, const char *name)
649 return (strcmp(device_get_name(device_get_parent(
650 device_get_parent(parent))), name) == 0);
654 mii_phy_mac_softc(struct mii_softc *mii)
657 return (device_get_softc(device_get_parent(mii->mii_dev)));
661 mii_dev_mac_softc(device_t parent)
664 return (device_get_softc(device_get_parent(device_get_parent(parent))));