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>
51 #include <net/route.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 int miibus_print_child(device_t dev, device_t child);
61 static int miibus_read_ivar(device_t dev, device_t child, int which,
63 static int miibus_child_location_str(device_t bus, device_t child, char *buf,
65 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
67 static int miibus_readreg(device_t, int, int);
68 static int miibus_writereg(device_t, int, int, int);
69 static void miibus_statchg(device_t);
70 static void miibus_linkchg(device_t);
71 static void miibus_mediainit(device_t);
72 static unsigned char mii_bitreverse(unsigned char x);
74 static device_method_t miibus_methods[] = {
75 /* device interface */
76 DEVMETHOD(device_probe, miibus_probe),
77 DEVMETHOD(device_attach, miibus_attach),
78 DEVMETHOD(device_detach, miibus_detach),
79 DEVMETHOD(device_shutdown, bus_generic_shutdown),
82 DEVMETHOD(bus_print_child, miibus_print_child),
83 DEVMETHOD(bus_read_ivar, miibus_read_ivar),
84 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
85 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
86 DEVMETHOD(bus_child_location_str, miibus_child_location_str),
89 DEVMETHOD(miibus_readreg, miibus_readreg),
90 DEVMETHOD(miibus_writereg, miibus_writereg),
91 DEVMETHOD(miibus_statchg, miibus_statchg),
92 DEVMETHOD(miibus_linkchg, miibus_linkchg),
93 DEVMETHOD(miibus_mediainit, miibus_mediainit),
98 devclass_t miibus_devclass;
100 driver_t miibus_driver = {
103 sizeof(struct mii_data)
106 struct miibus_ivars {
108 ifm_change_cb_t ifmedia_upd;
109 ifm_stat_cb_t ifmedia_sts;
114 miibus_probe(device_t dev)
117 device_set_desc(dev, "MII bus");
119 return (BUS_PROBE_SPECIFIC);
123 miibus_attach(device_t dev)
125 struct miibus_ivars *ivars;
126 struct mii_attach_args *ma;
127 struct mii_data *mii;
131 mii = device_get_softc(dev);
133 if (device_get_children(dev, &children, &nchildren) == 0) {
134 for (i = 0; i < nchildren; i++) {
135 ma = device_get_ivars(children[i]);
138 free(children, M_TEMP);
140 if (nchildren == 0) {
141 device_printf(dev, "cannot get children\n");
144 ivars = device_get_ivars(dev);
145 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
147 mii->mii_ifp = ivars->ifp;
148 mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE;
149 mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE;
150 LIST_INIT(&mii->mii_phys);
152 return (bus_generic_attach(dev));
156 miibus_detach(device_t dev)
158 struct mii_data *mii;
160 bus_generic_detach(dev);
161 mii = device_get_softc(dev);
162 ifmedia_removeall(&mii->mii_media);
169 miibus_print_child(device_t dev, device_t child)
171 struct mii_attach_args *ma;
174 ma = device_get_ivars(child);
175 retval = bus_print_child_header(dev, child);
176 retval += printf(" PHY %d", ma->mii_phyno);
177 retval += bus_print_child_footer(dev, child);
183 miibus_read_ivar(device_t dev, device_t child __unused, int which,
186 struct miibus_ivars *ivars;
189 * NB: this uses the instance variables of the miibus rather than
192 ivars = device_get_ivars(dev);
194 case MIIBUS_IVAR_FLAGS:
195 *result = ivars->mii_flags;
204 miibus_child_pnpinfo_str(device_t bus __unused, device_t child, char *buf,
207 struct mii_attach_args *ma;
209 ma = device_get_ivars(child);
210 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
211 MII_OUI(ma->mii_id1, ma->mii_id2),
212 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
217 miibus_child_location_str(device_t bus __unused, device_t child, char *buf,
220 struct mii_attach_args *ma;
222 ma = device_get_ivars(child);
223 snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
228 miibus_readreg(device_t dev, int phy, int reg)
232 parent = device_get_parent(dev);
233 return (MIIBUS_READREG(parent, phy, reg));
237 miibus_writereg(device_t dev, int phy, int reg, int data)
241 parent = device_get_parent(dev);
242 return (MIIBUS_WRITEREG(parent, phy, reg, data));
246 miibus_statchg(device_t dev)
249 struct mii_data *mii;
251 parent = device_get_parent(dev);
252 MIIBUS_STATCHG(parent);
254 mii = device_get_softc(dev);
255 mii->mii_ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
259 miibus_linkchg(device_t dev)
261 struct mii_data *mii;
265 parent = device_get_parent(dev);
266 MIIBUS_LINKCHG(parent);
268 mii = device_get_softc(dev);
270 if (mii->mii_media_status & IFM_AVALID) {
271 if (mii->mii_media_status & IFM_ACTIVE)
272 link_state = LINK_STATE_UP;
274 link_state = LINK_STATE_DOWN;
276 link_state = LINK_STATE_UNKNOWN;
277 if_link_state_change(mii->mii_ifp, link_state);
281 miibus_mediainit(device_t dev)
283 struct mii_data *mii;
284 struct ifmedia_entry *m;
287 /* Poke the parent in case it has any media of its own to add. */
288 MIIBUS_MEDIAINIT(device_get_parent(dev));
290 mii = device_get_softc(dev);
291 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
292 media = m->ifm_media;
293 if (media == (IFM_ETHER | IFM_AUTO))
297 ifmedia_set(&mii->mii_media, media);
301 * Helper function used by network interface drivers, attaches the miibus and
302 * the PHYs to the network interface driver parent.
305 mii_attach(device_t dev, device_t *miibus, struct ifnet *ifp,
306 ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
307 int phyloc, int offloc, int flags)
309 struct miibus_ivars *ivars;
310 struct mii_attach_args ma, *args;
311 device_t *children, phy;
312 int bmsr, first, i, nchildren, offset, phymax, phymin, rv;
314 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
315 printf("%s: phyloc and offloc specified\n", __func__);
319 if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
320 printf("%s: ivalid offloc %d\n", __func__, offloc);
324 if (phyloc == MII_PHY_ANY) {
326 phymax = MII_NPHY - 1;
328 if (phyloc < 0 || phyloc >= MII_NPHY) {
329 printf("%s: ivalid phyloc %d\n", __func__, phyloc);
332 phymin = phymax = phyloc;
336 if (*miibus == NULL) {
338 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
342 ivars->ifmedia_upd = ifmedia_upd;
343 ivars->ifmedia_sts = ifmedia_sts;
344 ivars->mii_flags = flags;
345 *miibus = device_add_child(dev, "miibus", -1);
346 if (*miibus == NULL) {
350 device_set_ivars(*miibus, ivars);
352 ivars = device_get_ivars(*miibus);
353 if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd ||
354 ivars->ifmedia_sts != ifmedia_sts ||
355 ivars->mii_flags != flags) {
356 printf("%s: non-matching invariant\n", __func__);
360 * Assignment of the attach arguments mii_data for the first
361 * pass is done in miibus_attach(), i.e. once the miibus softc
362 * has been allocated.
364 ma.mii_data = device_get_softc(*miibus);
367 ma.mii_capmask = capmask;
371 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
373 * Make sure we haven't already configured a PHY at this
374 * address. This allows mii_attach() to be called
377 if (device_get_children(*miibus, &children, &nchildren) == 0) {
378 for (i = 0; i < nchildren; i++) {
379 args = device_get_ivars(children[i]);
380 if (args->mii_phyno == ma.mii_phyno) {
382 * Yes, there is already something
383 * configured at this address.
385 free(children, M_TEMP);
389 free(children, M_TEMP);
393 * Check to see if there is a PHY at this address. Note,
394 * many braindead PHYs report 0/0 in their ID registers,
395 * so we test for media in the BMSR.
397 bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR);
398 if (bmsr == 0 || bmsr == 0xffff ||
399 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
400 /* Assume no PHY at this address. */
405 * There is a PHY at this address. If we were given an
406 * `offset' locator, skip this PHY if it doesn't match.
408 if (offloc != MII_OFFSET_ANY && offloc != offset)
412 * Extract the IDs. Braindead PHYs will be handled by
413 * the `ukphy' driver, as we have no ID information to
416 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
417 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
419 ma.mii_offset = offset;
420 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
424 bcopy((char *)&ma, (char *)args, sizeof(ma));
425 phy = device_add_child(*miibus, NULL, -1);
427 free(args, M_DEVBUF);
430 device_set_ivars(phy, args);
440 rv = bus_generic_attach(dev);
444 /* Attaching of the PHY drivers is done in miibus_attach(). */
447 rv = bus_generic_attach(*miibus);
455 device_delete_child(dev, *miibus);
456 free(ivars, M_DEVBUF);
463 * Media changed; notify all PHYs.
466 mii_mediachg(struct mii_data *mii)
468 struct mii_softc *child;
469 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
472 mii->mii_media_status = 0;
473 mii->mii_media_active = IFM_NONE;
475 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
477 * If the media indicates a different PHY instance,
480 if (IFM_INST(ife->ifm_media) != child->mii_inst) {
481 if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
482 device_printf(child->mii_dev, "%s: "
483 "can't handle non-zero PHY instance %d\n",
484 __func__, child->mii_inst);
487 PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
491 rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
499 * Call the PHY tick routines, used during autonegotiation.
502 mii_tick(struct mii_data *mii)
504 struct mii_softc *child;
505 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
507 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
509 * If this PHY instance isn't currently selected, just skip
512 if (IFM_INST(ife->ifm_media) != child->mii_inst)
514 (void)PHY_SERVICE(child, mii, MII_TICK);
519 * Get media status from PHYs.
522 mii_pollstat(struct mii_data *mii)
524 struct mii_softc *child;
525 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
527 mii->mii_media_status = 0;
528 mii->mii_media_active = IFM_NONE;
530 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
532 * If we're not polling this PHY instance, just skip it.
534 if (IFM_INST(ife->ifm_media) != child->mii_inst)
536 (void)PHY_SERVICE(child, mii, MII_POLLSTAT);
541 * Inform the PHYs that the interface is down.
544 mii_down(struct mii_data *mii)
546 struct mii_softc *child;
548 LIST_FOREACH(child, &mii->mii_phys, mii_list)
553 mii_bitreverse(unsigned char x)
555 unsigned const char const nibbletab[16] = {
556 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
559 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
563 mii_oui(u_int id1, u_int id2)
567 h = (id1 << 6) | (id2 >> 10);
569 return ((mii_bitreverse(h >> 16) << 16) |
570 (mii_bitreverse((h >> 8) & 0xff) << 8) |
571 mii_bitreverse(h & 0xff));