mii.c revision 1.37 1 1.37 drochner /* $NetBSD: mii.c,v 1.37 2004/09/13 12:55:48 drochner Exp $ */
2 1.6 thorpej
3 1.6 thorpej /*-
4 1.17 thorpej * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 1.6 thorpej * All rights reserved.
6 1.6 thorpej *
7 1.6 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.6 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.6 thorpej * NASA Ames Research Center.
10 1.1 bouyer *
11 1.1 bouyer * Redistribution and use in source and binary forms, with or without
12 1.1 bouyer * modification, are permitted provided that the following conditions
13 1.1 bouyer * are met:
14 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
15 1.1 bouyer * notice, this list of conditions and the following disclaimer.
16 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
18 1.1 bouyer * documentation and/or other materials provided with the distribution.
19 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
20 1.1 bouyer * must display the following acknowledgement:
21 1.6 thorpej * This product includes software developed by the NetBSD
22 1.6 thorpej * Foundation, Inc. and its contributors.
23 1.6 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.6 thorpej * contributors may be used to endorse or promote products derived
25 1.6 thorpej * from this software without specific prior written permission.
26 1.1 bouyer *
27 1.6 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.6 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.6 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.6 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.6 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.6 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.6 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.6 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.6 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.6 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.6 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.6 thorpej */
39 1.6 thorpej
40 1.6 thorpej /*
41 1.6 thorpej * MII bus layer, glues MII-capable network interface drivers to sharable
42 1.22 thorpej * PHY drivers.
43 1.1 bouyer */
44 1.28 lukem
45 1.28 lukem #include <sys/cdefs.h>
46 1.37 drochner __KERNEL_RCSID(0, "$NetBSD: mii.c,v 1.37 2004/09/13 12:55:48 drochner Exp $");
47 1.1 bouyer
48 1.1 bouyer #include <sys/param.h>
49 1.6 thorpej #include <sys/device.h>
50 1.1 bouyer #include <sys/systm.h>
51 1.1 bouyer #include <sys/socket.h>
52 1.1 bouyer
53 1.1 bouyer #include <net/if.h>
54 1.1 bouyer #include <net/if_media.h>
55 1.1 bouyer
56 1.6 thorpej #include <dev/mii/mii.h>
57 1.6 thorpej #include <dev/mii/miivar.h>
58 1.1 bouyer
59 1.36 drochner #include "locators.h"
60 1.36 drochner
61 1.35 thorpej static int mii_print(void *, const char *);
62 1.37 drochner static int mii_submatch(struct device *, struct cfdata *,
63 1.37 drochner const locdesc_t *, void *);
64 1.1 bouyer
65 1.6 thorpej /*
66 1.6 thorpej * Helper function used by network interface drivers, attaches PHYs
67 1.6 thorpej * to the network interface driver parent.
68 1.6 thorpej */
69 1.6 thorpej void
70 1.27 thorpej mii_attach(struct device *parent, struct mii_data *mii, int capmask,
71 1.27 thorpej int phyloc, int offloc, int flags)
72 1.6 thorpej {
73 1.6 thorpej struct mii_attach_args ma;
74 1.6 thorpej struct mii_softc *child;
75 1.13 thorpej int bmsr, offset = 0;
76 1.15 thorpej int phymin, phymax;
77 1.37 drochner int help[2];
78 1.37 drochner locdesc_t *ldesc = (void *)help; /* XXX */
79 1.1 bouyer
80 1.34 yamt if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
81 1.18 thorpej panic("mii_attach: phyloc and offloc specified");
82 1.1 bouyer
83 1.15 thorpej if (phyloc == MII_PHY_ANY) {
84 1.15 thorpej phymin = 0;
85 1.15 thorpej phymax = MII_NPHY - 1;
86 1.15 thorpej } else
87 1.15 thorpej phymin = phymax = phyloc;
88 1.15 thorpej
89 1.15 thorpej if ((mii->mii_flags & MIIF_INITDONE) == 0) {
90 1.15 thorpej LIST_INIT(&mii->mii_phys);
91 1.15 thorpej mii->mii_flags |= MIIF_INITDONE;
92 1.15 thorpej }
93 1.15 thorpej
94 1.15 thorpej for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
95 1.13 thorpej /*
96 1.13 thorpej * Make sure we haven't already configured a PHY at this
97 1.18 thorpej * address. This allows mii_attach() to be called
98 1.13 thorpej * multiple times.
99 1.13 thorpej */
100 1.13 thorpej for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
101 1.13 thorpej child = LIST_NEXT(child, mii_list)) {
102 1.13 thorpej if (child->mii_phy == ma.mii_phyno) {
103 1.13 thorpej /*
104 1.13 thorpej * Yes, there is already something
105 1.13 thorpej * configured at this address.
106 1.13 thorpej */
107 1.14 thorpej offset++;
108 1.13 thorpej continue;
109 1.13 thorpej }
110 1.13 thorpej }
111 1.13 thorpej
112 1.6 thorpej /*
113 1.11 thorpej * Check to see if there is a PHY at this address. Note,
114 1.11 thorpej * many braindead PHYs report 0/0 in their ID registers,
115 1.11 thorpej * so we test for media in the BMSR.
116 1.11 thorpej */
117 1.11 thorpej bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
118 1.11 thorpej if (bmsr == 0 || bmsr == 0xffff ||
119 1.26 thorpej (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
120 1.11 thorpej /* Assume no PHY at this address. */
121 1.15 thorpej continue;
122 1.15 thorpej }
123 1.15 thorpej
124 1.15 thorpej /*
125 1.15 thorpej * There is a PHY at this address. If we were given an
126 1.15 thorpej * `offset' locator, skip this PHY if it doesn't match.
127 1.15 thorpej */
128 1.15 thorpej if (offloc != MII_OFFSET_ANY && offloc != offset) {
129 1.15 thorpej offset++;
130 1.11 thorpej continue;
131 1.11 thorpej }
132 1.11 thorpej
133 1.11 thorpej /*
134 1.11 thorpej * Extract the IDs. Braindead PHYs will be handled by
135 1.11 thorpej * the `ukphy' driver, as we have no ID information to
136 1.11 thorpej * match on.
137 1.6 thorpej */
138 1.6 thorpej ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
139 1.6 thorpej MII_PHYIDR1);
140 1.6 thorpej ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
141 1.6 thorpej MII_PHYIDR2);
142 1.1 bouyer
143 1.6 thorpej ma.mii_data = mii;
144 1.6 thorpej ma.mii_capmask = capmask;
145 1.29 thorpej ma.mii_flags = flags | (mii->mii_flags & MIIF_INHERIT_MASK);
146 1.1 bouyer
147 1.37 drochner ldesc->len = 1;
148 1.37 drochner ldesc->locs[MIICF_PHY] = ma.mii_phyno;
149 1.37 drochner
150 1.37 drochner child = (struct mii_softc *)config_found_sm_loc(parent, "mii",
151 1.37 drochner ldesc, &ma, mii_print, mii_submatch);
152 1.37 drochner if (child) {
153 1.6 thorpej /*
154 1.6 thorpej * Link it up in the parent's MII data.
155 1.6 thorpej */
156 1.20 thorpej callout_init(&child->mii_nway_ch);
157 1.6 thorpej LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
158 1.13 thorpej child->mii_offset = offset;
159 1.6 thorpej mii->mii_instance++;
160 1.6 thorpej }
161 1.13 thorpej offset++;
162 1.17 thorpej }
163 1.17 thorpej }
164 1.17 thorpej
165 1.17 thorpej void
166 1.27 thorpej mii_activate(struct mii_data *mii, enum devact act, int phyloc, int offloc)
167 1.17 thorpej {
168 1.17 thorpej struct mii_softc *child;
169 1.17 thorpej
170 1.34 yamt if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
171 1.18 thorpej panic("mii_activate: phyloc and offloc specified");
172 1.17 thorpej
173 1.17 thorpej if ((mii->mii_flags & MIIF_INITDONE) == 0)
174 1.17 thorpej return;
175 1.17 thorpej
176 1.17 thorpej for (child = LIST_FIRST(&mii->mii_phys);
177 1.17 thorpej child != NULL; child = LIST_NEXT(child, mii_list)) {
178 1.17 thorpej if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
179 1.17 thorpej if (phyloc != MII_PHY_ANY &&
180 1.17 thorpej phyloc != child->mii_phy)
181 1.17 thorpej continue;
182 1.17 thorpej if (offloc != MII_OFFSET_ANY &&
183 1.17 thorpej offloc != child->mii_offset)
184 1.17 thorpej continue;
185 1.17 thorpej }
186 1.17 thorpej switch (act) {
187 1.17 thorpej case DVACT_ACTIVATE:
188 1.18 thorpej panic("mii_activate: DVACT_ACTIVATE");
189 1.17 thorpej break;
190 1.17 thorpej
191 1.17 thorpej case DVACT_DEACTIVATE:
192 1.17 thorpej if (config_deactivate(&child->mii_dev) != 0)
193 1.31 provos panic("%s: config_activate(%d) failed",
194 1.17 thorpej child->mii_dev.dv_xname, act);
195 1.17 thorpej }
196 1.17 thorpej }
197 1.17 thorpej }
198 1.17 thorpej
199 1.17 thorpej void
200 1.27 thorpej mii_detach(struct mii_data *mii, int phyloc, int offloc)
201 1.17 thorpej {
202 1.17 thorpej struct mii_softc *child, *nchild;
203 1.17 thorpej
204 1.17 thorpej if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
205 1.18 thorpej panic("mii_detach: phyloc and offloc specified");
206 1.17 thorpej
207 1.17 thorpej if ((mii->mii_flags & MIIF_INITDONE) == 0)
208 1.17 thorpej return;
209 1.17 thorpej
210 1.17 thorpej for (child = LIST_FIRST(&mii->mii_phys);
211 1.17 thorpej child != NULL; child = nchild) {
212 1.17 thorpej nchild = LIST_NEXT(child, mii_list);
213 1.17 thorpej if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
214 1.17 thorpej if (phyloc != MII_PHY_ANY &&
215 1.17 thorpej phyloc != child->mii_phy)
216 1.17 thorpej continue;
217 1.17 thorpej if (offloc != MII_OFFSET_ANY &&
218 1.17 thorpej offloc != child->mii_offset)
219 1.17 thorpej continue;
220 1.17 thorpej }
221 1.17 thorpej LIST_REMOVE(child, mii_list);
222 1.17 thorpej (void) config_detach(&child->mii_dev, DETACH_FORCE);
223 1.6 thorpej }
224 1.4 bouyer }
225 1.1 bouyer
226 1.35 thorpej static int
227 1.27 thorpej mii_print(void *aux, const char *pnp)
228 1.1 bouyer {
229 1.6 thorpej struct mii_attach_args *ma = aux;
230 1.2 thorpej
231 1.6 thorpej if (pnp != NULL)
232 1.33 thorpej aprint_normal("OUI 0x%06x model 0x%04x rev %d at %s",
233 1.6 thorpej MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
234 1.6 thorpej MII_REV(ma->mii_id2), pnp);
235 1.6 thorpej
236 1.33 thorpej aprint_normal(" phy %d", ma->mii_phyno);
237 1.1 bouyer return (UNCONF);
238 1.1 bouyer }
239 1.1 bouyer
240 1.35 thorpej static int
241 1.37 drochner mii_submatch(struct device *parent, struct cfdata *cf,
242 1.37 drochner const locdesc_t *ldesc, void *aux)
243 1.1 bouyer {
244 1.1 bouyer
245 1.37 drochner if (cf->cf_loc[MIICF_PHY] != MIICF_PHY_DEFAULT &&
246 1.37 drochner cf->cf_loc[MIICF_PHY] != ldesc->locs[MIICF_PHY])
247 1.6 thorpej return (0);
248 1.1 bouyer
249 1.30 thorpej return (config_match(parent, cf, aux));
250 1.1 bouyer }
251 1.1 bouyer
252 1.6 thorpej /*
253 1.6 thorpej * Media changed; notify all PHYs.
254 1.6 thorpej */
255 1.6 thorpej int
256 1.27 thorpej mii_mediachg(struct mii_data *mii)
257 1.1 bouyer {
258 1.6 thorpej struct mii_softc *child;
259 1.6 thorpej int rv;
260 1.1 bouyer
261 1.6 thorpej mii->mii_media_status = 0;
262 1.6 thorpej mii->mii_media_active = IFM_NONE;
263 1.6 thorpej
264 1.6 thorpej for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
265 1.6 thorpej child = LIST_NEXT(child, mii_list)) {
266 1.21 thorpej rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
267 1.6 thorpej if (rv)
268 1.6 thorpej return (rv);
269 1.1 bouyer }
270 1.6 thorpej return (0);
271 1.1 bouyer }
272 1.1 bouyer
273 1.6 thorpej /*
274 1.6 thorpej * Call the PHY tick routines, used during autonegotiation.
275 1.6 thorpej */
276 1.6 thorpej void
277 1.27 thorpej mii_tick(struct mii_data *mii)
278 1.1 bouyer {
279 1.6 thorpej struct mii_softc *child;
280 1.1 bouyer
281 1.6 thorpej for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
282 1.6 thorpej child = LIST_NEXT(child, mii_list))
283 1.21 thorpej (void) PHY_SERVICE(child, mii, MII_TICK);
284 1.1 bouyer }
285 1.1 bouyer
286 1.6 thorpej /*
287 1.6 thorpej * Get media status from PHYs.
288 1.6 thorpej */
289 1.2 thorpej void
290 1.27 thorpej mii_pollstat(struct mii_data *mii)
291 1.1 bouyer {
292 1.6 thorpej struct mii_softc *child;
293 1.1 bouyer
294 1.6 thorpej mii->mii_media_status = 0;
295 1.6 thorpej mii->mii_media_active = IFM_NONE;
296 1.1 bouyer
297 1.6 thorpej for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
298 1.6 thorpej child = LIST_NEXT(child, mii_list))
299 1.21 thorpej (void) PHY_SERVICE(child, mii, MII_POLLSTAT);
300 1.16 thorpej }
301 1.16 thorpej
302 1.16 thorpej /*
303 1.16 thorpej * Inform the PHYs that the interface is down.
304 1.16 thorpej */
305 1.16 thorpej void
306 1.27 thorpej mii_down(struct mii_data *mii)
307 1.16 thorpej {
308 1.16 thorpej struct mii_softc *child;
309 1.16 thorpej
310 1.16 thorpej for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
311 1.16 thorpej child = LIST_NEXT(child, mii_list))
312 1.21 thorpej (void) PHY_SERVICE(child, mii, MII_DOWN);
313 1.23 drochner }
314 1.23 drochner
315 1.23 drochner static unsigned char
316 1.23 drochner bitreverse(unsigned char x)
317 1.23 drochner {
318 1.23 drochner static unsigned char nibbletab[16] = {
319 1.23 drochner 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
320 1.23 drochner };
321 1.23 drochner
322 1.23 drochner return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
323 1.23 drochner }
324 1.23 drochner
325 1.32 thorpej u_int
326 1.32 thorpej mii_oui(u_int id1, u_int id2)
327 1.23 drochner {
328 1.32 thorpej u_int h;
329 1.23 drochner
330 1.23 drochner h = (id1 << 6) | (id2 >> 10);
331 1.23 drochner
332 1.23 drochner return ((bitreverse(h >> 16) << 16) |
333 1.23 drochner (bitreverse((h >> 8) & 255) << 8) |
334 1.23 drochner bitreverse(h & 255));
335 1.1 bouyer }
336