1 /* $NetBSD: if_media.c,v 1.25 2005/12/11 12:24:51 christos 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 * Copyright (c) 1997 42 * Jonathan Stone and Jason R. Thorpe. All rights reserved. 43 * 44 * This software is derived from information provided by Matt Thomas. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. All advertising materials mentioning features or use of this software 55 * must display the following acknowledgement: 56 * This product includes software developed by Jonathan Stone 57 * and Jason R. Thorpe for the NetBSD Project. 58 * 4. The names of the authors may not be used to endorse or promote products 59 * derived from this software without specific prior written permission. 60 * 61 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 62 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 63 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 64 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 65 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 66 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 67 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 68 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 69 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 71 * SUCH DAMAGE. 72 */ 73 74 /* 75 * BSD/OS-compatible network interface media selection. 76 * 77 * Where it is safe to do so, this code strays slightly from the BSD/OS 78 * design. Software which uses the API (device drivers, basically) 79 * shouldn't notice any difference. 80 * 81 * Many thanks to Matt Thomas for providing the information necessary 82 * to implement this interface. 83 */ 84 85 #include <sys/cdefs.h> 86 __KERNEL_RCSID(0, "$NetBSD: if_media.c,v 1.25 2005/12/11 12:24:51 christos Exp $"); 87 88 #include <sys/param.h> 89 #include <sys/systm.h> 90 #include <sys/errno.h> 91 #include <sys/ioctl.h> 92 #include <sys/socket.h> 93 #include <sys/malloc.h> 94 95 #include <net/if.h> 96 #include <net/if_media.h> 97 #include <net/netisr.h> 98 99 /* 100 * Compile-time options: 101 * IFMEDIA_DEBUG: 102 * turn on implementation-level debug printfs. 103 * Useful for debugging newly-ported drivers. 104 */ 105 106 #ifdef IFMEDIA_DEBUG 107 int ifmedia_debug = 0; 108 static void ifmedia_printword(int); 109 #endif 110 111 MALLOC_DEFINE(M_IFMEDIA, "ifmedia", "interface media state"); 112 113 /* 114 * Initialize if_media struct for a specific interface instance. 115 */ 116 void 117 ifmedia_init(struct ifmedia *ifm, int dontcare_mask, 118 ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback) 119 { 120 121 TAILQ_INIT(&ifm->ifm_list); 122 ifm->ifm_cur = NULL; 123 ifm->ifm_media = 0; 124 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */ 125 ifm->ifm_change = change_callback; 126 ifm->ifm_status = status_callback; 127 } 128 129 /* 130 * Add a media configuration to the list of supported media 131 * for a specific interface instance. 132 */ 133 void 134 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux) 135 { 136 struct ifmedia_entry *entry; 137 138 #ifdef IFMEDIA_DEBUG 139 if (ifmedia_debug) { 140 if (ifm == NULL) { 141 printf("ifmedia_add: null ifm\n"); 142 return; 143 } 144 printf("Adding entry for "); 145 ifmedia_printword(mword); 146 } 147 #endif 148 149 entry = malloc(sizeof(*entry), M_IFMEDIA, M_NOWAIT); 150 if (entry == NULL) 151 panic("ifmedia_add: can't malloc entry"); 152 153 entry->ifm_media = mword; 154 entry->ifm_data = data; 155 entry->ifm_aux = aux; 156 157 TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list); 158 } 159 160 /* 161 * Add an array of media configurations to the list of 162 * supported media for a specific interface instance. 163 */ 164 void 165 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count) 166 { 167 int i; 168 169 for (i = 0; i < count; i++) 170 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data, 171 lp[i].ifm_aux); 172 } 173 174 /* 175 * Set the default active media. 176 * 177 * Called by device-specific code which is assumed to have already 178 * selected the default media in hardware. We do _not_ call the 179 * media-change callback. 180 */ 181 void 182 ifmedia_set(struct ifmedia *ifm, int target) 183 { 184 struct ifmedia_entry *match; 185 186 match = ifmedia_match(ifm, target, ifm->ifm_mask); 187 188 /* 189 * If we didn't find the requested media, then we try to fall 190 * back to target-type (IFM_ETHER, e.g.) | IFM_NONE. If that's 191 * not on the list, then we add it and set the media to it. 192 * 193 * Since ifmedia_set is almost always called with IFM_AUTO or 194 * with a known-good media, this really should only occur if we: 195 * 196 * a) didn't find any PHYs, or 197 * b) didn't find an autoselect option on the PHY when the 198 * parent ethernet driver expected to. 199 * 200 * In either case, it makes sense to select no media. 201 */ 202 if (match == NULL) { 203 printf("ifmedia_set: no match for 0x%x/0x%x\n", 204 target, ~ifm->ifm_mask); 205 target = (target & IFM_NMASK) | IFM_NONE; 206 match = ifmedia_match(ifm, target, ifm->ifm_mask); 207 if (match == NULL) { 208 ifmedia_add(ifm, target, 0, NULL); 209 match = ifmedia_match(ifm, target, ifm->ifm_mask); 210 if (match == NULL) { 211 panic("ifmedia_set failed"); 212 } 213 } 214 } 215 ifm->ifm_cur = match; 216 217 #ifdef IFMEDIA_DEBUG 218 if (ifmedia_debug) { 219 printf("ifmedia_set: target "); 220 ifmedia_printword(target); 221 printf("ifmedia_set: setting to "); 222 ifmedia_printword(ifm->ifm_cur->ifm_media); 223 } 224 #endif 225 } 226 227 /* 228 * Device-independent media ioctl support function. 229 */ 230 int 231 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm, 232 u_long cmd) 233 { 234 struct ifmedia_entry *match; 235 struct ifmediareq *ifmr = (struct ifmediareq *) ifr; 236 int error = 0; 237 238 if (ifp == NULL || ifr == NULL || ifm == NULL) 239 return (EINVAL); 240 241 switch (cmd) { 242 243 /* 244 * Set the current media. 245 */ 246 case SIOCSIFMEDIA: 247 { 248 struct ifmedia_entry *oldentry; 249 u_int oldmedia; 250 u_int newmedia = ifr->ifr_media; 251 252 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask); 253 if (match == NULL) { 254 #ifdef IFMEDIA_DEBUG 255 if (ifmedia_debug) { 256 printf( 257 "ifmedia_ioctl: no media found for 0x%x\n", 258 newmedia); 259 } 260 #endif 261 return (EINVAL); 262 } 263 264 /* 265 * If no change, we're done. 266 * XXX Automedia may involve software intervention. 267 * Keep going in case the connected media changed. 268 * Similarly, if best match changed (kernel debugger?). 269 */ 270 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) && 271 (newmedia == ifm->ifm_media) && 272 (match == ifm->ifm_cur)) 273 return 0; 274 275 /* 276 * We found a match, now make the driver switch to it. 277 * Make sure to preserve our old media type in case the 278 * driver can't switch. 279 */ 280 #ifdef IFMEDIA_DEBUG 281 if (ifmedia_debug) { 282 printf("ifmedia_ioctl: switching %s to ", 283 ifp->if_xname); 284 ifmedia_printword(match->ifm_media); 285 } 286 #endif 287 oldentry = ifm->ifm_cur; 288 oldmedia = ifm->ifm_media; 289 ifm->ifm_cur = match; 290 ifm->ifm_media = newmedia; 291 error = (*ifm->ifm_change)(ifp); 292 if (error) { 293 ifm->ifm_cur = oldentry; 294 ifm->ifm_media = oldmedia; 295 } 296 break; 297 } 298 299 /* 300 * Get list of available media and current media on interface. 301 */ 302 case SIOCGIFMEDIA: 303 { 304 struct ifmedia_entry *ep; 305 size_t nwords; 306 307 if (ifmr->ifm_count < 0) 308 return EINVAL; 309 310 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ? 311 ifm->ifm_cur->ifm_media : IFM_NONE; 312 ifmr->ifm_mask = ifm->ifm_mask; 313 ifmr->ifm_status = 0; 314 (*ifm->ifm_status)(ifp, ifmr); 315 316 /* 317 * Count them so we know a-priori how much is the max we'll 318 * need. 319 */ 320 ep = TAILQ_FIRST(&ifm->ifm_list); 321 for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list)) 322 nwords++; 323 324 if (ifmr->ifm_count != 0) { 325 size_t count; 326 size_t minwords = nwords > (size_t)ifmr->ifm_count 327 ? (size_t)ifmr->ifm_count 328 : nwords; 329 int *kptr = (int *)malloc(minwords * sizeof(int), 330 M_TEMP, M_WAITOK); 331 /* 332 * Get the media words from the interface's list. 333 */ 334 ep = TAILQ_FIRST(&ifm->ifm_list); 335 for (count = 0; ep != NULL && count < minwords; 336 ep = TAILQ_NEXT(ep, ifm_list), count++) 337 kptr[count] = ep->ifm_media; 338 339 error = copyout(kptr, ifmr->ifm_ulist, 340 minwords * sizeof(int)); 341 if (error == 0 && ep != NULL) 342 error = E2BIG; /* oops! */ 343 free(kptr, M_TEMP); 344 } 345 ifmr->ifm_count = nwords; 346 break; 347 } 348 349 default: 350 return (EINVAL); 351 } 352 353 return (error); 354 } 355 356 /* 357 * Find media entry matching a given ifm word. 358 */ 359 struct ifmedia_entry * 360 ifmedia_match(struct ifmedia *ifm, u_int target, u_int mask) 361 { 362 struct ifmedia_entry *match, *next; 363 364 match = NULL; 365 mask = ~mask; 366 367 for (next = TAILQ_FIRST(&ifm->ifm_list); next != NULL; 368 next = TAILQ_NEXT(next, ifm_list)) { 369 if ((next->ifm_media & mask) == (target & mask)) { 370 if (match) { 371 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC) 372 printf("ifmedia_match: multiple match for " 373 "0x%x/0x%x, selected instance %d\n", 374 target, mask, IFM_INST(match->ifm_media)); 375 #endif 376 break; 377 } 378 match = next; 379 } 380 } 381 382 return match; 383 } 384 385 /* 386 * Delete all media for a given instance. 387 */ 388 void 389 ifmedia_delete_instance(struct ifmedia *ifm, u_int inst) 390 { 391 struct ifmedia_entry *ife, *nife; 392 393 for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL; 394 ife = nife) { 395 nife = TAILQ_NEXT(ife, ifm_list); 396 if (inst == IFM_INST_ANY || 397 inst == IFM_INST(ife->ifm_media)) { 398 TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list); 399 free(ife, M_IFMEDIA); 400 } 401 } 402 } 403 404 /* 405 * Compute the interface `baudrate' from the media, for the interface 406 * metrics (used by routing daemons). 407 */ 408 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] = 409 IFM_BAUDRATE_DESCRIPTIONS; 410 411 u_quad_t 412 ifmedia_baudrate(int mword) 413 { 414 int i; 415 416 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) { 417 if ((mword & (IFM_NMASK|IFM_TMASK)) == 418 ifmedia_baudrate_descriptions[i].ifmb_word) 419 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate); 420 } 421 422 /* Not known. */ 423 return (0); 424 } 425 426 #ifdef IFMEDIA_DEBUG 427 428 static const struct ifmedia_description ifm_type_descriptions[] = 429 IFM_TYPE_DESCRIPTIONS; 430 431 static const struct ifmedia_description ifm_subtype_descriptions[] = 432 IFM_SUBTYPE_DESCRIPTIONS; 433 434 static const struct ifmedia_description ifm_option_descriptions[] = 435 IFM_OPTION_DESCRIPTIONS; 436 437 /* 438 * print a media word. 439 */ 440 static void 441 ifmedia_printword(int ifmw) 442 { 443 const struct ifmedia_description *desc; 444 int seen_option = 0; 445 446 /* Print the top-level interface type. */ 447 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; 448 desc++) { 449 if (IFM_TYPE(ifmw) == desc->ifmt_word) 450 break; 451 } 452 if (desc->ifmt_string == NULL) 453 printf("<unknown type> "); 454 else 455 printf("%s ", desc->ifmt_string); 456 457 /* Print the subtype. */ 458 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; 459 desc++) { 460 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) && 461 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw)) 462 break; 463 } 464 if (desc->ifmt_string == NULL) 465 printf("<unknown subtype>"); 466 else 467 printf("%s", desc->ifmt_string); 468 469 /* Print any options. */ 470 for (desc = ifm_option_descriptions; desc->ifmt_string != NULL; 471 desc++) { 472 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) && 473 (ifmw & desc->ifmt_word) != 0 && 474 (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) { 475 if (seen_option == 0) 476 printf(" <"); 477 printf("%s%s", seen_option ? "," : "", 478 desc->ifmt_string); 479 seen_option |= IFM_OPTIONS(desc->ifmt_word); 480 } 481 } 482 printf("%s\n", seen_option ? ">" : ""); 483 } 484 485 #endif /* IFMEDIA_DEBUG */ 486