xref: /dragonfly/sys/dev/netif/mii_layer/mii.c (revision 92fc8b5c)
1 /*	$NetBSD: mii.c,v 1.40 2005/12/11 12:22:42 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000 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  * $FreeBSD: src/sys/dev/mii/mii.c,v 1.6.2.2 2002/08/19 16:56:33 ambrisko Exp $
40  * $DragonFly: src/sys/dev/netif/mii_layer/mii.c,v 1.12 2008/07/22 10:59:16 sephe Exp $
41  */
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/kernel.h>
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 
57 #include <net/if.h>
58 #include <net/if_media.h>
59 
60 #include "mii.h"
61 #include "miivar.h"
62 
63 #include "miibus_if.h"
64 
65 static int miibus_readreg	(device_t, int, int);
66 static int miibus_writereg	(device_t, int, int, int);
67 static void miibus_statchg	(device_t);
68 static void miibus_mediainit	(device_t);
69 
70 static device_method_t miibus_methods[] = {
71 	/* device interface */
72 	DEVMETHOD(device_probe,		miibus_probe),
73 	DEVMETHOD(device_attach,	miibus_attach),
74 	DEVMETHOD(device_detach,	miibus_detach),
75 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
76 
77 	/* bus interface */
78 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
79 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
80 
81 	/* MII interface */
82 	DEVMETHOD(miibus_readreg,	miibus_readreg),
83 	DEVMETHOD(miibus_writereg,	miibus_writereg),
84 	DEVMETHOD(miibus_statchg,	miibus_statchg),
85 	DEVMETHOD(miibus_mediainit,	miibus_mediainit),
86 
87 	{ 0, 0 }
88 };
89 
90 devclass_t miibus_devclass;
91 
92 driver_t miibus_driver = {
93 	"miibus",
94 	miibus_methods,
95 	sizeof(struct mii_data)
96 };
97 
98 /*
99  * Check to see if there is a PHY at this address.  Note,
100  * many braindead PHYs report 0/0 in their ID registers,
101  * so we test for media in the BMSR.
102  */
103 static __inline int
104 miibus_no_phy(device_t dev, int phyno)
105 {
106 	int bmsr;
107 
108 	bmsr = MIIBUS_READREG(dev, phyno, MII_BMSR);
109 	return (bmsr == 0 || bmsr == 0xffff ||
110 		(bmsr & (BMSR_MEDIAMASK | BMSR_EXTSTAT)) == 0);
111 }
112 
113 /*
114  * Helper function used by network interface drivers, attaches PHYs
115  * to the network interface driver parent.
116  */
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			capmask = 0xFFFFFFFF;
125 
126 	mii = device_get_softc(dev);
127 	parent = device_get_parent(dev);
128 	LIST_INIT(&mii->mii_phys);
129 	bzero(&ma, sizeof(ma));
130 
131 	for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
132 		if (miibus_no_phy(parent, ma.mii_phyno))
133 			continue;
134 
135 		/*
136 		 * Extract the IDs. Braindead PHYs will be handled by
137 		 * the `ukphy' driver, as we have no ID information to
138 		 * match on.
139 	 	 */
140 		ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
141 		    MII_PHYIDR1);
142 		ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
143 		    MII_PHYIDR2);
144 
145 		ma.mii_data = mii;
146 		ma.mii_capmask = capmask;
147 
148 		args = kmalloc(sizeof(struct mii_attach_args),
149 			      M_DEVBUF, M_INTWAIT);
150 		*args = ma;
151 		child = device_add_child(dev, NULL, -1);
152 		device_set_ivars(child, args);
153 	}
154 
155 	if (child == NULL)
156 		return(ENXIO);
157 
158 	device_set_desc(dev, "MII bus");
159 
160 	return(0);
161 }
162 
163 int
164 miibus_attach(device_t dev)
165 {
166 	void			**v;
167 	ifm_change_cb_t		ifmedia_upd;
168 	ifm_stat_cb_t		ifmedia_sts;
169 	struct mii_data		*mii;
170 
171 	mii = device_get_softc(dev);
172 	mii->mii_ifp = device_get_softc(device_get_parent(dev));
173 	v = device_get_ivars(dev);
174 	ifmedia_upd = v[0];
175 	ifmedia_sts = v[1];
176 	ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts);
177 	bus_generic_attach(dev);
178 
179 	return(0);
180 }
181 
182 int
183 miibus_detach(device_t dev)
184 {
185 	struct mii_data		*mii;
186 
187 	bus_generic_detach(dev);
188 	mii = device_get_softc(dev);
189 	ifmedia_removeall(&mii->mii_media);
190 	mii->mii_ifp = NULL;
191 
192 	return(0);
193 }
194 
195 static int
196 miibus_readreg(device_t dev, int phy, int reg)
197 {
198 	device_t		parent;
199 
200 	parent = device_get_parent(dev);
201 	return(MIIBUS_READREG(parent, phy, reg));
202 }
203 
204 static int
205 miibus_writereg(device_t dev, int phy, int reg, int data)
206 {
207 	device_t		parent;
208 
209 	parent = device_get_parent(dev);
210 	return(MIIBUS_WRITEREG(parent, phy, reg, data));
211 }
212 
213 static void
214 miibus_statchg(device_t dev)
215 {
216 	device_t		parent;
217 
218 	parent = device_get_parent(dev);
219 	MIIBUS_STATCHG(parent);
220 	return;
221 }
222 
223 static void
224 miibus_mediainit(device_t dev)
225 {
226 	struct mii_data		*mii;
227 	struct ifmedia_entry	*m;
228 	int			media = 0;
229 
230 	/* Poke the parent in case it has any media of its own to add. */
231 	MIIBUS_MEDIAINIT(device_get_parent(dev));
232 
233 	mii = device_get_softc(dev);
234 	for (m = LIST_FIRST(&mii->mii_media.ifm_list); m != NULL;
235 	     m = LIST_NEXT(m, ifm_list)) {
236 		media = m->ifm_media;
237 		if (media == (IFM_ETHER|IFM_AUTO))
238 			break;
239 	}
240 
241 	ifmedia_set(&mii->mii_media, media);
242 
243 	return;
244 }
245 
246 int
247 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
248 	      ifm_stat_cb_t ifmedia_sts)
249 {
250 	void			**v;
251 	int			i;
252 
253 	v = kmalloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_INTWAIT);
254 	v[0] = ifmedia_upd;
255 	v[1] = ifmedia_sts;
256 	*child = device_add_child(dev, "miibus", -1);
257 	device_set_ivars(*child, v);
258 
259 	for (i = 0; i < MII_NPHY; i++) {
260 		if (!miibus_no_phy(dev, i))
261 			break;
262 	}
263 
264 	if (i == MII_NPHY) {
265 		device_delete_child(dev, *child);
266 		*child = NULL;
267 		return(ENXIO);
268 	}
269 
270 	bus_generic_attach(dev);
271 
272 	return(0);
273 }
274 
275 /*
276  * Media changed; notify all PHYs.
277  */
278 int
279 mii_mediachg(struct mii_data *mii)
280 {
281 	struct mii_softc *child;
282 	int rv;
283 
284 	mii->mii_media_status = 0;
285 	mii->mii_media_active = IFM_NONE;
286 
287 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
288 	     child = LIST_NEXT(child, mii_list)) {
289 		rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
290 		if (rv) {
291 			return (rv);
292 		} else {
293 			/* Reset autonegotiation timer. */
294 			child->mii_ticks = 0;
295 		}
296 	}
297 	return (0);
298 }
299 
300 /*
301  * Call the PHY tick routines, used during autonegotiation.
302  */
303 void
304 mii_tick(struct mii_data *mii)
305 {
306 	struct mii_softc *child;
307 
308 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
309 	     child = LIST_NEXT(child, mii_list))
310 		(*child->mii_service)(child, mii, MII_TICK);
311 }
312 
313 /*
314  * Get media status from PHYs.
315  */
316 void
317 mii_pollstat(struct mii_data *mii)
318 {
319 	struct mii_softc *child;
320 
321 	mii->mii_media_status = 0;
322 	mii->mii_media_active = IFM_NONE;
323 
324 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
325 	     child = LIST_NEXT(child, mii_list))
326 		(*child->mii_service)(child, mii, MII_POLLSTAT);
327 }
328 
329 static moduledata_t miibus_mod = { "miibus" };
330 
331 DECLARE_MODULE(miibus, miibus_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
332 MODULE_VERSION(miibus, 1);
333