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