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 "opt_carp.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/socket.h>
54 #include <sys/malloc.h>
55 #include <sys/module.h>
56 #include <sys/bus.h>
57
58 #include <net/if.h>
59 #include <net/if_media.h>
60 #include <net/route.h>
61
62 #ifdef DEV_CARP
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip_carp.h>
66 #endif
67
68 #include <dev/mii/mii.h>
69 #include <dev/mii/miivar.h>
70
71 MODULE_VERSION(miibus, 1);
72
73 #include "miibus_if.h"
74
75 static int miibus_readreg(device_t, int, int);
76 static int miibus_writereg(device_t, int, int, int);
77 static void miibus_statchg(device_t);
78 static void miibus_linkchg(device_t);
79 static void miibus_mediainit(device_t);
80
81 static device_method_t miibus_methods[] = {
82 /* device interface */
83 DEVMETHOD(device_probe, miibus_probe),
84 DEVMETHOD(device_attach, miibus_attach),
85 DEVMETHOD(device_detach, miibus_detach),
86 DEVMETHOD(device_shutdown, bus_generic_shutdown),
87
88 /* bus interface */
89 DEVMETHOD(bus_print_child, bus_generic_print_child),
90 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
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 { 0, 0 }
100 };
101
102 devclass_t miibus_devclass;
103
104 driver_t miibus_driver = {
105 "miibus",
106 miibus_methods,
107 sizeof(struct mii_data)
108 };
109
110 /*
111 * Helper function used by network interface drivers, attaches PHYs
112 * to the network interface driver parent.
113 */
114
115 int
116 miibus_probe(dev)
117 device_t dev;
118 {
119 struct mii_attach_args ma, *args;
120 struct mii_data *mii;
121 device_t child = NULL, parent;
122 int bmsr, capmask = 0xFFFFFFFF;
123
124 mii = device_get_softc(dev);
125 parent = device_get_parent(dev);
126 LIST_INIT(&mii->mii_phys);
127
128 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
129 /*
130 * Check to see if there is a PHY at this address. Note,
131 * many braindead PHYs report 0/0 in their ID registers,
132 * so we test for media in the BMSR.
133 */
134 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
135 if (bmsr == 0 || bmsr == 0xffff ||
136 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
137 /* Assume no PHY at this address. */
138 continue;
139 }
140
141 /*
142 * Extract the IDs. Braindead PHYs will be handled by
143 * the `ukphy' driver, as we have no ID information to
144 * match on.
145 */
146 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
147 MII_PHYIDR1);
148 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
149 MII_PHYIDR2);
150
151 ma.mii_data = mii;
152 ma.mii_capmask = capmask;
153
154 args = malloc(sizeof(struct mii_attach_args),
155 M_DEVBUF, M_NOWAIT);
156 bcopy((char *)&ma, (char *)args, sizeof(ma));
157 child = device_add_child(dev, NULL, -1);
158 device_set_ivars(child, args);
159 }
160
161 if (child == NULL)
162 return(ENXIO);
163
164 device_set_desc(dev, "MII bus");
165
166 return(0);
167 }
168
169 int
170 miibus_attach(dev)
171 device_t dev;
172 {
173 void **v;
174 ifm_change_cb_t ifmedia_upd;
175 ifm_stat_cb_t ifmedia_sts;
176 struct mii_data *mii;
177
178 mii = device_get_softc(dev);
179 /*
180 * Note that each NIC's softc must start with an ifnet structure.
181 */
182 mii->mii_ifp = device_get_softc(device_get_parent(dev));
183 v = device_get_ivars(dev);
184 ifmedia_upd = v[0];
185 ifmedia_sts = v[1];
186 ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts);
187 bus_generic_attach(dev);
188
189 return(0);
190 }
191
192 int
193 miibus_detach(dev)
194 device_t dev;
195 {
196 struct mii_data *mii;
197
198 bus_generic_detach(dev);
199 mii = device_get_softc(dev);
200 ifmedia_removeall(&mii->mii_media);
201 mii->mii_ifp = NULL;
202
203 return(0);
204 }
205
206 static int
207 miibus_readreg(dev, phy, reg)
208 device_t dev;
209 int phy, reg;
210 {
211 device_t parent;
212
213 parent = device_get_parent(dev);
214 return(MIIBUS_READREG(parent, phy, reg));
215 }
216
217 static int
218 miibus_writereg(dev, phy, reg, data)
219 device_t dev;
220 int phy, reg, data;
221 {
222 device_t parent;
223
224 parent = device_get_parent(dev);
225 return(MIIBUS_WRITEREG(parent, phy, reg, data));
226 }
227
228 static void
229 miibus_statchg(dev)
230 device_t dev;
231 {
232 device_t parent;
233
234 parent = device_get_parent(dev);
235 MIIBUS_STATCHG(parent);
236 return;
237 }
238
239 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
240
241 static void
242 miibus_linkchg(dev)
243 device_t dev;
244 {
245 struct mii_data *mii;
246 struct ifnet *ifp;
247 device_t parent;
248 int link, link_state;
249
250 parent = device_get_parent(dev);
251 MIIBUS_LINKCHG(parent);
252
253 mii = device_get_softc(dev);
254 /*
255 * Note that each NIC's softc must start with an ifnet structure.
256 */
257 ifp = device_get_softc(parent);
258
259 if (mii->mii_media_status & IFM_AVALID) {
260 if (mii->mii_media_status & IFM_ACTIVE) {
261 link = NOTE_LINKUP;
262 link_state = LINK_STATE_UP;
263 } else {
264 link = NOTE_LINKDOWN;
265 link_state = LINK_STATE_DOWN;
266 }
267 } else {
268 link = NOTE_LINKINV;
269 link_state = LINK_STATE_UNKNOWN;
270 }
271
272 /* Notify that the link state has changed. */
273 if (ifp->if_link_state != link_state) {
274 ifp->if_link_state = link_state;
275 rt_ifmsg(ifp);
276 KNOTE_UNLOCKED(&ifp->if_klist, link);
277 if (ifp->if_nvlans != 0)
278 (*vlan_link_state_p)(ifp, link);
279 #ifdef DEV_CARP
280 if (ifp->if_carp)
281 carp_carpdev_state(ifp->if_carp);
282 #endif
283 }
284 }
285
286 static void
287 miibus_mediainit(dev)
288 device_t dev;
289 {
290 struct mii_data *mii;
291 struct ifmedia_entry *m;
292 int media = 0;
293
294 /* Poke the parent in case it has any media of its own to add. */
295 MIIBUS_MEDIAINIT(device_get_parent(dev));
296
297 mii = device_get_softc(dev);
298 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
299 media = m->ifm_media;
300 if (media == (IFM_ETHER|IFM_AUTO))
301 break;
302 }
303
304 ifmedia_set(&mii->mii_media, media);
305
306 return;
307 }
308
309 int
310 mii_phy_probe(dev, child, ifmedia_upd, ifmedia_sts)
311 device_t dev;
312 device_t *child;
313 ifm_change_cb_t ifmedia_upd;
314 ifm_stat_cb_t ifmedia_sts;
315 {
316 void **v;
317 int bmsr, i;
318
319 v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT);
320 if (v == 0) {
321 return (ENOMEM);
322 }
323 v[0] = ifmedia_upd;
324 v[1] = ifmedia_sts;
325 *child = device_add_child(dev, "miibus", -1);
326 device_set_ivars(*child, v);
327
328 for (i = 0; i < MII_NPHY; i++) {
329 bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
330 if (bmsr == 0 || bmsr == 0xffff ||
331 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
332 /* Assume no PHY at this address. */
333 continue;
334 } else
335 break;
336 }
337
338 if (i == MII_NPHY) {
339 device_delete_child(dev, *child);
340 *child = NULL;
341 return(ENXIO);
342 }
343
344 bus_generic_attach(dev);
345
346 return(0);
347 }
348
349 /*
350 * Media changed; notify all PHYs.
351 */
352 int
353 mii_mediachg(mii)
354 struct mii_data *mii;
355 {
356 struct mii_softc *child;
357 int rv;
358
359 mii->mii_media_status = 0;
360 mii->mii_media_active = IFM_NONE;
361
362 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
363 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
364 if (rv)
365 return (rv);
366 }
367 return (0);
368 }
369
370 /*
371 * Call the PHY tick routines, used during autonegotiation.
372 */
373 void
374 mii_tick(mii)
375 struct mii_data *mii;
376 {
377 struct mii_softc *child;
378
379 LIST_FOREACH(child, &mii->mii_phys, mii_list)
380 (void) (*child->mii_service)(child, mii, MII_TICK);
381 }
382
383 /*
384 * Get media status from PHYs.
385 */
386 void
387 mii_pollstat(mii)
388 struct mii_data *mii;
389 {
390 struct mii_softc *child;
391
392 mii->mii_media_status = 0;
393 mii->mii_media_active = IFM_NONE;
394
395 LIST_FOREACH(child, &mii->mii_phys, mii_list)
396 (void) (*child->mii_service)(child, mii, MII_POLLSTAT);
397 }
398
399 /*
400 * Inform the PHYs that the interface is down.
401 */
402 void
403 mii_down(struct mii_data *mii)
404 {
405 struct mii_softc *child;
406
407 LIST_FOREACH(child, &mii->mii_phys, mii_list)
408 mii_phy_down(child);
409 }
Cache object: 2741aa10c9432e004ca9bad394029eca
|