xref: /freebsd/sys/dev/mii/mii.c (revision d6b92ffa)
1 /*	$NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 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 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /*
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.
40  */
41 
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>
47 #include <sys/bus.h>
48 
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_media.h>
52 
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
55 
56 MODULE_VERSION(miibus, 1);
57 
58 #include "miibus_if.h"
59 
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;
73 
74 static unsigned char mii_bitreverse(unsigned char x);
75 
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),
82 
83 	/* bus interface */
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),
89 
90 	/* MII interface */
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),
96 
97 	DEVMETHOD_END
98 };
99 
100 devclass_t miibus_devclass;
101 
102 driver_t miibus_driver = {
103 	"miibus",
104 	miibus_methods,
105 	sizeof(struct mii_data)
106 };
107 
108 struct miibus_ivars {
109 	if_t		ifp;
110 	ifm_change_cb_t	ifmedia_upd;
111 	ifm_stat_cb_t	ifmedia_sts;
112 	u_int		mii_flags;
113 	u_int		mii_offset;
114 };
115 
116 static int
117 miibus_probe(device_t dev)
118 {
119 
120 	device_set_desc(dev, "MII bus");
121 
122 	return (BUS_PROBE_SPECIFIC);
123 }
124 
125 static int
126 miibus_attach(device_t dev)
127 {
128 	struct miibus_ivars	*ivars;
129 	struct mii_attach_args	*ma;
130 	struct mii_data		*mii;
131 	device_t		*children;
132 	int			i, nchildren;
133 
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]);
138 			ma->mii_data = mii;
139 		}
140 		free(children, M_TEMP);
141 	}
142 	if (nchildren == 0) {
143 		device_printf(dev, "cannot get children\n");
144 		return (ENXIO);
145 	}
146 	ivars = device_get_ivars(dev);
147 	ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
148 	    ivars->ifmedia_sts);
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);
153 
154 	return (bus_generic_attach(dev));
155 }
156 
157 static int
158 miibus_detach(device_t dev)
159 {
160 	struct mii_data		*mii;
161 
162 	bus_generic_detach(dev);
163 	mii = device_get_softc(dev);
164 	ifmedia_removeall(&mii->mii_media);
165 	mii->mii_ifp = NULL;
166 
167 	return (0);
168 }
169 
170 static int
171 miibus_print_child(device_t dev, device_t child)
172 {
173 	struct mii_attach_args *ma;
174 	int retval;
175 
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);
180 
181 	return (retval);
182 }
183 
184 static int
185 miibus_read_ivar(device_t dev, device_t child __unused, int which,
186     uintptr_t *result)
187 {
188 	struct miibus_ivars *ivars;
189 
190 	/*
191 	 * NB: this uses the instance variables of the miibus rather than
192 	 * its PHY children.
193 	 */
194 	ivars = device_get_ivars(dev);
195 	switch (which) {
196 	case MIIBUS_IVAR_FLAGS:
197 		*result = ivars->mii_flags;
198 		break;
199 	default:
200 		return (ENOENT);
201 	}
202 	return (0);
203 }
204 
205 static int
206 miibus_child_pnpinfo_str(device_t dev __unused, device_t child, char *buf,
207     size_t buflen)
208 {
209 	struct mii_attach_args *ma;
210 
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));
215 	return (0);
216 }
217 
218 static int
219 miibus_child_location_str(device_t dev __unused, device_t child, char *buf,
220     size_t buflen)
221 {
222 	struct mii_attach_args *ma;
223 
224 	ma = device_get_ivars(child);
225 	snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
226 	return (0);
227 }
228 
229 static void
230 miibus_hinted_child(device_t dev, const char *name, int unit)
231 {
232 	struct miibus_ivars *ivars;
233 	struct mii_attach_args *args, *ma;
234 	device_t *children, phy;
235 	int i, nchildren;
236 	u_int val;
237 
238 	if (resource_int_value(name, unit, "phyno", &val) != 0)
239 		return;
240 	if (device_get_children(dev, &children, &nchildren) != 0)
241 		return;
242 	ma = NULL;
243 	for (i = 0; i < nchildren; i++) {
244 		args = device_get_ivars(children[i]);
245 		if (args->mii_phyno == val) {
246 			ma = args;
247 			break;
248 		}
249 	}
250 	free(children, M_TEMP);
251 
252 	/*
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.
255 	 */
256 	if (ma == NULL) {
257 		ma = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
258 		    M_NOWAIT);
259 		if (ma == NULL)
260 			return;
261 		phy = device_add_child(dev, name, unit);
262 		if (phy == NULL) {
263 			free(ma, M_DEVBUF);
264 			return;
265 		}
266 		ivars = device_get_ivars(dev);
267 		ma->mii_phyno = val;
268 		ma->mii_offset = ivars->mii_offset++;
269 		ma->mii_id1 = 0;
270 		ma->mii_id2 = 0;
271 		ma->mii_capmask = BMSR_DEFCAPMASK;
272 		device_set_ivars(phy, ma);
273 	}
274 
275 	if (resource_int_value(name, unit, "id1", &val) == 0)
276 		ma->mii_id1 = val;
277 	if (resource_int_value(name, unit, "id2", &val) == 0)
278 		ma->mii_id2 = val;
279 	if (resource_int_value(name, unit, "capmask", &val) == 0)
280 		ma->mii_capmask = val;
281 }
282 
283 static int
284 miibus_readreg(device_t dev, int phy, int reg)
285 {
286 	device_t		parent;
287 
288 	parent = device_get_parent(dev);
289 	return (MIIBUS_READREG(parent, phy, reg));
290 }
291 
292 static int
293 miibus_writereg(device_t dev, int phy, int reg, int data)
294 {
295 	device_t		parent;
296 
297 	parent = device_get_parent(dev);
298 	return (MIIBUS_WRITEREG(parent, phy, reg, data));
299 }
300 
301 static void
302 miibus_statchg(device_t dev)
303 {
304 	device_t		parent;
305 	struct mii_data		*mii;
306 
307 	parent = device_get_parent(dev);
308 	MIIBUS_STATCHG(parent);
309 
310 	mii = device_get_softc(dev);
311 	if_setbaudrate(mii->mii_ifp, ifmedia_baudrate(mii->mii_media_active));
312 }
313 
314 static void
315 miibus_linkchg(device_t dev)
316 {
317 	struct mii_data		*mii;
318 	device_t		parent;
319 	int			link_state;
320 
321 	parent = device_get_parent(dev);
322 	MIIBUS_LINKCHG(parent);
323 
324 	mii = device_get_softc(dev);
325 
326 	if (mii->mii_media_status & IFM_AVALID) {
327 		if (mii->mii_media_status & IFM_ACTIVE)
328 			link_state = LINK_STATE_UP;
329 		else
330 			link_state = LINK_STATE_DOWN;
331 	} else
332 		link_state = LINK_STATE_UNKNOWN;
333 	if_link_state_change(mii->mii_ifp, link_state);
334 }
335 
336 static void
337 miibus_mediainit(device_t dev)
338 {
339 	struct mii_data		*mii;
340 	struct ifmedia_entry	*m;
341 	int			media = 0;
342 
343 	/* Poke the parent in case it has any media of its own to add. */
344 	MIIBUS_MEDIAINIT(device_get_parent(dev));
345 
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))
350 			break;
351 	}
352 
353 	ifmedia_set(&mii->mii_media, media);
354 }
355 
356 /*
357  * Helper function used by network interface drivers, attaches the miibus and
358  * the PHYs to the network interface driver parent.
359  */
360 int
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)
364 {
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;
369 	uint32_t phymask;
370 
371 	if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
372 		printf("%s: phyloc and offloc specified\n", __func__);
373 		return (EINVAL);
374 	}
375 
376 	if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
377 		printf("%s: invalid offloc %d\n", __func__, offloc);
378 		return (EINVAL);
379 	}
380 
381 	if (phyloc == MII_PHY_ANY) {
382 		phymin = 0;
383 		phymax = MII_NPHY - 1;
384 	} else {
385 		if (phyloc < 0 || phyloc >= MII_NPHY) {
386 			printf("%s: invalid phyloc %d\n", __func__, phyloc);
387 			return (EINVAL);
388 		}
389 		phymin = phymax = phyloc;
390 	}
391 
392 	first = 0;
393 	if (*miibus == NULL) {
394 		first = 1;
395 		ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
396 		if (ivars == NULL)
397 			return (ENOMEM);
398 		ivars->ifp = ifp;
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) {
404 			rv = ENXIO;
405 			goto fail;
406 		}
407 		device_set_ivars(*miibus, ivars);
408 	} else {
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__);
414 			return (EINVAL);
415 		}
416 		/*
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.
420 		 */
421 		ma.mii_data = device_get_softc(*miibus);
422 	}
423 
424 	ma.mii_capmask = capmask;
425 
426 	if (resource_int_value(device_get_name(*miibus),
427 	    device_get_unit(*miibus), "phymask", &phymask) != 0)
428 		phymask = 0xffffffff;
429 
430 	if (device_get_children(*miibus, &children, &nchildren) != 0) {
431 		children = NULL;
432 		nchildren = 0;
433 	}
434 	ivars->mii_offset = 0;
435 	for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
436 		/*
437 		 * Make sure we haven't already configured a PHY at this
438 		 * address.  This allows mii_attach() to be called
439 		 * multiple times.
440 		 */
441 		for (i = 0; i < nchildren; i++) {
442 			args = device_get_ivars(children[i]);
443 			if (args->mii_phyno == ma.mii_phyno) {
444 				/*
445 				 * Yes, there is already something
446 				 * configured at this address.
447 				 */
448 				goto skip;
449 			}
450 		}
451 
452 		/*
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.
456 		 */
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. */
461 			continue;
462 		}
463 
464 		/*
465 		 * There is a PHY at this address.  If we were given an
466 		 * `offset' locator, skip this PHY if it doesn't match.
467 		 */
468 		if (offloc != MII_OFFSET_ANY && offloc != ivars->mii_offset)
469 			goto skip;
470 
471 		/*
472 		 * Skip this PHY if it's not included in the phymask hint.
473 		 */
474 		if ((phymask & (1 << ma.mii_phyno)) == 0)
475 			goto skip;
476 
477 		/*
478 		 * Extract the IDs.  Braindead PHYs will be handled by
479 		 * the `ukphy' driver, as we have no ID information to
480 		 * match on.
481 		 */
482 		ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
483 		ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
484 
485 		ma.mii_offset = ivars->mii_offset;
486 		args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
487 		    M_NOWAIT);
488 		if (args == NULL)
489 			goto skip;
490 		bcopy((char *)&ma, (char *)args, sizeof(ma));
491 		phy = device_add_child(*miibus, NULL, -1);
492 		if (phy == NULL) {
493 			free(args, M_DEVBUF);
494 			goto skip;
495 		}
496 		device_set_ivars(phy, args);
497  skip:
498 		ivars->mii_offset++;
499 	}
500 	free(children, M_TEMP);
501 
502 	if (first != 0) {
503 		rv = device_set_driver(*miibus, &miibus_driver);
504 		if (rv != 0)
505 			goto fail;
506 		bus_enumerate_hinted_children(*miibus);
507 		rv = device_get_children(*miibus, &children, &nchildren);
508 		if (rv != 0)
509 			goto fail;
510 		free(children, M_TEMP);
511 		if (nchildren == 0) {
512 			rv = ENXIO;
513 			goto fail;
514 		}
515 		rv = bus_generic_attach(dev);
516 		if (rv != 0)
517 			goto fail;
518 
519 		/* Attaching of the PHY drivers is done in miibus_attach(). */
520 		return (0);
521 	}
522 	rv = bus_generic_attach(*miibus);
523 	if (rv != 0)
524 		goto fail;
525 
526 	return (0);
527 
528  fail:
529 	if (*miibus != NULL)
530 		device_delete_child(dev, *miibus);
531 	free(ivars, M_DEVBUF);
532 	if (first != 0)
533 		*miibus = NULL;
534 	return (rv);
535 }
536 
537 /*
538  * Media changed; notify all PHYs.
539  */
540 int
541 mii_mediachg(struct mii_data *mii)
542 {
543 	struct mii_softc *child;
544 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
545 	int rv;
546 
547 	mii->mii_media_status = 0;
548 	mii->mii_media_active = IFM_NONE;
549 
550 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
551 		/*
552 		 * If the media indicates a different PHY instance,
553 		 * isolate this one.
554 		 */
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);
560 				continue;
561 			}
562 			PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
563 			    BMCR_ISO);
564 			continue;
565 		}
566 		rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
567 		if (rv)
568 			return (rv);
569 	}
570 	return (0);
571 }
572 
573 /*
574  * Call the PHY tick routines, used during autonegotiation.
575  */
576 void
577 mii_tick(struct mii_data *mii)
578 {
579 	struct mii_softc *child;
580 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
581 
582 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
583 		/*
584 		 * If this PHY instance isn't currently selected, just skip
585 		 * it.
586 		 */
587 		if (IFM_INST(ife->ifm_media) != child->mii_inst)
588 			continue;
589 		(void)PHY_SERVICE(child, mii, MII_TICK);
590 	}
591 }
592 
593 /*
594  * Get media status from PHYs.
595  */
596 void
597 mii_pollstat(struct mii_data *mii)
598 {
599 	struct mii_softc *child;
600 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
601 
602 	mii->mii_media_status = 0;
603 	mii->mii_media_active = IFM_NONE;
604 
605 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
606 		/*
607 		 * If we're not polling this PHY instance, just skip it.
608 		 */
609 		if (IFM_INST(ife->ifm_media) != child->mii_inst)
610 			continue;
611 		(void)PHY_SERVICE(child, mii, MII_POLLSTAT);
612 	}
613 }
614 
615 static unsigned char
616 mii_bitreverse(unsigned char x)
617 {
618 	static unsigned const char nibbletab[16] = {
619 		0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
620 	};
621 
622 	return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
623 }
624 
625 u_int
626 mii_oui(u_int id1, u_int id2)
627 {
628 	u_int h;
629 
630 	h = (id1 << 6) | (id2 >> 10);
631 
632 	return ((mii_bitreverse(h >> 16) << 16) |
633 	    (mii_bitreverse((h >> 8) & 0xff) << 8) |
634 	    mii_bitreverse(h & 0xff));
635 }
636 
637 int
638 mii_phy_mac_match(struct mii_softc *mii, const char *name)
639 {
640 
641 	return (strcmp(device_get_name(device_get_parent(mii->mii_dev)),
642 	    name) == 0);
643 }
644 
645 int
646 mii_dev_mac_match(device_t parent, const char *name)
647 {
648 
649 	return (strcmp(device_get_name(device_get_parent(
650 	    device_get_parent(parent))), name) == 0);
651 }
652 
653 void *
654 mii_phy_mac_softc(struct mii_softc *mii)
655 {
656 
657 	return (device_get_softc(device_get_parent(mii->mii_dev)));
658 }
659 
660 void *
661 mii_dev_mac_softc(device_t parent)
662 {
663 
664 	return (device_get_softc(device_get_parent(device_get_parent(parent))));
665 }
666