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