mii.c revision 101492
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/*
41 * MII bus layer, glues MII-capable network interface drivers to sharable
42 * PHY drivers.  This exports an interface compatible with BSD/OS 3.0's,
43 * plus some NetBSD extensions.
44 */
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/socket.h>
49#include <sys/malloc.h>
50#include <sys/module.h>
51#include <sys/bus.h>
52
53#include <net/if.h>
54#include <net/if_media.h>
55
56#include <dev/mii/mii.h>
57#include <dev/mii/miivar.h>
58
59MODULE_VERSION(miibus, 1);
60
61#include "miibus_if.h"
62
63#if !defined(lint)
64static const char rcsid[] =
65  "$FreeBSD: head/sys/dev/mii/mii.c 101492 2002-08-07 22:18:33Z ambrisko $";
66#endif
67
68static int miibus_readreg	(device_t, int, int);
69static int miibus_writereg	(device_t, int, int, int);
70static void miibus_statchg	(device_t);
71static void miibus_linkchg	(device_t);
72static void miibus_mediainit	(device_t);
73
74static device_method_t miibus_methods[] = {
75	/* device interface */
76	DEVMETHOD(device_probe,		miibus_probe),
77	DEVMETHOD(device_attach,	miibus_attach),
78	DEVMETHOD(device_detach,	miibus_detach),
79	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
80
81	/* bus interface */
82	DEVMETHOD(bus_print_child,	bus_generic_print_child),
83	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
84
85	/* MII interface */
86	DEVMETHOD(miibus_readreg,	miibus_readreg),
87	DEVMETHOD(miibus_writereg,	miibus_writereg),
88	DEVMETHOD(miibus_statchg,	miibus_statchg),
89	DEVMETHOD(miibus_linkchg,	miibus_linkchg),
90	DEVMETHOD(miibus_mediainit,	miibus_mediainit),
91
92	{ 0, 0 }
93};
94
95devclass_t miibus_devclass;
96
97driver_t miibus_driver = {
98	"miibus",
99	miibus_methods,
100	sizeof(struct mii_data)
101};
102
103/*
104 * Helper function used by network interface drivers, attaches PHYs
105 * to the network interface driver parent.
106 */
107
108int 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, *children;
114	int			bmsr, capmask = 0xFFFFFFFF, nchildren;
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		device_get_children(dev, &children, &nchildren);
122		if (nchildren)
123			break;
124		/*
125		 * Check to see if there is a PHY at this address.  Note,
126		 * many braindead PHYs report 0/0 in their ID registers,
127		 * so we test for media in the BMSR.
128	 	 */
129		bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
130		if (bmsr == 0 || bmsr == 0xffff ||
131		    (bmsr & BMSR_MEDIAMASK) == 0) {
132			/* Assume no PHY at this address. */
133			continue;
134		}
135
136		/*
137		 * Extract the IDs. Braindead PHYs will be handled by
138		 * the `ukphy' driver, as we have no ID information to
139		 * match on.
140	 	 */
141		ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
142		    MII_PHYIDR1);
143		ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
144		    MII_PHYIDR2);
145
146		ma.mii_data = mii;
147		ma.mii_capmask = capmask;
148
149		args = malloc(sizeof(struct mii_attach_args),
150		    M_DEVBUF, M_NOWAIT);
151		bcopy((char *)&ma, (char *)args, sizeof(ma));
152		child = device_add_child(dev, NULL, -1);
153		device_set_ivars(child, args);
154	}
155
156	if (child == NULL)
157		return(ENXIO);
158
159	device_set_desc(dev, "MII bus");
160
161	return(0);
162}
163
164int 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
186int miibus_detach(dev)
187	device_t		dev;
188{
189	struct mii_data		*mii;
190
191	bus_generic_detach(dev);
192	mii = device_get_softc(dev);
193	ifmedia_removeall(&mii->mii_media);
194	mii->mii_ifp = NULL;
195
196	return(0);
197}
198
199static int 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
209static int miibus_writereg(dev, phy, reg, data)
210	device_t		dev;
211	int			phy, reg, data;
212{
213	device_t		parent;
214
215	parent = device_get_parent(dev);
216	return(MIIBUS_WRITEREG(parent, phy, reg, data));
217}
218
219static void miibus_statchg(dev)
220	device_t		dev;
221{
222	device_t		parent;
223
224	parent = device_get_parent(dev);
225	MIIBUS_STATCHG(parent);
226	return;
227}
228
229static void
230miibus_linkchg(dev)
231	device_t dev;
232{
233	struct mii_data *mii;
234	struct ifnet *ifp;
235	device_t parent;
236	int link;
237
238	parent = device_get_parent(dev);
239	MIIBUS_LINKCHG(parent);
240
241	mii = device_get_softc(dev);
242	/*
243	 * Note that each NIC's softc must start with an ifnet structure.
244	 */
245	ifp = device_get_softc(parent);
246
247	if (mii->mii_media_status & IFM_AVALID) {
248		if (mii->mii_media_status & IFM_ACTIVE)
249			link = NOTE_LINKUP;
250		else
251			link = NOTE_LINKDOWN;
252	} else {
253		link = NOTE_LINKINV;
254	}
255
256	KNOTE(&ifp->if_klist, link);
257}
258
259static void miibus_mediainit(dev)
260	device_t		dev;
261{
262	struct mii_data		*mii;
263	struct ifmedia_entry	*m;
264	int			media = 0;
265
266	/* Poke the parent in case it has any media of its own to add. */
267	MIIBUS_MEDIAINIT(device_get_parent(dev));
268
269	mii = device_get_softc(dev);
270	LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
271		media = m->ifm_media;
272		if (media == (IFM_ETHER|IFM_AUTO))
273			break;
274	}
275
276	ifmedia_set(&mii->mii_media, media);
277
278	return;
279}
280
281int 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_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 */
323int
324mii_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 */
344void
345mii_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 */
357void
358mii_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 */
373void
374mii_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