mii.c revision 1.57 1 1.57 thorpej /* $NetBSD: mii.c,v 1.57 2021/08/07 16:19:13 thorpej Exp $ */
2 1.6 thorpej
3 1.6 thorpej /*-
4 1.55 thorpej * Copyright (c) 1998, 2000, 2020 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 *
20 1.6 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.6 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.6 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.6 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.6 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.6 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.6 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.6 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.6 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.6 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.6 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.6 thorpej */
32 1.6 thorpej
33 1.6 thorpej /*
34 1.6 thorpej * MII bus layer, glues MII-capable network interface drivers to sharable
35 1.22 thorpej * PHY drivers.
36 1.1 bouyer */
37 1.28 lukem
38 1.28 lukem #include <sys/cdefs.h>
39 1.57 thorpej __KERNEL_RCSID(0, "$NetBSD: mii.c,v 1.57 2021/08/07 16:19:13 thorpej Exp $");
40 1.55 thorpej
41 1.55 thorpej #define __IFMEDIA_PRIVATE
42 1.1 bouyer
43 1.1 bouyer #include <sys/param.h>
44 1.6 thorpej #include <sys/device.h>
45 1.1 bouyer #include <sys/systm.h>
46 1.1 bouyer #include <sys/socket.h>
47 1.1 bouyer
48 1.1 bouyer #include <net/if.h>
49 1.1 bouyer #include <net/if_media.h>
50 1.1 bouyer
51 1.6 thorpej #include <dev/mii/mii.h>
52 1.6 thorpej #include <dev/mii/miivar.h>
53 1.1 bouyer
54 1.36 drochner #include "locators.h"
55 1.36 drochner
56 1.35 thorpej static int mii_print(void *, const char *);
57 1.1 bouyer
58 1.6 thorpej /*
59 1.6 thorpej * Helper function used by network interface drivers, attaches PHYs
60 1.6 thorpej * to the network interface driver parent.
61 1.6 thorpej */
62 1.6 thorpej void
63 1.47 xtraeme mii_attach(device_t parent, struct mii_data *mii, int capmask,
64 1.27 thorpej int phyloc, int offloc, int flags)
65 1.6 thorpej {
66 1.6 thorpej struct mii_attach_args ma;
67 1.6 thorpej struct mii_softc *child;
68 1.52 msaitoh int offset = 0;
69 1.52 msaitoh uint16_t bmsr;
70 1.15 thorpej int phymin, phymax;
71 1.38 drochner int locs[MIICF_NLOCS];
72 1.52 msaitoh int rv;
73 1.1 bouyer
74 1.55 thorpej KASSERT(mii->mii_media.ifm_lock != NULL);
75 1.55 thorpej
76 1.34 yamt if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
77 1.18 thorpej panic("mii_attach: phyloc and offloc specified");
78 1.1 bouyer
79 1.15 thorpej if (phyloc == MII_PHY_ANY) {
80 1.15 thorpej phymin = 0;
81 1.15 thorpej phymax = MII_NPHY - 1;
82 1.15 thorpej } else
83 1.15 thorpej phymin = phymax = phyloc;
84 1.15 thorpej
85 1.55 thorpej mii_lock(mii);
86 1.55 thorpej
87 1.15 thorpej if ((mii->mii_flags & MIIF_INITDONE) == 0) {
88 1.15 thorpej LIST_INIT(&mii->mii_phys);
89 1.55 thorpej cv_init(&mii->mii_probe_cv, "mii_attach");
90 1.15 thorpej mii->mii_flags |= MIIF_INITDONE;
91 1.15 thorpej }
92 1.15 thorpej
93 1.55 thorpej /*
94 1.55 thorpej * Probing temporarily unlocks the MII; wait until anyone
95 1.55 thorpej * else who might be doing this to finish.
96 1.55 thorpej */
97 1.55 thorpej mii->mii_probe_waiters++;
98 1.55 thorpej for (;;) {
99 1.55 thorpej if (mii->mii_flags & MIIF_EXITING) {
100 1.55 thorpej mii->mii_probe_waiters--;
101 1.55 thorpej cv_signal(&mii->mii_probe_cv);
102 1.55 thorpej mii_unlock(mii);
103 1.55 thorpej return;
104 1.55 thorpej }
105 1.55 thorpej if ((mii->mii_flags & MIIF_PROBING) == 0)
106 1.55 thorpej break;
107 1.55 thorpej cv_wait(&mii->mii_probe_cv, mii->mii_media.ifm_lock);
108 1.55 thorpej }
109 1.55 thorpej mii->mii_probe_waiters--;
110 1.55 thorpej
111 1.55 thorpej /* ...and old others off. */
112 1.55 thorpej mii->mii_flags |= MIIF_PROBING;
113 1.55 thorpej
114 1.15 thorpej for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
115 1.13 thorpej /*
116 1.13 thorpej * Make sure we haven't already configured a PHY at this
117 1.18 thorpej * address. This allows mii_attach() to be called
118 1.13 thorpej * multiple times.
119 1.13 thorpej */
120 1.42 dyoung LIST_FOREACH(child, &mii->mii_phys, mii_list) {
121 1.13 thorpej if (child->mii_phy == ma.mii_phyno) {
122 1.13 thorpej /*
123 1.13 thorpej * Yes, there is already something
124 1.13 thorpej * configured at this address.
125 1.13 thorpej */
126 1.14 thorpej offset++;
127 1.13 thorpej continue;
128 1.13 thorpej }
129 1.13 thorpej }
130 1.13 thorpej
131 1.6 thorpej /*
132 1.11 thorpej * Check to see if there is a PHY at this address. Note,
133 1.11 thorpej * many braindead PHYs report 0/0 in their ID registers,
134 1.11 thorpej * so we test for media in the BMSR.
135 1.11 thorpej */
136 1.52 msaitoh bmsr = 0;
137 1.52 msaitoh rv = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR,
138 1.52 msaitoh &bmsr);
139 1.52 msaitoh if ((rv != 0) || bmsr == 0 || bmsr == 0xffff ||
140 1.53 msaitoh (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
141 1.11 thorpej /* Assume no PHY at this address. */
142 1.15 thorpej continue;
143 1.15 thorpej }
144 1.15 thorpej
145 1.15 thorpej /*
146 1.15 thorpej * There is a PHY at this address. If we were given an
147 1.15 thorpej * `offset' locator, skip this PHY if it doesn't match.
148 1.15 thorpej */
149 1.15 thorpej if (offloc != MII_OFFSET_ANY && offloc != offset) {
150 1.15 thorpej offset++;
151 1.11 thorpej continue;
152 1.11 thorpej }
153 1.11 thorpej
154 1.11 thorpej /*
155 1.11 thorpej * Extract the IDs. Braindead PHYs will be handled by
156 1.11 thorpej * the `ukphy' driver, as we have no ID information to
157 1.11 thorpej * match on.
158 1.6 thorpej */
159 1.52 msaitoh ma.mii_id1 = ma.mii_id2 = 0;
160 1.52 msaitoh rv = (*mii->mii_readreg)(parent, ma.mii_phyno,
161 1.52 msaitoh MII_PHYIDR1, &ma.mii_id1);
162 1.52 msaitoh rv |= (*mii->mii_readreg)(parent, ma.mii_phyno,
163 1.52 msaitoh MII_PHYIDR2, &ma.mii_id2);
164 1.52 msaitoh if (rv != 0)
165 1.52 msaitoh continue;
166 1.1 bouyer
167 1.6 thorpej ma.mii_data = mii;
168 1.6 thorpej ma.mii_capmask = capmask;
169 1.29 thorpej ma.mii_flags = flags | (mii->mii_flags & MIIF_INHERIT_MASK);
170 1.1 bouyer
171 1.38 drochner locs[MIICF_PHY] = ma.mii_phyno;
172 1.37 drochner
173 1.55 thorpej mii_unlock(mii);
174 1.55 thorpej
175 1.56 thorpej child = device_private(
176 1.56 thorpej config_found(parent, &ma, mii_print,
177 1.57 thorpej CFARGS(.submatch = config_stdsubmatch,
178 1.57 thorpej .iattr = "mii",
179 1.57 thorpej .locators = locs)));
180 1.37 drochner if (child) {
181 1.53 msaitoh /* Link it up in the parent's MII data. */
182 1.41 ad callout_init(&child->mii_nway_ch, 0);
183 1.55 thorpej mii_lock(mii);
184 1.6 thorpej LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
185 1.13 thorpej child->mii_offset = offset;
186 1.6 thorpej mii->mii_instance++;
187 1.55 thorpej } else {
188 1.55 thorpej mii_lock(mii);
189 1.6 thorpej }
190 1.13 thorpej offset++;
191 1.17 thorpej }
192 1.55 thorpej
193 1.55 thorpej /* All done! */
194 1.55 thorpej mii->mii_flags &= ~MIIF_PROBING;
195 1.55 thorpej cv_signal(&mii->mii_probe_cv);
196 1.55 thorpej mii_unlock(mii);
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.55 thorpej bool exiting = false;
204 1.17 thorpej
205 1.17 thorpej if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
206 1.18 thorpej panic("mii_detach: phyloc and offloc specified");
207 1.17 thorpej
208 1.55 thorpej mii_lock(mii);
209 1.55 thorpej
210 1.55 thorpej if ((mii->mii_flags & MIIF_INITDONE) == 0 ||
211 1.55 thorpej (mii->mii_flags & MIIF_EXITING) != 0) {
212 1.55 thorpej mii_unlock(mii);
213 1.17 thorpej return;
214 1.55 thorpej }
215 1.55 thorpej
216 1.55 thorpej /*
217 1.55 thorpej * XXX This is probably not the best hueristic. Consider
218 1.55 thorpej * XXX adding an argument to mii_detach().
219 1.55 thorpej */
220 1.55 thorpej if (phyloc == MII_PHY_ANY && MII_PHY_ANY == MII_OFFSET_ANY)
221 1.55 thorpej exiting = true;
222 1.55 thorpej
223 1.55 thorpej if (exiting) {
224 1.55 thorpej mii->mii_flags |= MIIF_EXITING;
225 1.55 thorpej cv_broadcast(&mii->mii_probe_cv);
226 1.55 thorpej }
227 1.17 thorpej
228 1.55 thorpej /* Wait for everyone else to get out. */
229 1.55 thorpej mii->mii_probe_waiters++;
230 1.55 thorpej for (;;) {
231 1.55 thorpej /*
232 1.55 thorpej * If we've been waiting to do a less-than-exit, and
233 1.55 thorpej * *someone else* initiated an exit, then get out.
234 1.55 thorpej */
235 1.55 thorpej if (!exiting && (mii->mii_flags & MIIF_EXITING) != 0) {
236 1.55 thorpej mii->mii_probe_waiters--;
237 1.55 thorpej cv_signal(&mii->mii_probe_cv);
238 1.55 thorpej mii_unlock(mii);
239 1.55 thorpej return;
240 1.55 thorpej }
241 1.55 thorpej if ((mii->mii_flags & MIIF_PROBING) == 0 &&
242 1.55 thorpej (!exiting || (exiting && mii->mii_probe_waiters == 1)))
243 1.55 thorpej break;
244 1.55 thorpej cv_wait(&mii->mii_probe_cv, mii->mii_media.ifm_lock);
245 1.55 thorpej }
246 1.55 thorpej mii->mii_probe_waiters--;
247 1.55 thorpej
248 1.55 thorpej if (!exiting)
249 1.55 thorpej mii->mii_flags |= MIIF_PROBING;
250 1.55 thorpej
251 1.55 thorpej LIST_FOREACH_SAFE(child, &mii->mii_phys, mii_list, nchild) {
252 1.17 thorpej if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
253 1.17 thorpej if (phyloc != MII_PHY_ANY &&
254 1.17 thorpej phyloc != child->mii_phy)
255 1.17 thorpej continue;
256 1.17 thorpej if (offloc != MII_OFFSET_ANY &&
257 1.17 thorpej offloc != child->mii_offset)
258 1.17 thorpej continue;
259 1.17 thorpej }
260 1.55 thorpej LIST_REMOVE(child, mii_list);
261 1.55 thorpej mii_unlock(mii);
262 1.47 xtraeme (void)config_detach(child->mii_dev, DETACH_FORCE);
263 1.55 thorpej mii_lock(mii);
264 1.55 thorpej }
265 1.55 thorpej
266 1.55 thorpej if (exiting) {
267 1.55 thorpej cv_destroy(&mii->mii_probe_cv);
268 1.55 thorpej } else {
269 1.55 thorpej mii->mii_flags &= ~MIIF_PROBING;
270 1.55 thorpej cv_signal(&mii->mii_probe_cv);
271 1.6 thorpej }
272 1.55 thorpej
273 1.55 thorpej mii_unlock(mii);
274 1.4 bouyer }
275 1.1 bouyer
276 1.35 thorpej static int
277 1.27 thorpej mii_print(void *aux, const char *pnp)
278 1.1 bouyer {
279 1.6 thorpej struct mii_attach_args *ma = aux;
280 1.2 thorpej
281 1.6 thorpej if (pnp != NULL)
282 1.33 thorpej aprint_normal("OUI 0x%06x model 0x%04x rev %d at %s",
283 1.6 thorpej MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
284 1.6 thorpej MII_REV(ma->mii_id2), pnp);
285 1.6 thorpej
286 1.33 thorpej aprint_normal(" phy %d", ma->mii_phyno);
287 1.53 msaitoh return UNCONF;
288 1.1 bouyer }
289 1.1 bouyer
290 1.43 dyoung static inline int
291 1.43 dyoung phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
292 1.43 dyoung {
293 1.53 msaitoh
294 1.47 xtraeme if (!device_is_active(sc->mii_dev))
295 1.43 dyoung return ENXIO;
296 1.43 dyoung return PHY_SERVICE(sc, mii, cmd);
297 1.43 dyoung }
298 1.43 dyoung
299 1.44 dyoung int
300 1.44 dyoung mii_ifmedia_change(struct mii_data *mii)
301 1.44 dyoung {
302 1.55 thorpej
303 1.55 thorpej KASSERT(mii_locked(mii) ||
304 1.55 thorpej ifmedia_islegacy(&mii->mii_media));
305 1.44 dyoung return ifmedia_change(&mii->mii_media, mii->mii_ifp);
306 1.44 dyoung }
307 1.44 dyoung
308 1.6 thorpej /*
309 1.6 thorpej * Media changed; notify all PHYs.
310 1.6 thorpej */
311 1.6 thorpej int
312 1.27 thorpej mii_mediachg(struct mii_data *mii)
313 1.1 bouyer {
314 1.6 thorpej struct mii_softc *child;
315 1.55 thorpej int rv = 0;
316 1.55 thorpej
317 1.55 thorpej IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
318 1.55 thorpej KASSERT(mii_locked(mii));
319 1.1 bouyer
320 1.6 thorpej mii->mii_media_status = 0;
321 1.6 thorpej mii->mii_media_active = IFM_NONE;
322 1.6 thorpej
323 1.42 dyoung LIST_FOREACH(child, &mii->mii_phys, mii_list) {
324 1.43 dyoung rv = phy_service(child, mii, MII_MEDIACHG);
325 1.6 thorpej if (rv)
326 1.55 thorpej break;
327 1.1 bouyer }
328 1.55 thorpej IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
329 1.55 thorpej return rv;
330 1.1 bouyer }
331 1.1 bouyer
332 1.6 thorpej /*
333 1.6 thorpej * Call the PHY tick routines, used during autonegotiation.
334 1.6 thorpej */
335 1.6 thorpej void
336 1.27 thorpej mii_tick(struct mii_data *mii)
337 1.1 bouyer {
338 1.6 thorpej struct mii_softc *child;
339 1.1 bouyer
340 1.55 thorpej IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
341 1.55 thorpej KASSERT(mii_locked(mii));
342 1.55 thorpej
343 1.42 dyoung LIST_FOREACH(child, &mii->mii_phys, mii_list)
344 1.43 dyoung (void)phy_service(child, mii, MII_TICK);
345 1.55 thorpej
346 1.55 thorpej IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
347 1.1 bouyer }
348 1.1 bouyer
349 1.6 thorpej /*
350 1.6 thorpej * Get media status from PHYs.
351 1.6 thorpej */
352 1.2 thorpej void
353 1.27 thorpej mii_pollstat(struct mii_data *mii)
354 1.1 bouyer {
355 1.6 thorpej struct mii_softc *child;
356 1.1 bouyer
357 1.55 thorpej IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
358 1.55 thorpej KASSERT(mii_locked(mii));
359 1.55 thorpej
360 1.6 thorpej mii->mii_media_status = 0;
361 1.6 thorpej mii->mii_media_active = IFM_NONE;
362 1.1 bouyer
363 1.42 dyoung LIST_FOREACH(child, &mii->mii_phys, mii_list)
364 1.43 dyoung (void)phy_service(child, mii, MII_POLLSTAT);
365 1.55 thorpej
366 1.55 thorpej IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
367 1.16 thorpej }
368 1.16 thorpej
369 1.16 thorpej /*
370 1.16 thorpej * Inform the PHYs that the interface is down.
371 1.16 thorpej */
372 1.16 thorpej void
373 1.27 thorpej mii_down(struct mii_data *mii)
374 1.16 thorpej {
375 1.16 thorpej struct mii_softc *child;
376 1.16 thorpej
377 1.55 thorpej IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
378 1.55 thorpej KASSERT(mii_locked(mii));
379 1.55 thorpej
380 1.42 dyoung LIST_FOREACH(child, &mii->mii_phys, mii_list)
381 1.43 dyoung (void)phy_service(child, mii, MII_DOWN);
382 1.55 thorpej
383 1.55 thorpej IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
384 1.23 drochner }
385 1.23 drochner
386 1.23 drochner static unsigned char
387 1.23 drochner bitreverse(unsigned char x)
388 1.23 drochner {
389 1.51 matt static const unsigned char nibbletab[16] = {
390 1.23 drochner 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
391 1.23 drochner };
392 1.23 drochner
393 1.23 drochner return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
394 1.23 drochner }
395 1.23 drochner
396 1.32 thorpej u_int
397 1.54 msaitoh mii_oui(uint16_t id1, uint16_t id2)
398 1.23 drochner {
399 1.32 thorpej u_int h;
400 1.23 drochner
401 1.23 drochner h = (id1 << 6) | (id2 >> 10);
402 1.23 drochner
403 1.23 drochner return ((bitreverse(h >> 16) << 16) |
404 1.23 drochner (bitreverse((h >> 8) & 255) << 8) |
405 1.23 drochner bitreverse(h & 255));
406 1.1 bouyer }
407