1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/conf.h> 29 #include <sys/id_space.h> 30 #include <sys/esunddi.h> 31 #include <sys/stat.h> 32 #include <sys/mkdev.h> 33 #include <sys/stream.h> 34 #include <sys/strsubr.h> 35 #include <sys/dlpi.h> 36 #include <sys/modhash.h> 37 #include <sys/mac.h> 38 #include <sys/mac_provider.h> 39 #include <sys/mac_impl.h> 40 #include <sys/mac_client_impl.h> 41 #include <sys/mac_client_priv.h> 42 #include <sys/mac_soft_ring.h> 43 #include <sys/modctl.h> 44 #include <sys/fs/dv_node.h> 45 #include <sys/thread.h> 46 #include <sys/proc.h> 47 #include <sys/callb.h> 48 #include <sys/cpuvar.h> 49 #include <sys/atomic.h> 50 #include <sys/sdt.h> 51 #include <sys/mac_flow.h> 52 #include <sys/ddi_intr_impl.h> 53 #include <sys/disp.h> 54 #include <sys/sdt.h> 55 56 /* 57 * MAC Provider Interface. 58 * 59 * Interface for GLDv3 compatible NIC drivers. 60 */ 61 62 static void i_mac_notify_thread(void *); 63 64 typedef void (*mac_notify_default_cb_fn_t)(mac_impl_t *); 65 66 static const mac_notify_default_cb_fn_t mac_notify_cb_list[MAC_NNOTE] = { 67 mac_fanout_recompute, /* MAC_NOTE_LINK */ 68 NULL, /* MAC_NOTE_UNICST */ 69 NULL, /* MAC_NOTE_TX */ 70 NULL, /* MAC_NOTE_DEVPROMISC */ 71 NULL, /* MAC_NOTE_FASTPATH_FLUSH */ 72 NULL, /* MAC_NOTE_SDU_SIZE */ 73 NULL, /* MAC_NOTE_MARGIN */ 74 NULL, /* MAC_NOTE_CAPAB_CHG */ 75 NULL /* MAC_NOTE_LOWLINK */ 76 }; 77 78 /* 79 * Driver support functions. 80 */ 81 82 /* REGISTRATION */ 83 84 mac_register_t * 85 mac_alloc(uint_t mac_version) 86 { 87 mac_register_t *mregp; 88 89 /* 90 * Make sure there isn't a version mismatch between the driver and 91 * the framework. In the future, if multiple versions are 92 * supported, this check could become more sophisticated. 93 */ 94 if (mac_version != MAC_VERSION) 95 return (NULL); 96 97 mregp = kmem_zalloc(sizeof (mac_register_t), KM_SLEEP); 98 mregp->m_version = mac_version; 99 return (mregp); 100 } 101 102 void 103 mac_free(mac_register_t *mregp) 104 { 105 kmem_free(mregp, sizeof (mac_register_t)); 106 } 107 108 /* 109 * mac_register() is how drivers register new MACs with the GLDv3 110 * framework. The mregp argument is allocated by drivers using the 111 * mac_alloc() function, and can be freed using mac_free() immediately upon 112 * return from mac_register(). Upon success (0 return value), the mhp 113 * opaque pointer becomes the driver's handle to its MAC interface, and is 114 * the argument to all other mac module entry points. 115 */ 116 /* ARGSUSED */ 117 int 118 mac_register(mac_register_t *mregp, mac_handle_t *mhp) 119 { 120 mac_impl_t *mip; 121 mactype_t *mtype; 122 int err = EINVAL; 123 struct devnames *dnp = NULL; 124 uint_t instance; 125 boolean_t style1_created = B_FALSE; 126 boolean_t style2_created = B_FALSE; 127 char *driver; 128 minor_t minor = 0; 129 130 /* Find the required MAC-Type plugin. */ 131 if ((mtype = mactype_getplugin(mregp->m_type_ident)) == NULL) 132 return (EINVAL); 133 134 /* Create a mac_impl_t to represent this MAC. */ 135 mip = kmem_cache_alloc(i_mac_impl_cachep, KM_SLEEP); 136 137 /* 138 * The mac is not ready for open yet. 139 */ 140 mip->mi_state_flags |= MIS_DISABLED; 141 142 /* 143 * When a mac is registered, the m_instance field can be set to: 144 * 145 * 0: Get the mac's instance number from m_dip. 146 * This is usually used for physical device dips. 147 * 148 * [1 .. MAC_MAX_MINOR-1]: Use the value as the mac's instance number. 149 * For example, when an aggregation is created with the key option, 150 * "key" will be used as the instance number. 151 * 152 * -1: Assign an instance number from [MAC_MAX_MINOR .. MAXMIN-1]. 153 * This is often used when a MAC of a virtual link is registered 154 * (e.g., aggregation when "key" is not specified, or vnic). 155 * 156 * Note that the instance number is used to derive the mi_minor field 157 * of mac_impl_t, which will then be used to derive the name of kstats 158 * and the devfs nodes. The first 2 cases are needed to preserve 159 * backward compatibility. 160 */ 161 switch (mregp->m_instance) { 162 case 0: 163 instance = ddi_get_instance(mregp->m_dip); 164 break; 165 case ((uint_t)-1): 166 minor = mac_minor_hold(B_TRUE); 167 if (minor == 0) { 168 err = ENOSPC; 169 goto fail; 170 } 171 instance = minor - 1; 172 break; 173 default: 174 instance = mregp->m_instance; 175 if (instance >= MAC_MAX_MINOR) { 176 err = EINVAL; 177 goto fail; 178 } 179 break; 180 } 181 182 mip->mi_minor = (minor_t)(instance + 1); 183 mip->mi_dip = mregp->m_dip; 184 mip->mi_clients_list = NULL; 185 mip->mi_nclients = 0; 186 187 /* Set the default IEEE Port VLAN Identifier */ 188 mip->mi_pvid = 1; 189 190 /* Default bridge link learning protection values */ 191 mip->mi_llimit = 1000; 192 mip->mi_ldecay = 200; 193 194 driver = (char *)ddi_driver_name(mip->mi_dip); 195 196 /* Construct the MAC name as <drvname><instance> */ 197 (void) snprintf(mip->mi_name, sizeof (mip->mi_name), "%s%d", 198 driver, instance); 199 200 mip->mi_driver = mregp->m_driver; 201 202 mip->mi_type = mtype; 203 mip->mi_margin = mregp->m_margin; 204 mip->mi_info.mi_media = mtype->mt_type; 205 mip->mi_info.mi_nativemedia = mtype->mt_nativetype; 206 if (mregp->m_max_sdu <= mregp->m_min_sdu) 207 goto fail; 208 mip->mi_sdu_min = mregp->m_min_sdu; 209 mip->mi_sdu_max = mregp->m_max_sdu; 210 mip->mi_info.mi_addr_length = mip->mi_type->mt_addr_length; 211 /* 212 * If the media supports a broadcast address, cache a pointer to it 213 * in the mac_info_t so that upper layers can use it. 214 */ 215 mip->mi_info.mi_brdcst_addr = mip->mi_type->mt_brdcst_addr; 216 217 mip->mi_v12n_level = mregp->m_v12n; 218 219 /* 220 * Copy the unicast source address into the mac_info_t, but only if 221 * the MAC-Type defines a non-zero address length. We need to 222 * handle MAC-Types that have an address length of 0 223 * (point-to-point protocol MACs for example). 224 */ 225 if (mip->mi_type->mt_addr_length > 0) { 226 if (mregp->m_src_addr == NULL) 227 goto fail; 228 mip->mi_info.mi_unicst_addr = 229 kmem_alloc(mip->mi_type->mt_addr_length, KM_SLEEP); 230 bcopy(mregp->m_src_addr, mip->mi_info.mi_unicst_addr, 231 mip->mi_type->mt_addr_length); 232 233 /* 234 * Copy the fixed 'factory' MAC address from the immutable 235 * info. This is taken to be the MAC address currently in 236 * use. 237 */ 238 bcopy(mip->mi_info.mi_unicst_addr, mip->mi_addr, 239 mip->mi_type->mt_addr_length); 240 241 /* 242 * At this point, we should set up the classification 243 * rules etc but we delay it till mac_open() so that 244 * the resource discovery has taken place and we 245 * know someone wants to use the device. Otherwise 246 * memory gets allocated for Rx ring structures even 247 * during probe. 248 */ 249 250 /* Copy the destination address if one is provided. */ 251 if (mregp->m_dst_addr != NULL) { 252 bcopy(mregp->m_dst_addr, mip->mi_dstaddr, 253 mip->mi_type->mt_addr_length); 254 } 255 } else if (mregp->m_src_addr != NULL) { 256 goto fail; 257 } 258 259 /* 260 * The format of the m_pdata is specific to the plugin. It is 261 * passed in as an argument to all of the plugin callbacks. The 262 * driver can update this information by calling 263 * mac_pdata_update(). 264 */ 265 if (mregp->m_pdata != NULL) { 266 /* 267 * Verify that the plugin supports MAC plugin data and that 268 * the supplied data is valid. 269 */ 270 if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY)) 271 goto fail; 272 if (!mip->mi_type->mt_ops.mtops_pdata_verify(mregp->m_pdata, 273 mregp->m_pdata_size)) { 274 goto fail; 275 } 276 mip->mi_pdata = kmem_alloc(mregp->m_pdata_size, KM_SLEEP); 277 bcopy(mregp->m_pdata, mip->mi_pdata, mregp->m_pdata_size); 278 mip->mi_pdata_size = mregp->m_pdata_size; 279 } 280 281 /* 282 * Register the private properties. 283 */ 284 mac_register_priv_prop(mip, mregp->m_priv_props, 285 mregp->m_priv_prop_count); 286 287 /* 288 * Stash the driver callbacks into the mac_impl_t, but first sanity 289 * check to make sure all mandatory callbacks are set. 290 */ 291 if (mregp->m_callbacks->mc_getstat == NULL || 292 mregp->m_callbacks->mc_start == NULL || 293 mregp->m_callbacks->mc_stop == NULL || 294 mregp->m_callbacks->mc_setpromisc == NULL || 295 mregp->m_callbacks->mc_multicst == NULL) { 296 goto fail; 297 } 298 mip->mi_callbacks = mregp->m_callbacks; 299 300 if (mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LEGACY, 301 &mip->mi_capab_legacy)) { 302 mip->mi_state_flags |= MIS_LEGACY; 303 mip->mi_phy_dev = mip->mi_capab_legacy.ml_dev; 304 } else { 305 mip->mi_phy_dev = makedevice(ddi_driver_major(mip->mi_dip), 306 ddi_get_instance(mip->mi_dip) + 1); 307 } 308 309 /* 310 * Allocate a notification thread. thread_create blocks for memory 311 * if needed, it never fails. 312 */ 313 mip->mi_notify_thread = thread_create(NULL, 0, i_mac_notify_thread, 314 mip, 0, &p0, TS_RUN, minclsyspri); 315 316 /* 317 * Initialize the capabilities 318 */ 319 320 if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, NULL)) 321 mip->mi_state_flags |= MIS_IS_VNIC; 322 323 if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR, NULL)) 324 mip->mi_state_flags |= MIS_IS_AGGR; 325 326 mac_addr_factory_init(mip); 327 328 /* 329 * Enforce the virtrualization level registered. 330 */ 331 if (mip->mi_v12n_level & MAC_VIRT_LEVEL1) { 332 if (mac_init_rings(mip, MAC_RING_TYPE_RX) != 0 || 333 mac_init_rings(mip, MAC_RING_TYPE_TX) != 0) 334 goto fail; 335 336 /* 337 * The driver needs to register at least rx rings for this 338 * virtualization level. 339 */ 340 if (mip->mi_rx_groups == NULL) 341 goto fail; 342 } 343 344 /* 345 * The driver must set mc_unicst entry point to NULL when it advertises 346 * CAP_RINGS for rx groups. 347 */ 348 if (mip->mi_rx_groups != NULL) { 349 if (mregp->m_callbacks->mc_unicst != NULL) 350 goto fail; 351 } else { 352 if (mregp->m_callbacks->mc_unicst == NULL) 353 goto fail; 354 } 355 356 /* 357 * The driver must set mc_tx entry point to NULL when it advertises 358 * CAP_RINGS for tx rings. 359 */ 360 if (mip->mi_tx_groups != NULL) { 361 if (mregp->m_callbacks->mc_tx != NULL) 362 goto fail; 363 } else { 364 if (mregp->m_callbacks->mc_tx == NULL) 365 goto fail; 366 } 367 368 /* 369 * Initialize MAC addresses. Must be called after mac_init_rings(). 370 */ 371 mac_init_macaddr(mip); 372 373 mip->mi_share_capab.ms_snum = 0; 374 if (mip->mi_v12n_level & MAC_VIRT_HIO) { 375 (void) mac_capab_get((mac_handle_t)mip, MAC_CAPAB_SHARES, 376 &mip->mi_share_capab); 377 } 378 379 /* 380 * Initialize the kstats for this device. 381 */ 382 mac_stat_create(mip); 383 384 /* Zero out any properties. */ 385 bzero(&mip->mi_resource_props, sizeof (mac_resource_props_t)); 386 387 /* set the gldv3 flag in dn_flags */ 388 dnp = &devnamesp[ddi_driver_major(mip->mi_dip)]; 389 LOCK_DEV_OPS(&dnp->dn_lock); 390 dnp->dn_flags |= (DN_GLDV3_DRIVER | DN_NETWORK_DRIVER); 391 UNLOCK_DEV_OPS(&dnp->dn_lock); 392 393 if (mip->mi_minor < MAC_MAX_MINOR + 1) { 394 /* Create a style-2 DLPI device */ 395 if (ddi_create_minor_node(mip->mi_dip, driver, S_IFCHR, 0, 396 DDI_NT_NET, CLONE_DEV) != DDI_SUCCESS) 397 goto fail; 398 style2_created = B_TRUE; 399 400 /* Create a style-1 DLPI device */ 401 if (ddi_create_minor_node(mip->mi_dip, mip->mi_name, S_IFCHR, 402 mip->mi_minor, DDI_NT_NET, 0) != DDI_SUCCESS) 403 goto fail; 404 style1_created = B_TRUE; 405 } 406 407 mac_flow_l2tab_create(mip, &mip->mi_flow_tab); 408 409 rw_enter(&i_mac_impl_lock, RW_WRITER); 410 if (mod_hash_insert(i_mac_impl_hash, 411 (mod_hash_key_t)mip->mi_name, (mod_hash_val_t)mip) != 0) { 412 rw_exit(&i_mac_impl_lock); 413 err = EEXIST; 414 goto fail; 415 } 416 417 DTRACE_PROBE2(mac__register, struct devnames *, dnp, 418 (mac_impl_t *), mip); 419 420 /* 421 * Mark the MAC to be ready for open. 422 */ 423 mip->mi_state_flags &= ~MIS_DISABLED; 424 rw_exit(&i_mac_impl_lock); 425 426 atomic_inc_32(&i_mac_impl_count); 427 428 cmn_err(CE_NOTE, "!%s registered", mip->mi_name); 429 *mhp = (mac_handle_t)mip; 430 return (0); 431 432 fail: 433 if (style1_created) 434 ddi_remove_minor_node(mip->mi_dip, mip->mi_name); 435 436 if (style2_created) 437 ddi_remove_minor_node(mip->mi_dip, driver); 438 439 mac_addr_factory_fini(mip); 440 441 /* Clean up registered MAC addresses */ 442 mac_fini_macaddr(mip); 443 444 /* Clean up registered rings */ 445 mac_free_rings(mip, MAC_RING_TYPE_RX); 446 mac_free_rings(mip, MAC_RING_TYPE_TX); 447 448 /* Clean up notification thread */ 449 if (mip->mi_notify_thread != NULL) 450 i_mac_notify_exit(mip); 451 452 if (mip->mi_info.mi_unicst_addr != NULL) { 453 kmem_free(mip->mi_info.mi_unicst_addr, 454 mip->mi_type->mt_addr_length); 455 mip->mi_info.mi_unicst_addr = NULL; 456 } 457 458 mac_stat_destroy(mip); 459 460 if (mip->mi_type != NULL) { 461 atomic_dec_32(&mip->mi_type->mt_ref); 462 mip->mi_type = NULL; 463 } 464 465 if (mip->mi_pdata != NULL) { 466 kmem_free(mip->mi_pdata, mip->mi_pdata_size); 467 mip->mi_pdata = NULL; 468 mip->mi_pdata_size = 0; 469 } 470 471 if (minor != 0) { 472 ASSERT(minor > MAC_MAX_MINOR); 473 mac_minor_rele(minor); 474 } 475 476 mac_unregister_priv_prop(mip); 477 478 kmem_cache_free(i_mac_impl_cachep, mip); 479 return (err); 480 } 481 482 /* 483 * Unregister from the GLDv3 framework 484 */ 485 int 486 mac_unregister(mac_handle_t mh) 487 { 488 int err; 489 mac_impl_t *mip = (mac_impl_t *)mh; 490 mod_hash_val_t val; 491 mac_margin_req_t *mmr, *nextmmr; 492 493 /* Fail the unregister if there are any open references to this mac. */ 494 if ((err = mac_disable_nowait(mh)) != 0) 495 return (err); 496 497 /* 498 * Clean up notification thread and wait for it to exit. 499 */ 500 i_mac_notify_exit(mip); 501 502 i_mac_perim_enter(mip); 503 504 /* 505 * There is still resource properties configured over this mac. 506 */ 507 if (mip->mi_resource_props.mrp_mask != 0) 508 mac_fastpath_enable((mac_handle_t)mip); 509 510 if (mip->mi_minor < MAC_MAX_MINOR + 1) { 511 ddi_remove_minor_node(mip->mi_dip, mip->mi_name); 512 ddi_remove_minor_node(mip->mi_dip, 513 (char *)ddi_driver_name(mip->mi_dip)); 514 } 515 516 ASSERT(mip->mi_nactiveclients == 0 && !(mip->mi_state_flags & 517 MIS_EXCLUSIVE)); 518 519 mac_stat_destroy(mip); 520 521 (void) mod_hash_remove(i_mac_impl_hash, 522 (mod_hash_key_t)mip->mi_name, &val); 523 ASSERT(mip == (mac_impl_t *)val); 524 525 ASSERT(i_mac_impl_count > 0); 526 atomic_dec_32(&i_mac_impl_count); 527 528 if (mip->mi_pdata != NULL) 529 kmem_free(mip->mi_pdata, mip->mi_pdata_size); 530 mip->mi_pdata = NULL; 531 mip->mi_pdata_size = 0; 532 533 /* 534 * Free the list of margin request. 535 */ 536 for (mmr = mip->mi_mmrp; mmr != NULL; mmr = nextmmr) { 537 nextmmr = mmr->mmr_nextp; 538 kmem_free(mmr, sizeof (mac_margin_req_t)); 539 } 540 mip->mi_mmrp = NULL; 541 542 mip->mi_linkstate = mip->mi_lowlinkstate = LINK_STATE_UNKNOWN; 543 kmem_free(mip->mi_info.mi_unicst_addr, mip->mi_type->mt_addr_length); 544 mip->mi_info.mi_unicst_addr = NULL; 545 546 atomic_dec_32(&mip->mi_type->mt_ref); 547 mip->mi_type = NULL; 548 549 /* 550 * Free the primary MAC address. 551 */ 552 mac_fini_macaddr(mip); 553 554 /* 555 * free all rings 556 */ 557 mac_free_rings(mip, MAC_RING_TYPE_RX); 558 mac_free_rings(mip, MAC_RING_TYPE_TX); 559 560 mac_addr_factory_fini(mip); 561 562 bzero(mip->mi_addr, MAXMACADDRLEN); 563 bzero(mip->mi_dstaddr, MAXMACADDRLEN); 564 565 /* and the flows */ 566 mac_flow_tab_destroy(mip->mi_flow_tab); 567 mip->mi_flow_tab = NULL; 568 569 if (mip->mi_minor > MAC_MAX_MINOR) 570 mac_minor_rele(mip->mi_minor); 571 572 cmn_err(CE_NOTE, "!%s unregistered", mip->mi_name); 573 574 /* 575 * Reset the perim related fields to default values before 576 * kmem_cache_free 577 */ 578 i_mac_perim_exit(mip); 579 mip->mi_state_flags = 0; 580 581 mac_unregister_priv_prop(mip); 582 583 ASSERT(mip->mi_bridge_link == NULL); 584 kmem_cache_free(i_mac_impl_cachep, mip); 585 586 return (0); 587 } 588 589 /* DATA RECEPTION */ 590 591 /* 592 * This function is invoked for packets received by the MAC driver in 593 * interrupt context. The ring generation number provided by the driver 594 * is matched with the ring generation number held in MAC. If they do not 595 * match, received packets are considered stale packets coming from an older 596 * assignment of the ring. Drop them. 597 */ 598 void 599 mac_rx_ring(mac_handle_t mh, mac_ring_handle_t mrh, mblk_t *mp_chain, 600 uint64_t mr_gen_num) 601 { 602 mac_ring_t *mr = (mac_ring_t *)mrh; 603 604 if ((mr != NULL) && (mr->mr_gen_num != mr_gen_num)) { 605 DTRACE_PROBE2(mac__rx__rings__stale__packet, uint64_t, 606 mr->mr_gen_num, uint64_t, mr_gen_num); 607 freemsgchain(mp_chain); 608 return; 609 } 610 mac_rx(mh, (mac_resource_handle_t)mrh, mp_chain); 611 } 612 613 /* 614 * This function is invoked for each packet received by the underlying driver. 615 */ 616 void 617 mac_rx(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain) 618 { 619 mac_impl_t *mip = (mac_impl_t *)mh; 620 621 /* 622 * Check if the link is part of a bridge. If not, then we don't need 623 * to take the lock to remain consistent. Make this common case 624 * lock-free and tail-call optimized. 625 */ 626 if (mip->mi_bridge_link == NULL) { 627 mac_rx_common(mh, mrh, mp_chain); 628 } else { 629 /* 630 * Once we take a reference on the bridge link, the bridge 631 * module itself can't unload, so the callback pointers are 632 * stable. 633 */ 634 mutex_enter(&mip->mi_bridge_lock); 635 if ((mh = mip->mi_bridge_link) != NULL) 636 mac_bridge_ref_cb(mh, B_TRUE); 637 mutex_exit(&mip->mi_bridge_lock); 638 if (mh == NULL) { 639 mac_rx_common((mac_handle_t)mip, mrh, mp_chain); 640 } else { 641 mac_bridge_rx_cb(mh, mrh, mp_chain); 642 mac_bridge_ref_cb(mh, B_FALSE); 643 } 644 } 645 } 646 647 /* 648 * Special case function: this allows snooping of packets transmitted and 649 * received by TRILL. By design, they go directly into the TRILL module. 650 */ 651 void 652 mac_trill_snoop(mac_handle_t mh, mblk_t *mp) 653 { 654 mac_impl_t *mip = (mac_impl_t *)mh; 655 656 if (mip->mi_promisc_list != NULL) 657 mac_promisc_dispatch(mip, mp, NULL); 658 } 659 660 /* 661 * This is the upward reentry point for packets arriving from the bridging 662 * module and from mac_rx for links not part of a bridge. 663 */ 664 void 665 mac_rx_common(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain) 666 { 667 mac_impl_t *mip = (mac_impl_t *)mh; 668 mac_ring_t *mr = (mac_ring_t *)mrh; 669 mac_soft_ring_set_t *mac_srs; 670 mblk_t *bp = mp_chain; 671 boolean_t hw_classified = B_FALSE; 672 673 /* 674 * If there are any promiscuous mode callbacks defined for 675 * this MAC, pass them a copy if appropriate. 676 */ 677 if (mip->mi_promisc_list != NULL) 678 mac_promisc_dispatch(mip, mp_chain, NULL); 679 680 if (mr != NULL) { 681 /* 682 * If the SRS teardown has started, just return. The 'mr' 683 * continues to be valid until the driver unregisters the mac. 684 * Hardware classified packets will not make their way up 685 * beyond this point once the teardown has started. The driver 686 * is never passed a pointer to a flow entry or SRS or any 687 * structure that can be freed much before mac_unregister. 688 */ 689 mutex_enter(&mr->mr_lock); 690 if ((mr->mr_state != MR_INUSE) || (mr->mr_flag & 691 (MR_INCIPIENT | MR_CONDEMNED | MR_QUIESCE))) { 692 mutex_exit(&mr->mr_lock); 693 freemsgchain(mp_chain); 694 return; 695 } 696 if (mr->mr_classify_type == MAC_HW_CLASSIFIER) { 697 hw_classified = B_TRUE; 698 MR_REFHOLD_LOCKED(mr); 699 } 700 mutex_exit(&mr->mr_lock); 701 702 /* 703 * We check if an SRS is controlling this ring. 704 * If so, we can directly call the srs_lower_proc 705 * routine otherwise we need to go through mac_rx_classify 706 * to reach the right place. 707 */ 708 if (hw_classified) { 709 mac_srs = mr->mr_srs; 710 /* 711 * This is supposed to be the fast path. 712 * All packets received though here were steered by 713 * the hardware classifier, and share the same 714 * MAC header info. 715 */ 716 mac_srs->srs_rx.sr_lower_proc(mh, 717 (mac_resource_handle_t)mac_srs, mp_chain, B_FALSE); 718 MR_REFRELE(mr); 719 return; 720 } 721 /* We'll fall through to software classification */ 722 } else { 723 flow_entry_t *flent; 724 int err; 725 726 rw_enter(&mip->mi_rw_lock, RW_READER); 727 if (mip->mi_single_active_client != NULL) { 728 flent = mip->mi_single_active_client->mci_flent_list; 729 FLOW_TRY_REFHOLD(flent, err); 730 rw_exit(&mip->mi_rw_lock); 731 if (err == 0) { 732 (flent->fe_cb_fn)(flent->fe_cb_arg1, 733 flent->fe_cb_arg2, mp_chain, B_FALSE); 734 FLOW_REFRELE(flent); 735 return; 736 } 737 } else { 738 rw_exit(&mip->mi_rw_lock); 739 } 740 } 741 742 if (!FLOW_TAB_EMPTY(mip->mi_flow_tab)) { 743 if ((bp = mac_rx_flow(mh, mrh, bp)) == NULL) 744 return; 745 } 746 747 freemsgchain(bp); 748 } 749 750 /* DATA TRANSMISSION */ 751 752 /* 753 * A driver's notification to resume transmission, in case of a provider 754 * without TX rings. 755 */ 756 void 757 mac_tx_update(mac_handle_t mh) 758 { 759 /* 760 * Walk the list of MAC clients (mac_client_handle) 761 * and update 762 */ 763 i_mac_tx_srs_notify((mac_impl_t *)mh, NULL); 764 } 765 766 /* 767 * A driver's notification to resume transmission on the specified TX ring. 768 */ 769 void 770 mac_tx_ring_update(mac_handle_t mh, mac_ring_handle_t rh) 771 { 772 i_mac_tx_srs_notify((mac_impl_t *)mh, rh); 773 } 774 775 /* LINK STATE */ 776 /* 777 * Notify the MAC layer about a link state change 778 */ 779 void 780 mac_link_update(mac_handle_t mh, link_state_t link) 781 { 782 mac_impl_t *mip = (mac_impl_t *)mh; 783 784 /* 785 * Save the link state. 786 */ 787 mip->mi_lowlinkstate = link; 788 789 /* 790 * Send a MAC_NOTE_LOWLINK notification. This tells the notification 791 * thread to deliver both lower and upper notifications. 792 */ 793 i_mac_notify(mip, MAC_NOTE_LOWLINK); 794 } 795 796 /* 797 * Notify the MAC layer about a link state change due to bridging. 798 */ 799 void 800 mac_link_redo(mac_handle_t mh, link_state_t link) 801 { 802 mac_impl_t *mip = (mac_impl_t *)mh; 803 804 /* 805 * Save the link state. 806 */ 807 mip->mi_linkstate = link; 808 809 /* 810 * Send a MAC_NOTE_LINK notification. Only upper notifications are 811 * made. 812 */ 813 i_mac_notify(mip, MAC_NOTE_LINK); 814 } 815 816 /* OTHER CONTROL INFORMATION */ 817 818 /* 819 * A driver notified us that its primary MAC address has changed. 820 */ 821 void 822 mac_unicst_update(mac_handle_t mh, const uint8_t *addr) 823 { 824 mac_impl_t *mip = (mac_impl_t *)mh; 825 826 if (mip->mi_type->mt_addr_length == 0) 827 return; 828 829 i_mac_perim_enter(mip); 830 /* 831 * If address doesn't change, do nothing. 832 */ 833 if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) { 834 i_mac_perim_exit(mip); 835 return; 836 } 837 838 /* 839 * Freshen the MAC address value and update all MAC clients that 840 * share this MAC address. 841 */ 842 mac_freshen_macaddr(mac_find_macaddr(mip, mip->mi_addr), 843 (uint8_t *)addr); 844 845 i_mac_perim_exit(mip); 846 847 /* 848 * Send a MAC_NOTE_UNICST notification. 849 */ 850 i_mac_notify(mip, MAC_NOTE_UNICST); 851 } 852 853 /* 854 * MAC plugin information changed. 855 */ 856 int 857 mac_pdata_update(mac_handle_t mh, void *mac_pdata, size_t dsize) 858 { 859 mac_impl_t *mip = (mac_impl_t *)mh; 860 861 /* 862 * Verify that the plugin supports MAC plugin data and that the 863 * supplied data is valid. 864 */ 865 if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY)) 866 return (EINVAL); 867 if (!mip->mi_type->mt_ops.mtops_pdata_verify(mac_pdata, dsize)) 868 return (EINVAL); 869 870 if (mip->mi_pdata != NULL) 871 kmem_free(mip->mi_pdata, mip->mi_pdata_size); 872 873 mip->mi_pdata = kmem_alloc(dsize, KM_SLEEP); 874 bcopy(mac_pdata, mip->mi_pdata, dsize); 875 mip->mi_pdata_size = dsize; 876 877 /* 878 * Since the MAC plugin data is used to construct MAC headers that 879 * were cached in fast-path headers, we need to flush fast-path 880 * information for links associated with this mac. 881 */ 882 i_mac_notify(mip, MAC_NOTE_FASTPATH_FLUSH); 883 return (0); 884 } 885 886 /* 887 * Invoked by driver as well as the framework to notify its capability change. 888 */ 889 void 890 mac_capab_update(mac_handle_t mh) 891 { 892 /* Send MAC_NOTE_CAPAB_CHG notification */ 893 i_mac_notify((mac_impl_t *)mh, MAC_NOTE_CAPAB_CHG); 894 } 895 896 int 897 mac_maxsdu_update(mac_handle_t mh, uint_t sdu_max) 898 { 899 mac_impl_t *mip = (mac_impl_t *)mh; 900 901 if (sdu_max == 0 || sdu_max < mip->mi_sdu_min) 902 return (EINVAL); 903 mip->mi_sdu_max = sdu_max; 904 905 /* Send a MAC_NOTE_SDU_SIZE notification. */ 906 i_mac_notify(mip, MAC_NOTE_SDU_SIZE); 907 return (0); 908 } 909 910 /* PRIVATE FUNCTIONS, FOR INTERNAL USE ONLY */ 911 912 /* 913 * Updates the mac_impl structure with the current state of the link 914 */ 915 static void 916 i_mac_log_link_state(mac_impl_t *mip) 917 { 918 /* 919 * If no change, then it is not interesting. 920 */ 921 if (mip->mi_lastlowlinkstate == mip->mi_lowlinkstate) 922 return; 923 924 switch (mip->mi_lowlinkstate) { 925 case LINK_STATE_UP: 926 if (mip->mi_type->mt_ops.mtops_ops & MTOPS_LINK_DETAILS) { 927 char det[200]; 928 929 mip->mi_type->mt_ops.mtops_link_details(det, 930 sizeof (det), (mac_handle_t)mip, mip->mi_pdata); 931 932 cmn_err(CE_NOTE, "!%s link up, %s", mip->mi_name, det); 933 } else { 934 cmn_err(CE_NOTE, "!%s link up", mip->mi_name); 935 } 936 break; 937 938 case LINK_STATE_DOWN: 939 /* 940 * Only transitions from UP to DOWN are interesting 941 */ 942 if (mip->mi_lastlowlinkstate != LINK_STATE_UNKNOWN) 943 cmn_err(CE_NOTE, "!%s link down", mip->mi_name); 944 break; 945 946 case LINK_STATE_UNKNOWN: 947 /* 948 * This case is normally not interesting. 949 */ 950 break; 951 } 952 mip->mi_lastlowlinkstate = mip->mi_lowlinkstate; 953 } 954 955 /* 956 * Main routine for the callbacks notifications thread 957 */ 958 static void 959 i_mac_notify_thread(void *arg) 960 { 961 mac_impl_t *mip = arg; 962 callb_cpr_t cprinfo; 963 mac_cb_t *mcb; 964 mac_cb_info_t *mcbi; 965 mac_notify_cb_t *mncb; 966 967 mcbi = &mip->mi_notify_cb_info; 968 CALLB_CPR_INIT(&cprinfo, mcbi->mcbi_lockp, callb_generic_cpr, 969 "i_mac_notify_thread"); 970 971 mutex_enter(mcbi->mcbi_lockp); 972 973 for (;;) { 974 uint32_t bits; 975 uint32_t type; 976 977 bits = mip->mi_notify_bits; 978 if (bits == 0) { 979 CALLB_CPR_SAFE_BEGIN(&cprinfo); 980 cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 981 CALLB_CPR_SAFE_END(&cprinfo, mcbi->mcbi_lockp); 982 continue; 983 } 984 mip->mi_notify_bits = 0; 985 if ((bits & (1 << MAC_NNOTE)) != 0) { 986 /* request to quit */ 987 ASSERT(mip->mi_state_flags & MIS_DISABLED); 988 break; 989 } 990 991 mutex_exit(mcbi->mcbi_lockp); 992 993 /* 994 * Log link changes on the actual link, but then do reports on 995 * synthetic state (if part of a bridge). 996 */ 997 if ((bits & (1 << MAC_NOTE_LOWLINK)) != 0) { 998 link_state_t newstate; 999 mac_handle_t mh; 1000 1001 i_mac_log_link_state(mip); 1002 newstate = mip->mi_lowlinkstate; 1003 if (mip->mi_bridge_link != NULL) { 1004 mutex_enter(&mip->mi_bridge_lock); 1005 if ((mh = mip->mi_bridge_link) != NULL) { 1006 newstate = mac_bridge_ls_cb(mh, 1007 newstate); 1008 } 1009 mutex_exit(&mip->mi_bridge_lock); 1010 } 1011 if (newstate != mip->mi_linkstate) { 1012 mip->mi_linkstate = newstate; 1013 bits |= 1 << MAC_NOTE_LINK; 1014 } 1015 } 1016 1017 /* 1018 * Do notification callbacks for each notification type. 1019 */ 1020 for (type = 0; type < MAC_NNOTE; type++) { 1021 if ((bits & (1 << type)) == 0) { 1022 continue; 1023 } 1024 1025 if (mac_notify_cb_list[type] != NULL) 1026 (*mac_notify_cb_list[type])(mip); 1027 1028 /* 1029 * Walk the list of notifications. 1030 */ 1031 MAC_CALLBACK_WALKER_INC(&mip->mi_notify_cb_info); 1032 for (mcb = mip->mi_notify_cb_list; mcb != NULL; 1033 mcb = mcb->mcb_nextp) { 1034 mncb = (mac_notify_cb_t *)mcb->mcb_objp; 1035 mncb->mncb_fn(mncb->mncb_arg, type); 1036 } 1037 MAC_CALLBACK_WALKER_DCR(&mip->mi_notify_cb_info, 1038 &mip->mi_notify_cb_list); 1039 } 1040 1041 mutex_enter(mcbi->mcbi_lockp); 1042 } 1043 1044 mip->mi_state_flags |= MIS_NOTIFY_DONE; 1045 cv_broadcast(&mcbi->mcbi_cv); 1046 1047 /* CALLB_CPR_EXIT drops the lock */ 1048 CALLB_CPR_EXIT(&cprinfo); 1049 thread_exit(); 1050 } 1051 1052 /* 1053 * Signal the i_mac_notify_thread asking it to quit. 1054 * Then wait till it is done. 1055 */ 1056 void 1057 i_mac_notify_exit(mac_impl_t *mip) 1058 { 1059 mac_cb_info_t *mcbi; 1060 1061 mcbi = &mip->mi_notify_cb_info; 1062 1063 mutex_enter(mcbi->mcbi_lockp); 1064 mip->mi_notify_bits = (1 << MAC_NNOTE); 1065 cv_broadcast(&mcbi->mcbi_cv); 1066 1067 1068 while ((mip->mi_notify_thread != NULL) && 1069 !(mip->mi_state_flags & MIS_NOTIFY_DONE)) { 1070 cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 1071 } 1072 1073 /* Necessary clean up before doing kmem_cache_free */ 1074 mip->mi_state_flags &= ~MIS_NOTIFY_DONE; 1075 mip->mi_notify_bits = 0; 1076 mip->mi_notify_thread = NULL; 1077 mutex_exit(mcbi->mcbi_lockp); 1078 } 1079 1080 /* 1081 * Entry point invoked by drivers to dynamically add a ring to an 1082 * existing group. 1083 */ 1084 int 1085 mac_group_add_ring(mac_group_handle_t gh, int index) 1086 { 1087 mac_group_t *group = (mac_group_t *)gh; 1088 mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 1089 int ret; 1090 1091 i_mac_perim_enter(mip); 1092 1093 /* 1094 * Only RX rings can be added or removed by drivers currently. 1095 */ 1096 ASSERT(group->mrg_type == MAC_RING_TYPE_RX); 1097 1098 ret = i_mac_group_add_ring(group, NULL, index); 1099 1100 i_mac_perim_exit(mip); 1101 1102 return (ret); 1103 } 1104 1105 /* 1106 * Entry point invoked by drivers to dynamically remove a ring 1107 * from an existing group. The specified ring handle must no longer 1108 * be used by the driver after a call to this function. 1109 */ 1110 void 1111 mac_group_rem_ring(mac_group_handle_t gh, mac_ring_handle_t rh) 1112 { 1113 mac_group_t *group = (mac_group_t *)gh; 1114 mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 1115 1116 i_mac_perim_enter(mip); 1117 1118 /* 1119 * Only RX rings can be added or removed by drivers currently. 1120 */ 1121 ASSERT(group->mrg_type == MAC_RING_TYPE_RX); 1122 1123 i_mac_group_rem_ring(group, (mac_ring_t *)rh, B_TRUE); 1124 1125 i_mac_perim_exit(mip); 1126 } 1127