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
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 bus_child_deleted_t miibus_child_deleted;
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_deleted, miibus_child_deleted),
87 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
88 DEVMETHOD(bus_child_location_str, miibus_child_location_str),
89 DEVMETHOD(bus_hinted_child, miibus_hinted_child),
90
91 /* MII interface */
92 DEVMETHOD(miibus_readreg, miibus_readreg),
93 DEVMETHOD(miibus_writereg, miibus_writereg),
94 DEVMETHOD(miibus_statchg, miibus_statchg),
95 DEVMETHOD(miibus_linkchg, miibus_linkchg),
96 DEVMETHOD(miibus_mediainit, miibus_mediainit),
97
98 DEVMETHOD_END
99 };
100
101 devclass_t miibus_devclass;
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
miibus_probe(device_t dev)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
miibus_attach(device_t dev)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
miibus_detach(device_t dev)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
miibus_child_deleted(device_t dev,device_t child)170 miibus_child_deleted(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
miibus_print_child(device_t dev,device_t child)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
miibus_read_ivar(device_t dev,device_t child __unused,int which,uintptr_t * result)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
miibus_child_pnpinfo_str(device_t dev __unused,device_t child,char * buf,size_t buflen)214 miibus_child_pnpinfo_str(device_t dev __unused, device_t child, char *buf,
215 size_t buflen)
216 {
217 struct mii_attach_args *ma;
218
219 ma = device_get_ivars(child);
220 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
221 MII_OUI(ma->mii_id1, ma->mii_id2),
222 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
223 return (0);
224 }
225
226 static int
miibus_child_location_str(device_t dev __unused,device_t child,char * buf,size_t buflen)227 miibus_child_location_str(device_t dev __unused, device_t child, char *buf,
228 size_t buflen)
229 {
230 struct mii_attach_args *ma;
231
232 ma = device_get_ivars(child);
233 snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
234 return (0);
235 }
236
237 static void
miibus_hinted_child(device_t dev,const char * name,int unit)238 miibus_hinted_child(device_t dev, const char *name, int unit)
239 {
240 struct miibus_ivars *ivars;
241 struct mii_attach_args *args, *ma;
242 device_t *children, phy;
243 int i, nchildren;
244 u_int val;
245
246 if (resource_int_value(name, unit, "phyno", &val) != 0)
247 return;
248 if (device_get_children(dev, &children, &nchildren) != 0)
249 return;
250 ma = NULL;
251 for (i = 0; i < nchildren; i++) {
252 args = device_get_ivars(children[i]);
253 if (args->mii_phyno == val) {
254 ma = args;
255 break;
256 }
257 }
258 free(children, M_TEMP);
259
260 /*
261 * Don't add a PHY that was automatically identified by having media
262 * in its BMSR twice, only allow to alter its attach arguments.
263 */
264 if (ma == NULL) {
265 ma = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
266 M_NOWAIT);
267 if (ma == NULL)
268 return;
269 phy = device_add_child(dev, name, unit);
270 if (phy == NULL) {
271 free(ma, M_DEVBUF);
272 return;
273 }
274 ivars = device_get_ivars(dev);
275 ma->mii_phyno = val;
276 ma->mii_offset = ivars->mii_offset++;
277 ma->mii_id1 = 0;
278 ma->mii_id2 = 0;
279 ma->mii_capmask = BMSR_DEFCAPMASK;
280 device_set_ivars(phy, ma);
281 }
282
283 if (resource_int_value(name, unit, "id1", &val) == 0)
284 ma->mii_id1 = val;
285 if (resource_int_value(name, unit, "id2", &val) == 0)
286 ma->mii_id2 = val;
287 if (resource_int_value(name, unit, "capmask", &val) == 0)
288 ma->mii_capmask = val;
289 }
290
291 static int
miibus_readreg(device_t dev,int phy,int reg)292 miibus_readreg(device_t dev, int phy, int reg)
293 {
294 device_t parent;
295
296 parent = device_get_parent(dev);
297 return (MIIBUS_READREG(parent, phy, reg));
298 }
299
300 static int
miibus_writereg(device_t dev,int phy,int reg,int data)301 miibus_writereg(device_t dev, int phy, int reg, int data)
302 {
303 device_t parent;
304
305 parent = device_get_parent(dev);
306 return (MIIBUS_WRITEREG(parent, phy, reg, data));
307 }
308
309 static void
miibus_statchg(device_t dev)310 miibus_statchg(device_t dev)
311 {
312 device_t parent;
313 struct mii_data *mii;
314
315 parent = device_get_parent(dev);
316 MIIBUS_STATCHG(parent);
317
318 mii = device_get_softc(dev);
319 if_setbaudrate(mii->mii_ifp, ifmedia_baudrate(mii->mii_media_active));
320 }
321
322 static void
miibus_linkchg(device_t dev)323 miibus_linkchg(device_t dev)
324 {
325 struct mii_data *mii;
326 device_t parent;
327 int link_state;
328
329 parent = device_get_parent(dev);
330 MIIBUS_LINKCHG(parent);
331
332 mii = device_get_softc(dev);
333
334 if (mii->mii_media_status & IFM_AVALID) {
335 if (mii->mii_media_status & IFM_ACTIVE)
336 link_state = LINK_STATE_UP;
337 else
338 link_state = LINK_STATE_DOWN;
339 } else
340 link_state = LINK_STATE_UNKNOWN;
341 if_link_state_change(mii->mii_ifp, link_state);
342 }
343
344 static void
miibus_mediainit(device_t dev)345 miibus_mediainit(device_t dev)
346 {
347 struct mii_data *mii;
348 struct ifmedia_entry *m;
349 int media = 0;
350
351 /* Poke the parent in case it has any media of its own to add. */
352 MIIBUS_MEDIAINIT(device_get_parent(dev));
353
354 mii = device_get_softc(dev);
355 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
356 media = m->ifm_media;
357 if (media == (IFM_ETHER | IFM_AUTO))
358 break;
359 }
360
361 ifmedia_set(&mii->mii_media, media);
362 }
363
364 /*
365 * Helper function used by network interface drivers, attaches the miibus and
366 * the PHYs to the network interface driver parent.
367 */
368 int
mii_attach(device_t dev,device_t * miibus,if_t ifp,ifm_change_cb_t ifmedia_upd,ifm_stat_cb_t ifmedia_sts,int capmask,int phyloc,int offloc,int flags)369 mii_attach(device_t dev, device_t *miibus, if_t ifp,
370 ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
371 int phyloc, int offloc, int flags)
372 {
373 struct miibus_ivars *ivars;
374 struct mii_attach_args *args, ma;
375 device_t *children, phy;
376 int bmsr, first, i, nchildren, phymax, phymin, rv;
377 uint32_t phymask;
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
mii_mediachg(struct mii_data * mii)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
mii_tick(struct mii_data * mii)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
mii_pollstat(struct mii_data * mii)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
mii_bitreverse(unsigned char x)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
mii_oui(u_int id1,u_int id2)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
mii_phy_mac_match(struct mii_softc * mii,const char * name)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
mii_dev_mac_match(device_t parent,const char * name)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 *
mii_phy_mac_softc(struct mii_softc * mii)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 *
mii_dev_mac_softc(device_t parent)669 mii_dev_mac_softc(device_t parent)
670 {
671
672 return (device_get_softc(device_get_parent(device_get_parent(parent))));
673 }
674