xref: /freebsd/sys/dev/mii/mii.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*	$NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
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.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
21  *
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.
33  */
34 
35 #include <sys/cdefs.h>
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 #include <sys/sbuf.h>
49 
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_media.h>
53 
54 #include <dev/mii/mii.h>
55 #include <dev/mii/miivar.h>
56 
57 MODULE_VERSION(miibus, 1);
58 
59 #include "miibus_if.h"
60 
61 static bus_child_detached_t miibus_child_detached;
62 static bus_child_location_t miibus_child_location;
63 static bus_child_pnpinfo_t miibus_child_pnpinfo;
64 static device_detach_t miibus_detach;
65 static bus_hinted_child_t miibus_hinted_child;
66 static bus_print_child_t miibus_print_child;
67 static device_probe_t miibus_probe;
68 static bus_read_ivar_t miibus_read_ivar;
69 static miibus_readreg_t miibus_readreg;
70 static miibus_statchg_t miibus_statchg;
71 static miibus_writereg_t miibus_writereg;
72 static miibus_linkchg_t miibus_linkchg;
73 static miibus_mediainit_t miibus_mediainit;
74 
75 static unsigned char mii_bitreverse(unsigned char x);
76 
77 static device_method_t miibus_methods[] = {
78 	/* device interface */
79 	DEVMETHOD(device_probe,		miibus_probe),
80 	DEVMETHOD(device_attach,	miibus_attach),
81 	DEVMETHOD(device_detach,	miibus_detach),
82 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
83 
84 	/* bus interface */
85 	DEVMETHOD(bus_print_child,	miibus_print_child),
86 	DEVMETHOD(bus_read_ivar,	miibus_read_ivar),
87 	DEVMETHOD(bus_child_detached,	miibus_child_detached),
88 	DEVMETHOD(bus_child_pnpinfo,	miibus_child_pnpinfo),
89 	DEVMETHOD(bus_child_location,	miibus_child_location),
90 	DEVMETHOD(bus_hinted_child,	miibus_hinted_child),
91 
92 	/* MII interface */
93 	DEVMETHOD(miibus_readreg,	miibus_readreg),
94 	DEVMETHOD(miibus_writereg,	miibus_writereg),
95 	DEVMETHOD(miibus_statchg,	miibus_statchg),
96 	DEVMETHOD(miibus_linkchg,	miibus_linkchg),
97 	DEVMETHOD(miibus_mediainit,	miibus_mediainit),
98 
99 	DEVMETHOD_END
100 };
101 
102 DEFINE_CLASS_0(miibus, miibus_driver, miibus_methods, sizeof(struct mii_data));
103 
104 struct miibus_ivars {
105 	if_t		ifp;
106 	ifm_change_cb_t	ifmedia_upd;
107 	ifm_stat_cb_t	ifmedia_sts;
108 	u_int		mii_flags;
109 	u_int		mii_offset;
110 };
111 
112 static int
113 miibus_probe(device_t dev)
114 {
115 
116 	device_set_desc(dev, "MII bus");
117 
118 	return (BUS_PROBE_SPECIFIC);
119 }
120 
121 int
122 miibus_attach(device_t dev)
123 {
124 	struct miibus_ivars	*ivars;
125 	struct mii_attach_args	*ma;
126 	struct mii_data		*mii;
127 	device_t		*children;
128 	int			i, nchildren;
129 
130 	mii = device_get_softc(dev);
131 	if (device_get_children(dev, &children, &nchildren) == 0) {
132 		for (i = 0; i < nchildren; i++) {
133 			ma = device_get_ivars(children[i]);
134 			ma->mii_data = mii;
135 		}
136 		free(children, M_TEMP);
137 	}
138 	if (nchildren == 0) {
139 		device_printf(dev, "cannot get children\n");
140 		return (ENXIO);
141 	}
142 	ivars = device_get_ivars(dev);
143 	ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
144 	    ivars->ifmedia_sts);
145 	mii->mii_ifp = ivars->ifp;
146 	if_setcapabilitiesbit(mii->mii_ifp, IFCAP_LINKSTATE, 0);
147 	if_setcapenablebit(mii->mii_ifp, IFCAP_LINKSTATE, 0);
148 	LIST_INIT(&mii->mii_phys);
149 
150 	return (bus_generic_attach(dev));
151 }
152 
153 static int
154 miibus_detach(device_t dev)
155 {
156 	struct mii_data		*mii;
157 	struct miibus_ivars	*ivars;
158 
159 	ivars = device_get_ivars(dev);
160 	bus_generic_detach(dev);
161 	mii = device_get_softc(dev);
162 	ifmedia_removeall(&mii->mii_media);
163 	free(ivars, M_DEVBUF);
164 	mii->mii_ifp = NULL;
165 
166 	return (0);
167 }
168 
169 static void
170 miibus_child_detached(device_t dev, device_t child)
171 {
172 	struct mii_attach_args *args;
173 
174 	args = device_get_ivars(child);
175 	free(args, M_DEVBUF);
176 }
177 
178 static int
179 miibus_print_child(device_t dev, device_t child)
180 {
181 	struct mii_attach_args *ma;
182 	int retval;
183 
184 	ma = device_get_ivars(child);
185 	retval = bus_print_child_header(dev, child);
186 	retval += printf(" PHY %d", ma->mii_phyno);
187 	retval += bus_print_child_footer(dev, child);
188 
189 	return (retval);
190 }
191 
192 static int
193 miibus_read_ivar(device_t dev, device_t child __unused, int which,
194     uintptr_t *result)
195 {
196 	struct miibus_ivars *ivars;
197 
198 	/*
199 	 * NB: this uses the instance variables of the miibus rather than
200 	 * its PHY children.
201 	 */
202 	ivars = device_get_ivars(dev);
203 	switch (which) {
204 	case MIIBUS_IVAR_FLAGS:
205 		*result = ivars->mii_flags;
206 		break;
207 	default:
208 		return (ENOENT);
209 	}
210 	return (0);
211 }
212 
213 static int
214 miibus_child_pnpinfo(device_t dev __unused, device_t child, struct sbuf *sb)
215 {
216 	struct mii_attach_args *ma;
217 
218 	ma = device_get_ivars(child);
219 	sbuf_printf(sb, "oui=0x%x model=0x%x rev=0x%x",
220 	    MII_OUI(ma->mii_id1, ma->mii_id2),
221 	    MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
222 	return (0);
223 }
224 
225 static int
226 miibus_child_location(device_t dev __unused, device_t child, struct sbuf *sb)
227 {
228 	struct mii_attach_args *ma;
229 
230 	ma = device_get_ivars(child);
231 	sbuf_printf(sb, "phyno=%d", ma->mii_phyno);
232 	return (0);
233 }
234 
235 static void
236 miibus_hinted_child(device_t dev, const char *name, int unit)
237 {
238 	struct miibus_ivars *ivars;
239 	struct mii_attach_args *args, *ma;
240 	device_t *children, phy;
241 	int i, nchildren;
242 	u_int val;
243 
244 	if (resource_int_value(name, unit, "phyno", &val) != 0)
245 		return;
246 	if (device_get_children(dev, &children, &nchildren) != 0)
247 		return;
248 	ma = NULL;
249 	for (i = 0; i < nchildren; i++) {
250 		args = device_get_ivars(children[i]);
251 		if (args->mii_phyno == val) {
252 			ma = args;
253 			break;
254 		}
255 	}
256 	free(children, M_TEMP);
257 
258 	/*
259 	 * Don't add a PHY that was automatically identified by having media
260 	 * in its BMSR twice, only allow to alter its attach arguments.
261 	 */
262 	if (ma == NULL) {
263 		ma = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
264 		    M_NOWAIT);
265 		if (ma == NULL)
266 			return;
267 		phy = device_add_child(dev, name, unit);
268 		if (phy == NULL) {
269 			free(ma, M_DEVBUF);
270 			return;
271 		}
272 		ivars = device_get_ivars(dev);
273 		ma->mii_phyno = val;
274 		ma->mii_offset = ivars->mii_offset++;
275 		ma->mii_id1 = 0;
276 		ma->mii_id2 = 0;
277 		ma->mii_capmask = BMSR_DEFCAPMASK;
278 		device_set_ivars(phy, ma);
279 	}
280 
281 	if (resource_int_value(name, unit, "id1", &val) == 0)
282 		ma->mii_id1 = val;
283 	if (resource_int_value(name, unit, "id2", &val) == 0)
284 		ma->mii_id2 = val;
285 	if (resource_int_value(name, unit, "capmask", &val) == 0)
286 		ma->mii_capmask = val;
287 }
288 
289 static int
290 miibus_readreg(device_t dev, int phy, int reg)
291 {
292 	device_t		parent;
293 
294 	parent = device_get_parent(dev);
295 	return (MIIBUS_READREG(parent, phy, reg));
296 }
297 
298 static int
299 miibus_writereg(device_t dev, int phy, int reg, int data)
300 {
301 	device_t		parent;
302 
303 	parent = device_get_parent(dev);
304 	return (MIIBUS_WRITEREG(parent, phy, reg, data));
305 }
306 
307 static void
308 miibus_statchg(device_t dev)
309 {
310 	device_t		parent;
311 	struct mii_data		*mii;
312 
313 	parent = device_get_parent(dev);
314 	MIIBUS_STATCHG(parent);
315 
316 	mii = device_get_softc(dev);
317 	if_setbaudrate(mii->mii_ifp, ifmedia_baudrate(mii->mii_media_active));
318 }
319 
320 static void
321 miibus_linkchg(device_t dev)
322 {
323 	struct mii_data		*mii;
324 	device_t		parent;
325 	int			link_state;
326 
327 	parent = device_get_parent(dev);
328 	MIIBUS_LINKCHG(parent);
329 
330 	mii = device_get_softc(dev);
331 
332 	if (mii->mii_media_status & IFM_AVALID) {
333 		if (mii->mii_media_status & IFM_ACTIVE)
334 			link_state = LINK_STATE_UP;
335 		else
336 			link_state = LINK_STATE_DOWN;
337 	} else
338 		link_state = LINK_STATE_UNKNOWN;
339 	if_link_state_change(mii->mii_ifp, link_state);
340 }
341 
342 static void
343 miibus_mediainit(device_t dev)
344 {
345 	struct mii_data		*mii;
346 	struct ifmedia_entry	*m;
347 	int			media = 0;
348 
349 	/* Poke the parent in case it has any media of its own to add. */
350 	MIIBUS_MEDIAINIT(device_get_parent(dev));
351 
352 	mii = device_get_softc(dev);
353 	LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
354 		media = m->ifm_media;
355 		if (media == (IFM_ETHER | IFM_AUTO))
356 			break;
357 	}
358 
359 	ifmedia_set(&mii->mii_media, media);
360 }
361 
362 /*
363  * Helper function used by network interface drivers, attaches the miibus and
364  * the PHYs to the network interface driver parent.
365  */
366 int
367 mii_attach(device_t dev, device_t *miibus, if_t ifp,
368     ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
369     int phyloc, int offloc, int flags)
370 {
371 	struct miibus_ivars *ivars;
372 	struct mii_attach_args *args, ma;
373 	device_t *children, phy;
374 	int bmsr, first, i, nchildren, phymax, phymin, rv;
375 	uint32_t phymask;
376 
377 	bus_topo_assert();
378 
379 	if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
380 		printf("%s: phyloc and offloc specified\n", __func__);
381 		return (EINVAL);
382 	}
383 
384 	if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
385 		printf("%s: invalid offloc %d\n", __func__, offloc);
386 		return (EINVAL);
387 	}
388 
389 	if (phyloc == MII_PHY_ANY) {
390 		phymin = 0;
391 		phymax = MII_NPHY - 1;
392 	} else {
393 		if (phyloc < 0 || phyloc >= MII_NPHY) {
394 			printf("%s: invalid phyloc %d\n", __func__, phyloc);
395 			return (EINVAL);
396 		}
397 		phymin = phymax = phyloc;
398 	}
399 
400 	first = 0;
401 	if (*miibus == NULL) {
402 		first = 1;
403 		ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
404 		if (ivars == NULL)
405 			return (ENOMEM);
406 		ivars->ifp = ifp;
407 		ivars->ifmedia_upd = ifmedia_upd;
408 		ivars->ifmedia_sts = ifmedia_sts;
409 		ivars->mii_flags = flags;
410 		*miibus = device_add_child(dev, "miibus", -1);
411 		if (*miibus == NULL) {
412 			rv = ENXIO;
413 			goto fail;
414 		}
415 		device_set_ivars(*miibus, ivars);
416 	} else {
417 		ivars = device_get_ivars(*miibus);
418 		if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd ||
419 		    ivars->ifmedia_sts != ifmedia_sts ||
420 		    ivars->mii_flags != flags) {
421 			printf("%s: non-matching invariant\n", __func__);
422 			return (EINVAL);
423 		}
424 		/*
425 		 * Assignment of the attach arguments mii_data for the first
426 		 * pass is done in miibus_attach(), i.e. once the miibus softc
427 		 * has been allocated.
428 		 */
429 		ma.mii_data = device_get_softc(*miibus);
430 	}
431 
432 	ma.mii_capmask = capmask;
433 
434 	if (resource_int_value(device_get_name(*miibus),
435 	    device_get_unit(*miibus), "phymask", &phymask) != 0)
436 		phymask = 0xffffffff;
437 
438 	if (device_get_children(*miibus, &children, &nchildren) != 0) {
439 		children = NULL;
440 		nchildren = 0;
441 	}
442 	ivars->mii_offset = 0;
443 	for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
444 		/*
445 		 * Make sure we haven't already configured a PHY at this
446 		 * address.  This allows mii_attach() to be called
447 		 * multiple times.
448 		 */
449 		for (i = 0; i < nchildren; i++) {
450 			args = device_get_ivars(children[i]);
451 			if (args->mii_phyno == ma.mii_phyno) {
452 				/*
453 				 * Yes, there is already something
454 				 * configured at this address.
455 				 */
456 				goto skip;
457 			}
458 		}
459 
460 		/*
461 		 * Check to see if there is a PHY at this address.  Note,
462 		 * many braindead PHYs report 0/0 in their ID registers,
463 		 * so we test for media in the BMSR.
464 		 */
465 		bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR);
466 		if (bmsr == 0 || bmsr == 0xffff ||
467 		    (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
468 			/* Assume no PHY at this address. */
469 			continue;
470 		}
471 
472 		/*
473 		 * There is a PHY at this address.  If we were given an
474 		 * `offset' locator, skip this PHY if it doesn't match.
475 		 */
476 		if (offloc != MII_OFFSET_ANY && offloc != ivars->mii_offset)
477 			goto skip;
478 
479 		/*
480 		 * Skip this PHY if it's not included in the phymask hint.
481 		 */
482 		if ((phymask & (1 << ma.mii_phyno)) == 0)
483 			goto skip;
484 
485 		/*
486 		 * Extract the IDs.  Braindead PHYs will be handled by
487 		 * the `ukphy' driver, as we have no ID information to
488 		 * match on.
489 		 */
490 		ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
491 		ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
492 
493 		ma.mii_offset = ivars->mii_offset;
494 		args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
495 		    M_NOWAIT);
496 		if (args == NULL)
497 			goto skip;
498 		bcopy((char *)&ma, (char *)args, sizeof(ma));
499 		phy = device_add_child(*miibus, NULL, -1);
500 		if (phy == NULL) {
501 			free(args, M_DEVBUF);
502 			goto skip;
503 		}
504 		device_set_ivars(phy, args);
505  skip:
506 		ivars->mii_offset++;
507 	}
508 	free(children, M_TEMP);
509 
510 	if (first != 0) {
511 		rv = device_set_driver(*miibus, &miibus_driver);
512 		if (rv != 0)
513 			goto fail;
514 		bus_enumerate_hinted_children(*miibus);
515 		rv = device_get_children(*miibus, &children, &nchildren);
516 		if (rv != 0)
517 			goto fail;
518 		free(children, M_TEMP);
519 		if (nchildren == 0) {
520 			rv = ENXIO;
521 			goto fail;
522 		}
523 		rv = bus_generic_attach(dev);
524 		if (rv != 0)
525 			goto fail;
526 
527 		/* Attaching of the PHY drivers is done in miibus_attach(). */
528 		return (0);
529 	}
530 	rv = bus_generic_attach(*miibus);
531 	if (rv != 0)
532 		goto fail;
533 
534 	return (0);
535 
536  fail:
537 	if (*miibus != NULL)
538 		device_delete_child(dev, *miibus);
539 	free(ivars, M_DEVBUF);
540 	if (first != 0)
541 		*miibus = NULL;
542 	return (rv);
543 }
544 
545 /*
546  * Media changed; notify all PHYs.
547  */
548 int
549 mii_mediachg(struct mii_data *mii)
550 {
551 	struct mii_softc *child;
552 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
553 	int rv;
554 
555 	mii->mii_media_status = 0;
556 	mii->mii_media_active = IFM_NONE;
557 
558 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
559 		/*
560 		 * If the media indicates a different PHY instance,
561 		 * isolate this one.
562 		 */
563 		if (IFM_INST(ife->ifm_media) != child->mii_inst) {
564 			if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
565 				device_printf(child->mii_dev, "%s: "
566 				    "can't handle non-zero PHY instance %d\n",
567 				    __func__, child->mii_inst);
568 				continue;
569 			}
570 			PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
571 			    BMCR_ISO);
572 			continue;
573 		}
574 		rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
575 		if (rv)
576 			return (rv);
577 	}
578 	return (0);
579 }
580 
581 /*
582  * Call the PHY tick routines, used during autonegotiation.
583  */
584 void
585 mii_tick(struct mii_data *mii)
586 {
587 	struct mii_softc *child;
588 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
589 
590 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
591 		/*
592 		 * If this PHY instance isn't currently selected, just skip
593 		 * it.
594 		 */
595 		if (IFM_INST(ife->ifm_media) != child->mii_inst)
596 			continue;
597 		(void)PHY_SERVICE(child, mii, MII_TICK);
598 	}
599 }
600 
601 /*
602  * Get media status from PHYs.
603  */
604 void
605 mii_pollstat(struct mii_data *mii)
606 {
607 	struct mii_softc *child;
608 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
609 
610 	mii->mii_media_status = 0;
611 	mii->mii_media_active = IFM_NONE;
612 
613 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
614 		/*
615 		 * If we're not polling this PHY instance, just skip it.
616 		 */
617 		if (IFM_INST(ife->ifm_media) != child->mii_inst)
618 			continue;
619 		(void)PHY_SERVICE(child, mii, MII_POLLSTAT);
620 	}
621 }
622 
623 static unsigned char
624 mii_bitreverse(unsigned char x)
625 {
626 	static unsigned const char nibbletab[16] = {
627 		0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
628 	};
629 
630 	return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
631 }
632 
633 u_int
634 mii_oui(u_int id1, u_int id2)
635 {
636 	u_int h;
637 
638 	h = (id1 << 6) | (id2 >> 10);
639 
640 	return ((mii_bitreverse(h >> 16) << 16) |
641 	    (mii_bitreverse((h >> 8) & 0xff) << 8) |
642 	    mii_bitreverse(h & 0xff));
643 }
644 
645 int
646 mii_phy_mac_match(struct mii_softc *mii, const char *name)
647 {
648 
649 	return (strcmp(device_get_name(device_get_parent(mii->mii_dev)),
650 	    name) == 0);
651 }
652 
653 int
654 mii_dev_mac_match(device_t parent, const char *name)
655 {
656 
657 	return (strcmp(device_get_name(device_get_parent(
658 	    device_get_parent(parent))), name) == 0);
659 }
660 
661 void *
662 mii_phy_mac_softc(struct mii_softc *mii)
663 {
664 
665 	return (device_get_softc(device_get_parent(mii->mii_dev)));
666 }
667 
668 void *
669 mii_dev_mac_softc(device_t parent)
670 {
671 
672 	return (device_get_softc(device_get_parent(device_get_parent(parent))));
673 }
674