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