1 /* $NetBSD: mii.c,v 1.55 2020/03/15 23:04:50 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.55 2020/03/15 23:04:50 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(config_found_sm_loc(parent, "mii", 176 locs, &ma, mii_print, config_stdsubmatch)); 177 if (child) { 178 /* Link it up in the parent's MII data. */ 179 callout_init(&child->mii_nway_ch, 0); 180 mii_lock(mii); 181 LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list); 182 child->mii_offset = offset; 183 mii->mii_instance++; 184 } else { 185 mii_lock(mii); 186 } 187 offset++; 188 } 189 190 /* All done! */ 191 mii->mii_flags &= ~MIIF_PROBING; 192 cv_signal(&mii->mii_probe_cv); 193 mii_unlock(mii); 194 } 195 196 void 197 mii_detach(struct mii_data *mii, int phyloc, int offloc) 198 { 199 struct mii_softc *child, *nchild; 200 bool exiting = false; 201 202 if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY) 203 panic("mii_detach: phyloc and offloc specified"); 204 205 mii_lock(mii); 206 207 if ((mii->mii_flags & MIIF_INITDONE) == 0 || 208 (mii->mii_flags & MIIF_EXITING) != 0) { 209 mii_unlock(mii); 210 return; 211 } 212 213 /* 214 * XXX This is probably not the best hueristic. Consider 215 * XXX adding an argument to mii_detach(). 216 */ 217 if (phyloc == MII_PHY_ANY && MII_PHY_ANY == MII_OFFSET_ANY) 218 exiting = true; 219 220 if (exiting) { 221 mii->mii_flags |= MIIF_EXITING; 222 cv_broadcast(&mii->mii_probe_cv); 223 } 224 225 /* Wait for everyone else to get out. */ 226 mii->mii_probe_waiters++; 227 for (;;) { 228 /* 229 * If we've been waiting to do a less-than-exit, and 230 * *someone else* initiated an exit, then get out. 231 */ 232 if (!exiting && (mii->mii_flags & MIIF_EXITING) != 0) { 233 mii->mii_probe_waiters--; 234 cv_signal(&mii->mii_probe_cv); 235 mii_unlock(mii); 236 return; 237 } 238 if ((mii->mii_flags & MIIF_PROBING) == 0 && 239 (!exiting || (exiting && mii->mii_probe_waiters == 1))) 240 break; 241 cv_wait(&mii->mii_probe_cv, mii->mii_media.ifm_lock); 242 } 243 mii->mii_probe_waiters--; 244 245 if (!exiting) 246 mii->mii_flags |= MIIF_PROBING; 247 248 LIST_FOREACH_SAFE(child, &mii->mii_phys, mii_list, nchild) { 249 if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) { 250 if (phyloc != MII_PHY_ANY && 251 phyloc != child->mii_phy) 252 continue; 253 if (offloc != MII_OFFSET_ANY && 254 offloc != child->mii_offset) 255 continue; 256 } 257 LIST_REMOVE(child, mii_list); 258 mii_unlock(mii); 259 (void)config_detach(child->mii_dev, DETACH_FORCE); 260 mii_lock(mii); 261 } 262 263 if (exiting) { 264 cv_destroy(&mii->mii_probe_cv); 265 } else { 266 mii->mii_flags &= ~MIIF_PROBING; 267 cv_signal(&mii->mii_probe_cv); 268 } 269 270 mii_unlock(mii); 271 } 272 273 static int 274 mii_print(void *aux, const char *pnp) 275 { 276 struct mii_attach_args *ma = aux; 277 278 if (pnp != NULL) 279 aprint_normal("OUI 0x%06x model 0x%04x rev %d at %s", 280 MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2), 281 MII_REV(ma->mii_id2), pnp); 282 283 aprint_normal(" phy %d", ma->mii_phyno); 284 return UNCONF; 285 } 286 287 static inline int 288 phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 289 { 290 291 if (!device_is_active(sc->mii_dev)) 292 return ENXIO; 293 return PHY_SERVICE(sc, mii, cmd); 294 } 295 296 int 297 mii_ifmedia_change(struct mii_data *mii) 298 { 299 300 KASSERT(mii_locked(mii) || 301 ifmedia_islegacy(&mii->mii_media)); 302 return ifmedia_change(&mii->mii_media, mii->mii_ifp); 303 } 304 305 /* 306 * Media changed; notify all PHYs. 307 */ 308 int 309 mii_mediachg(struct mii_data *mii) 310 { 311 struct mii_softc *child; 312 int rv = 0; 313 314 IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media); 315 KASSERT(mii_locked(mii)); 316 317 mii->mii_media_status = 0; 318 mii->mii_media_active = IFM_NONE; 319 320 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 321 rv = phy_service(child, mii, MII_MEDIACHG); 322 if (rv) 323 break; 324 } 325 IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media); 326 return rv; 327 } 328 329 /* 330 * Call the PHY tick routines, used during autonegotiation. 331 */ 332 void 333 mii_tick(struct mii_data *mii) 334 { 335 struct mii_softc *child; 336 337 IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media); 338 KASSERT(mii_locked(mii)); 339 340 LIST_FOREACH(child, &mii->mii_phys, mii_list) 341 (void)phy_service(child, mii, MII_TICK); 342 343 IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media); 344 } 345 346 /* 347 * Get media status from PHYs. 348 */ 349 void 350 mii_pollstat(struct mii_data *mii) 351 { 352 struct mii_softc *child; 353 354 IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media); 355 KASSERT(mii_locked(mii)); 356 357 mii->mii_media_status = 0; 358 mii->mii_media_active = IFM_NONE; 359 360 LIST_FOREACH(child, &mii->mii_phys, mii_list) 361 (void)phy_service(child, mii, MII_POLLSTAT); 362 363 IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media); 364 } 365 366 /* 367 * Inform the PHYs that the interface is down. 368 */ 369 void 370 mii_down(struct mii_data *mii) 371 { 372 struct mii_softc *child; 373 374 IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media); 375 KASSERT(mii_locked(mii)); 376 377 LIST_FOREACH(child, &mii->mii_phys, mii_list) 378 (void)phy_service(child, mii, MII_DOWN); 379 380 IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media); 381 } 382 383 static unsigned char 384 bitreverse(unsigned char x) 385 { 386 static const unsigned char nibbletab[16] = { 387 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 388 }; 389 390 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]); 391 } 392 393 u_int 394 mii_oui(uint16_t id1, uint16_t id2) 395 { 396 u_int h; 397 398 h = (id1 << 6) | (id2 >> 10); 399 400 return ((bitreverse(h >> 16) << 16) | 401 (bitreverse((h >> 8) & 255) << 8) | 402 bitreverse(h & 255)); 403 } 404