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