FreeBSD/Linux Kernel Cross Reference
sys/dev/mii/mii.c
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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 /*
44 * MII bus layer, glues MII-capable network interface drivers to sharable
45 * PHY drivers. This exports an interface compatible with BSD/OS 3.0's,
46 * plus some NetBSD extensions.
47 */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/bus.h>
55
56 #include <net/if.h>
57 #include <net/if_media.h>
58 #include <net/route.h>
59
60 #include <dev/mii/mii.h>
61 #include <dev/mii/miivar.h>
62
63 MODULE_VERSION(miibus, 1);
64
65 #include "miibus_if.h"
66
67 static int miibus_print_child(device_t dev, device_t child);
68 static int miibus_child_location_str(device_t bus, device_t child, char *buf,
69 size_t buflen);
70 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
71 size_t buflen);
72 static int miibus_readreg(device_t, int, int);
73 static int miibus_writereg(device_t, int, int, int);
74 static void miibus_statchg(device_t);
75 static void miibus_linkchg(device_t);
76 static void miibus_mediainit(device_t);
77
78 static device_method_t miibus_methods[] = {
79 /* device interface */
80 DEVMETHOD(device_probe, miibus_probe),
81 DEVMETHOD(device_attach, miibus_attach),
82 DEVMETHOD(device_detach, miibus_detach),
83 DEVMETHOD(device_shutdown, bus_generic_shutdown),
84
85 /* bus interface */
86 DEVMETHOD(bus_print_child, miibus_print_child),
87 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
88 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
89 DEVMETHOD(bus_child_location_str, miibus_child_location_str),
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 { 0, 0 }
99 };
100
101 devclass_t miibus_devclass;
102
103 driver_t miibus_driver = {
104 "miibus",
105 miibus_methods,
106 sizeof(struct mii_data)
107 };
108
109 struct miibus_ivars {
110 ifm_change_cb_t ifmedia_upd;
111 ifm_stat_cb_t ifmedia_sts;
112 };
113
114 /*
115 * Helper function used by network interface drivers, attaches PHYs
116 * to the network interface driver parent.
117 */
118 int
119 miibus_probe(device_t dev)
120 {
121 struct mii_attach_args ma, *args;
122 struct mii_data *mii;
123 device_t child = NULL, parent;
124 int bmsr, capmask = 0xFFFFFFFF;
125
126 mii = device_get_softc(dev);
127 parent = device_get_parent(dev);
128 LIST_INIT(&mii->mii_phys);
129
130 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
131 /*
132 * Check to see if there is a PHY at this address. Note,
133 * many braindead PHYs report 0/0 in their ID registers,
134 * so we test for media in the BMSR.
135 */
136 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
137 if (bmsr == 0 || bmsr == 0xffff ||
138 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
139 /* Assume no PHY at this address. */
140 continue;
141 }
142
143 /*
144 * Extract the IDs. Braindead PHYs will be handled by
145 * the `ukphy' driver, as we have no ID information to
146 * match on.
147 */
148 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
149 MII_PHYIDR1);
150 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
151 MII_PHYIDR2);
152
153 ma.mii_data = mii;
154 ma.mii_capmask = capmask;
155
156 args = malloc(sizeof(struct mii_attach_args),
157 M_DEVBUF, M_NOWAIT);
158 bcopy((char *)&ma, (char *)args, sizeof(ma));
159 child = device_add_child(dev, NULL, -1);
160 device_set_ivars(child, args);
161 }
162
163 if (child == NULL)
164 return(ENXIO);
165
166 device_set_desc(dev, "MII bus");
167
168 return(0);
169 }
170
171 int
172 miibus_attach(device_t dev)
173 {
174 struct miibus_ivars *ivars;
175 struct mii_data *mii;
176
177 mii = device_get_softc(dev);
178 /*
179 * Note that each NIC's softc must start with an ifnet pointer.
180 * XXX: EVIL HACK!
181 */
182 mii->mii_ifp = *(struct ifnet**)device_get_softc(device_get_parent(dev));
183 ivars = device_get_ivars(dev);
184 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
185 ivars->ifmedia_sts);
186 bus_generic_attach(dev);
187
188 return(0);
189 }
190
191 int
192 miibus_detach(device_t dev)
193 {
194 struct mii_data *mii;
195
196 bus_generic_detach(dev);
197 mii = device_get_softc(dev);
198 ifmedia_removeall(&mii->mii_media);
199 mii->mii_ifp = NULL;
200
201 return(0);
202 }
203
204 static int
205 miibus_print_child(device_t dev, device_t child)
206 {
207 struct mii_attach_args *ma;
208 int retval;
209
210 ma = device_get_ivars(child);
211 retval = bus_print_child_header(dev, child);
212 retval += printf(" PHY %d", ma->mii_phyno);
213 retval += bus_print_child_footer(dev, child);
214
215 return (retval);
216 }
217
218 static int
219 miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
220 size_t buflen)
221 {
222 struct mii_attach_args *maa = device_get_ivars(child);
223 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
224 MII_OUI(maa->mii_id1, maa->mii_id2),
225 MII_MODEL(maa->mii_id2), MII_REV(maa->mii_id2));
226 return (0);
227 }
228
229 static int
230 miibus_child_location_str(device_t bus, device_t child, char *buf,
231 size_t buflen)
232 {
233 struct mii_attach_args *maa = device_get_ivars(child);
234 snprintf(buf, buflen, "phyno=%d", maa->mii_phyno);
235 return (0);
236 }
237
238 static int
239 miibus_readreg(device_t dev, int phy, int reg)
240 {
241 device_t parent;
242
243 parent = device_get_parent(dev);
244 return(MIIBUS_READREG(parent, phy, reg));
245 }
246
247 static int
248 miibus_writereg(device_t dev, int phy, int reg, int data)
249 {
250 device_t parent;
251
252 parent = device_get_parent(dev);
253 return(MIIBUS_WRITEREG(parent, phy, reg, data));
254 }
255
256 static void
257 miibus_statchg(device_t dev)
258 {
259 device_t parent;
260 struct mii_data *mii;
261 struct ifnet *ifp;
262
263 parent = device_get_parent(dev);
264 MIIBUS_STATCHG(parent);
265
266 mii = device_get_softc(dev);
267
268 /*
269 * Note that each NIC's softc must start with an ifnet pointer.
270 * XXX: EVIL HACK!
271 */
272 ifp = *(struct ifnet **)device_get_softc(parent);
273 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
274 return;
275 }
276
277 static void
278 miibus_linkchg(device_t dev)
279 {
280 struct mii_data *mii;
281 device_t parent;
282 int link_state;
283
284 parent = device_get_parent(dev);
285 MIIBUS_LINKCHG(parent);
286
287 mii = device_get_softc(dev);
288
289 if (mii->mii_media_status & IFM_AVALID) {
290 if (mii->mii_media_status & IFM_ACTIVE)
291 link_state = LINK_STATE_UP;
292 else
293 link_state = LINK_STATE_DOWN;
294 } else
295 link_state = LINK_STATE_UNKNOWN;
296 /*
297 * Note that each NIC's softc must start with an ifnet pointer.
298 * XXX: EVIL HACK!
299 */
300 if_link_state_change(*(struct ifnet**)device_get_softc(parent), link_state);
301 }
302
303 static void
304 miibus_mediainit(device_t dev)
305 {
306 struct mii_data *mii;
307 struct ifmedia_entry *m;
308 int media = 0;
309
310 /* Poke the parent in case it has any media of its own to add. */
311 MIIBUS_MEDIAINIT(device_get_parent(dev));
312
313 mii = device_get_softc(dev);
314 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
315 media = m->ifm_media;
316 if (media == (IFM_ETHER|IFM_AUTO))
317 break;
318 }
319
320 ifmedia_set(&mii->mii_media, media);
321
322 return;
323 }
324
325 int
326 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
327 ifm_stat_cb_t ifmedia_sts)
328 {
329 struct miibus_ivars *ivars;
330 int bmsr, i;
331
332 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
333 if (ivars == NULL)
334 return (ENOMEM);
335 ivars->ifmedia_upd = ifmedia_upd;
336 ivars->ifmedia_sts = ifmedia_sts;
337 *child = device_add_child(dev, "miibus", -1);
338 device_set_ivars(*child, ivars);
339
340 for (i = 0; i < MII_NPHY; i++) {
341 bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
342 if (bmsr == 0 || bmsr == 0xffff ||
343 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
344 /* Assume no PHY at this address. */
345 continue;
346 } else
347 break;
348 }
349
350 if (i == MII_NPHY) {
351 device_delete_child(dev, *child);
352 *child = NULL;
353 return(ENXIO);
354 }
355
356 bus_generic_attach(dev);
357
358 return(0);
359 }
360
361 /*
362 * Media changed; notify all PHYs.
363 */
364 int
365 mii_mediachg(struct mii_data *mii)
366 {
367 struct mii_softc *child;
368 int rv;
369
370 mii->mii_media_status = 0;
371 mii->mii_media_active = IFM_NONE;
372
373 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
374 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
375 if (rv)
376 return (rv);
377 }
378 return (0);
379 }
380
381 /*
382 * Call the PHY tick routines, used during autonegotiation.
383 */
384 void
385 mii_tick(struct mii_data *mii)
386 {
387 struct mii_softc *child;
388
389 LIST_FOREACH(child, &mii->mii_phys, mii_list)
390 (void) (*child->mii_service)(child, mii, MII_TICK);
391 }
392
393 /*
394 * Get media status from PHYs.
395 */
396 void
397 mii_pollstat(struct mii_data *mii)
398 {
399 struct mii_softc *child;
400
401 mii->mii_media_status = 0;
402 mii->mii_media_active = IFM_NONE;
403
404 LIST_FOREACH(child, &mii->mii_phys, mii_list)
405 (void) (*child->mii_service)(child, mii, MII_POLLSTAT);
406 }
407
408 /*
409 * Inform the PHYs that the interface is down.
410 */
411 void
412 mii_down(struct mii_data *mii)
413 {
414 struct mii_softc *child;
415
416 LIST_FOREACH(child, &mii->mii_phys, mii_list)
417 mii_phy_down(child);
418 }
Cache object: 6b805e283fb5b33610984d73c52b4b2c
|