18275SEric Cheng /* 28275SEric Cheng * CDDL HEADER START 38275SEric Cheng * 48275SEric Cheng * The contents of this file are subject to the terms of the 58275SEric Cheng * Common Development and Distribution License (the "License"). 68275SEric Cheng * You may not use this file except in compliance with the License. 78275SEric Cheng * 88275SEric Cheng * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 98275SEric Cheng * or http://www.opensolaris.org/os/licensing. 108275SEric Cheng * See the License for the specific language governing permissions 118275SEric Cheng * and limitations under the License. 128275SEric Cheng * 138275SEric Cheng * When distributing Covered Code, include this CDDL HEADER in each 148275SEric Cheng * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 158275SEric Cheng * If applicable, add the following below this CDDL HEADER, with the 168275SEric Cheng * fields enclosed by brackets "[]" replaced with your own identifying 178275SEric Cheng * information: Portions Copyright [yyyy] [name of copyright owner] 188275SEric Cheng * 198275SEric Cheng * CDDL HEADER END 208275SEric Cheng */ 218275SEric Cheng 228275SEric Cheng /* 238833SVenu.Iyer@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 248275SEric Cheng * Use is subject to license terms. 258275SEric Cheng */ 268275SEric Cheng 278275SEric Cheng #include <sys/types.h> 288275SEric Cheng #include <sys/conf.h> 298275SEric Cheng #include <sys/id_space.h> 308275SEric Cheng #include <sys/esunddi.h> 318275SEric Cheng #include <sys/stat.h> 328275SEric Cheng #include <sys/mkdev.h> 338275SEric Cheng #include <sys/stream.h> 348275SEric Cheng #include <sys/strsubr.h> 358275SEric Cheng #include <sys/dlpi.h> 368275SEric Cheng #include <sys/modhash.h> 378275SEric Cheng #include <sys/mac.h> 388275SEric Cheng #include <sys/mac_provider.h> 398275SEric Cheng #include <sys/mac_impl.h> 408275SEric Cheng #include <sys/mac_client_impl.h> 418275SEric Cheng #include <sys/mac_client_priv.h> 428275SEric Cheng #include <sys/mac_soft_ring.h> 438275SEric Cheng #include <sys/modctl.h> 448275SEric Cheng #include <sys/fs/dv_node.h> 458275SEric Cheng #include <sys/thread.h> 468275SEric Cheng #include <sys/proc.h> 478275SEric Cheng #include <sys/callb.h> 488275SEric Cheng #include <sys/cpuvar.h> 498275SEric Cheng #include <sys/atomic.h> 508275SEric Cheng #include <sys/sdt.h> 518275SEric Cheng #include <sys/mac_flow.h> 528275SEric Cheng #include <sys/ddi_intr_impl.h> 538275SEric Cheng #include <sys/disp.h> 548275SEric Cheng #include <sys/sdt.h> 558275SEric Cheng 568275SEric Cheng /* 578275SEric Cheng * MAC Provider Interface. 588275SEric Cheng * 598275SEric Cheng * Interface for GLDv3 compatible NIC drivers. 608275SEric Cheng */ 618275SEric Cheng 628275SEric Cheng static void i_mac_notify_thread(void *); 638275SEric Cheng 648275SEric Cheng typedef void (*mac_notify_default_cb_fn_t)(mac_impl_t *); 658275SEric Cheng 66*10491SRishi.Srivatsavai@Sun.COM static const mac_notify_default_cb_fn_t mac_notify_cb_list[MAC_NNOTE] = { 67*10491SRishi.Srivatsavai@Sun.COM mac_fanout_recompute, /* MAC_NOTE_LINK */ 68*10491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_UNICST */ 69*10491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_TX */ 70*10491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_DEVPROMISC */ 71*10491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_FASTPATH_FLUSH */ 72*10491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_SDU_SIZE */ 73*10491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_MARGIN */ 74*10491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_CAPAB_CHG */ 75*10491SRishi.Srivatsavai@Sun.COM NULL /* MAC_NOTE_LOWLINK */ 768275SEric Cheng }; 778275SEric Cheng 788275SEric Cheng /* 798275SEric Cheng * Driver support functions. 808275SEric Cheng */ 818275SEric Cheng 828275SEric Cheng /* REGISTRATION */ 838275SEric Cheng 848275SEric Cheng mac_register_t * 858275SEric Cheng mac_alloc(uint_t mac_version) 868275SEric Cheng { 878275SEric Cheng mac_register_t *mregp; 888275SEric Cheng 898275SEric Cheng /* 908275SEric Cheng * Make sure there isn't a version mismatch between the driver and 918275SEric Cheng * the framework. In the future, if multiple versions are 928275SEric Cheng * supported, this check could become more sophisticated. 938275SEric Cheng */ 948275SEric Cheng if (mac_version != MAC_VERSION) 958275SEric Cheng return (NULL); 968275SEric Cheng 978275SEric Cheng mregp = kmem_zalloc(sizeof (mac_register_t), KM_SLEEP); 988275SEric Cheng mregp->m_version = mac_version; 998275SEric Cheng return (mregp); 1008275SEric Cheng } 1018275SEric Cheng 1028275SEric Cheng void 1038275SEric Cheng mac_free(mac_register_t *mregp) 1048275SEric Cheng { 1058275SEric Cheng kmem_free(mregp, sizeof (mac_register_t)); 1068275SEric Cheng } 1078275SEric Cheng 1088275SEric Cheng /* 1098275SEric Cheng * mac_register() is how drivers register new MACs with the GLDv3 1108275SEric Cheng * framework. The mregp argument is allocated by drivers using the 1118275SEric Cheng * mac_alloc() function, and can be freed using mac_free() immediately upon 1128275SEric Cheng * return from mac_register(). Upon success (0 return value), the mhp 1138275SEric Cheng * opaque pointer becomes the driver's handle to its MAC interface, and is 1148275SEric Cheng * the argument to all other mac module entry points. 1158275SEric Cheng */ 1168275SEric Cheng /* ARGSUSED */ 1178275SEric Cheng int 1188275SEric Cheng mac_register(mac_register_t *mregp, mac_handle_t *mhp) 1198275SEric Cheng { 1208275SEric Cheng mac_impl_t *mip; 1218275SEric Cheng mactype_t *mtype; 1228275SEric Cheng int err = EINVAL; 1238275SEric Cheng struct devnames *dnp = NULL; 1248275SEric Cheng uint_t instance; 1258275SEric Cheng boolean_t style1_created = B_FALSE; 1268275SEric Cheng boolean_t style2_created = B_FALSE; 1278275SEric Cheng char *driver; 1288275SEric Cheng minor_t minor = 0; 1298275SEric Cheng 1308275SEric Cheng /* Find the required MAC-Type plugin. */ 1318275SEric Cheng if ((mtype = mactype_getplugin(mregp->m_type_ident)) == NULL) 1328275SEric Cheng return (EINVAL); 1338275SEric Cheng 1348275SEric Cheng /* Create a mac_impl_t to represent this MAC. */ 1358275SEric Cheng mip = kmem_cache_alloc(i_mac_impl_cachep, KM_SLEEP); 1368275SEric Cheng 1378275SEric Cheng /* 1388275SEric Cheng * The mac is not ready for open yet. 1398275SEric Cheng */ 1408275SEric Cheng mip->mi_state_flags |= MIS_DISABLED; 1418275SEric Cheng 1428275SEric Cheng /* 1438275SEric Cheng * When a mac is registered, the m_instance field can be set to: 1448275SEric Cheng * 1458275SEric Cheng * 0: Get the mac's instance number from m_dip. 1468275SEric Cheng * This is usually used for physical device dips. 1478275SEric Cheng * 1488275SEric Cheng * [1 .. MAC_MAX_MINOR-1]: Use the value as the mac's instance number. 1498275SEric Cheng * For example, when an aggregation is created with the key option, 1508275SEric Cheng * "key" will be used as the instance number. 1518275SEric Cheng * 1528275SEric Cheng * -1: Assign an instance number from [MAC_MAX_MINOR .. MAXMIN-1]. 1538275SEric Cheng * This is often used when a MAC of a virtual link is registered 1548275SEric Cheng * (e.g., aggregation when "key" is not specified, or vnic). 1558275SEric Cheng * 1568275SEric Cheng * Note that the instance number is used to derive the mi_minor field 1578275SEric Cheng * of mac_impl_t, which will then be used to derive the name of kstats 1588275SEric Cheng * and the devfs nodes. The first 2 cases are needed to preserve 1598275SEric Cheng * backward compatibility. 1608275SEric Cheng */ 1618275SEric Cheng switch (mregp->m_instance) { 1628275SEric Cheng case 0: 1638275SEric Cheng instance = ddi_get_instance(mregp->m_dip); 1648275SEric Cheng break; 1658275SEric Cheng case ((uint_t)-1): 1668275SEric Cheng minor = mac_minor_hold(B_TRUE); 1678275SEric Cheng if (minor == 0) { 1688275SEric Cheng err = ENOSPC; 1698275SEric Cheng goto fail; 1708275SEric Cheng } 1718275SEric Cheng instance = minor - 1; 1728275SEric Cheng break; 1738275SEric Cheng default: 1748275SEric Cheng instance = mregp->m_instance; 1758275SEric Cheng if (instance >= MAC_MAX_MINOR) { 1768275SEric Cheng err = EINVAL; 1778275SEric Cheng goto fail; 1788275SEric Cheng } 1798275SEric Cheng break; 1808275SEric Cheng } 1818275SEric Cheng 1828275SEric Cheng mip->mi_minor = (minor_t)(instance + 1); 1838275SEric Cheng mip->mi_dip = mregp->m_dip; 1848275SEric Cheng mip->mi_clients_list = NULL; 1858275SEric Cheng mip->mi_nclients = 0; 1868275SEric Cheng 187*10491SRishi.Srivatsavai@Sun.COM /* Set the default IEEE Port VLAN Identifier */ 188*10491SRishi.Srivatsavai@Sun.COM mip->mi_pvid = 1; 189*10491SRishi.Srivatsavai@Sun.COM 190*10491SRishi.Srivatsavai@Sun.COM /* Default bridge link learning protection values */ 191*10491SRishi.Srivatsavai@Sun.COM mip->mi_llimit = 1000; 192*10491SRishi.Srivatsavai@Sun.COM mip->mi_ldecay = 200; 193*10491SRishi.Srivatsavai@Sun.COM 1948275SEric Cheng driver = (char *)ddi_driver_name(mip->mi_dip); 1958275SEric Cheng 1968275SEric Cheng /* Construct the MAC name as <drvname><instance> */ 1978275SEric Cheng (void) snprintf(mip->mi_name, sizeof (mip->mi_name), "%s%d", 1988275SEric Cheng driver, instance); 1998275SEric Cheng 2008275SEric Cheng mip->mi_driver = mregp->m_driver; 2018275SEric Cheng 2028275SEric Cheng mip->mi_type = mtype; 2038275SEric Cheng mip->mi_margin = mregp->m_margin; 2048275SEric Cheng mip->mi_info.mi_media = mtype->mt_type; 2058275SEric Cheng mip->mi_info.mi_nativemedia = mtype->mt_nativetype; 2068275SEric Cheng if (mregp->m_max_sdu <= mregp->m_min_sdu) 2078275SEric Cheng goto fail; 2088275SEric Cheng mip->mi_sdu_min = mregp->m_min_sdu; 2098275SEric Cheng mip->mi_sdu_max = mregp->m_max_sdu; 2108275SEric Cheng mip->mi_info.mi_addr_length = mip->mi_type->mt_addr_length; 2118275SEric Cheng /* 2128275SEric Cheng * If the media supports a broadcast address, cache a pointer to it 2138275SEric Cheng * in the mac_info_t so that upper layers can use it. 2148275SEric Cheng */ 2158275SEric Cheng mip->mi_info.mi_brdcst_addr = mip->mi_type->mt_brdcst_addr; 2168275SEric Cheng 2178275SEric Cheng mip->mi_v12n_level = mregp->m_v12n; 2188275SEric Cheng 2198275SEric Cheng /* 2208275SEric Cheng * Copy the unicast source address into the mac_info_t, but only if 2218275SEric Cheng * the MAC-Type defines a non-zero address length. We need to 2228275SEric Cheng * handle MAC-Types that have an address length of 0 2238275SEric Cheng * (point-to-point protocol MACs for example). 2248275SEric Cheng */ 2258275SEric Cheng if (mip->mi_type->mt_addr_length > 0) { 2268275SEric Cheng if (mregp->m_src_addr == NULL) 2278275SEric Cheng goto fail; 2288275SEric Cheng mip->mi_info.mi_unicst_addr = 2298275SEric Cheng kmem_alloc(mip->mi_type->mt_addr_length, KM_SLEEP); 2308275SEric Cheng bcopy(mregp->m_src_addr, mip->mi_info.mi_unicst_addr, 2318275SEric Cheng mip->mi_type->mt_addr_length); 2328275SEric Cheng 2338275SEric Cheng /* 2348275SEric Cheng * Copy the fixed 'factory' MAC address from the immutable 2358275SEric Cheng * info. This is taken to be the MAC address currently in 2368275SEric Cheng * use. 2378275SEric Cheng */ 2388275SEric Cheng bcopy(mip->mi_info.mi_unicst_addr, mip->mi_addr, 2398275SEric Cheng mip->mi_type->mt_addr_length); 2408275SEric Cheng 2418275SEric Cheng /* 2428275SEric Cheng * At this point, we should set up the classification 2438275SEric Cheng * rules etc but we delay it till mac_open() so that 2448275SEric Cheng * the resource discovery has taken place and we 2458275SEric Cheng * know someone wants to use the device. Otherwise 2468275SEric Cheng * memory gets allocated for Rx ring structures even 2478275SEric Cheng * during probe. 2488275SEric Cheng */ 2498275SEric Cheng 2508275SEric Cheng /* Copy the destination address if one is provided. */ 2518275SEric Cheng if (mregp->m_dst_addr != NULL) { 2528275SEric Cheng bcopy(mregp->m_dst_addr, mip->mi_dstaddr, 2538275SEric Cheng mip->mi_type->mt_addr_length); 2548275SEric Cheng } 2558275SEric Cheng } else if (mregp->m_src_addr != NULL) { 2568275SEric Cheng goto fail; 2578275SEric Cheng } 2588275SEric Cheng 2598275SEric Cheng /* 2608275SEric Cheng * The format of the m_pdata is specific to the plugin. It is 2618275SEric Cheng * passed in as an argument to all of the plugin callbacks. The 2628275SEric Cheng * driver can update this information by calling 2638275SEric Cheng * mac_pdata_update(). 2648275SEric Cheng */ 2658275SEric Cheng if (mregp->m_pdata != NULL) { 2668275SEric Cheng /* 2678275SEric Cheng * Verify that the plugin supports MAC plugin data and that 2688275SEric Cheng * the supplied data is valid. 2698275SEric Cheng */ 2708275SEric Cheng if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY)) 2718275SEric Cheng goto fail; 2728275SEric Cheng if (!mip->mi_type->mt_ops.mtops_pdata_verify(mregp->m_pdata, 2738275SEric Cheng mregp->m_pdata_size)) { 2748275SEric Cheng goto fail; 2758275SEric Cheng } 2768275SEric Cheng mip->mi_pdata = kmem_alloc(mregp->m_pdata_size, KM_SLEEP); 2778275SEric Cheng bcopy(mregp->m_pdata, mip->mi_pdata, mregp->m_pdata_size); 2788275SEric Cheng mip->mi_pdata_size = mregp->m_pdata_size; 2798275SEric Cheng } 2808275SEric Cheng 2818275SEric Cheng /* 2828275SEric Cheng * Register the private properties. 2838275SEric Cheng */ 2848275SEric Cheng mac_register_priv_prop(mip, mregp->m_priv_props, 2858275SEric Cheng mregp->m_priv_prop_count); 2868275SEric Cheng 2878275SEric Cheng /* 2888275SEric Cheng * Stash the driver callbacks into the mac_impl_t, but first sanity 2898275SEric Cheng * check to make sure all mandatory callbacks are set. 2908275SEric Cheng */ 2918275SEric Cheng if (mregp->m_callbacks->mc_getstat == NULL || 2928275SEric Cheng mregp->m_callbacks->mc_start == NULL || 2938275SEric Cheng mregp->m_callbacks->mc_stop == NULL || 2948275SEric Cheng mregp->m_callbacks->mc_setpromisc == NULL || 2958275SEric Cheng mregp->m_callbacks->mc_multicst == NULL) { 2968275SEric Cheng goto fail; 2978275SEric Cheng } 2988275SEric Cheng mip->mi_callbacks = mregp->m_callbacks; 2998275SEric Cheng 3009073SCathy.Zhou@Sun.COM if (mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LEGACY, 3019073SCathy.Zhou@Sun.COM &mip->mi_capab_legacy)) { 3028275SEric Cheng mip->mi_state_flags |= MIS_LEGACY; 3039073SCathy.Zhou@Sun.COM mip->mi_phy_dev = mip->mi_capab_legacy.ml_dev; 3048275SEric Cheng } else { 3058275SEric Cheng mip->mi_phy_dev = makedevice(ddi_driver_major(mip->mi_dip), 3068275SEric Cheng ddi_get_instance(mip->mi_dip) + 1); 3078275SEric Cheng } 3088275SEric Cheng 3098275SEric Cheng /* 3108275SEric Cheng * Allocate a notification thread. thread_create blocks for memory 3118275SEric Cheng * if needed, it never fails. 3128275SEric Cheng */ 3138275SEric Cheng mip->mi_notify_thread = thread_create(NULL, 0, i_mac_notify_thread, 3148275SEric Cheng mip, 0, &p0, TS_RUN, minclsyspri); 3158275SEric Cheng 3168275SEric Cheng /* 3178275SEric Cheng * Initialize the capabilities 3188275SEric Cheng */ 3198275SEric Cheng 3208275SEric Cheng if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, NULL)) 3218275SEric Cheng mip->mi_state_flags |= MIS_IS_VNIC; 3228275SEric Cheng 3238275SEric Cheng if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR, NULL)) 3248275SEric Cheng mip->mi_state_flags |= MIS_IS_AGGR; 3258275SEric Cheng 3268275SEric Cheng mac_addr_factory_init(mip); 3278275SEric Cheng 3288275SEric Cheng /* 3298275SEric Cheng * Enforce the virtrualization level registered. 3308275SEric Cheng */ 3318275SEric Cheng if (mip->mi_v12n_level & MAC_VIRT_LEVEL1) { 3328275SEric Cheng if (mac_init_rings(mip, MAC_RING_TYPE_RX) != 0 || 3338275SEric Cheng mac_init_rings(mip, MAC_RING_TYPE_TX) != 0) 3348275SEric Cheng goto fail; 3358275SEric Cheng 3368275SEric Cheng /* 3378275SEric Cheng * The driver needs to register at least rx rings for this 3388275SEric Cheng * virtualization level. 3398275SEric Cheng */ 3408275SEric Cheng if (mip->mi_rx_groups == NULL) 3418275SEric Cheng goto fail; 3428275SEric Cheng } 3438275SEric Cheng 3448275SEric Cheng /* 3458275SEric Cheng * The driver must set mc_unicst entry point to NULL when it advertises 3468275SEric Cheng * CAP_RINGS for rx groups. 3478275SEric Cheng */ 3488275SEric Cheng if (mip->mi_rx_groups != NULL) { 3498275SEric Cheng if (mregp->m_callbacks->mc_unicst != NULL) 3508275SEric Cheng goto fail; 3518275SEric Cheng } else { 3528275SEric Cheng if (mregp->m_callbacks->mc_unicst == NULL) 3538275SEric Cheng goto fail; 3548275SEric Cheng } 3558275SEric Cheng 3568275SEric Cheng /* 3578275SEric Cheng * The driver must set mc_tx entry point to NULL when it advertises 3588275SEric Cheng * CAP_RINGS for tx rings. 3598275SEric Cheng */ 3608275SEric Cheng if (mip->mi_tx_groups != NULL) { 3618275SEric Cheng if (mregp->m_callbacks->mc_tx != NULL) 3628275SEric Cheng goto fail; 3638275SEric Cheng } else { 3648275SEric Cheng if (mregp->m_callbacks->mc_tx == NULL) 3658275SEric Cheng goto fail; 3668275SEric Cheng } 3678275SEric Cheng 3688275SEric Cheng /* 3698275SEric Cheng * Initialize MAC addresses. Must be called after mac_init_rings(). 3708275SEric Cheng */ 3718275SEric Cheng mac_init_macaddr(mip); 3728275SEric Cheng 3738275SEric Cheng mip->mi_share_capab.ms_snum = 0; 3748275SEric Cheng if (mip->mi_v12n_level & MAC_VIRT_HIO) { 3758275SEric Cheng (void) mac_capab_get((mac_handle_t)mip, MAC_CAPAB_SHARES, 3768275SEric Cheng &mip->mi_share_capab); 3778275SEric Cheng } 3788275SEric Cheng 3798275SEric Cheng /* 3808275SEric Cheng * Initialize the kstats for this device. 3818275SEric Cheng */ 3828275SEric Cheng mac_stat_create(mip); 3838275SEric Cheng 3848275SEric Cheng /* Zero out any properties. */ 3858275SEric Cheng bzero(&mip->mi_resource_props, sizeof (mac_resource_props_t)); 3868275SEric Cheng 3878275SEric Cheng /* set the gldv3 flag in dn_flags */ 3888275SEric Cheng dnp = &devnamesp[ddi_driver_major(mip->mi_dip)]; 3898275SEric Cheng LOCK_DEV_OPS(&dnp->dn_lock); 3908275SEric Cheng dnp->dn_flags |= (DN_GLDV3_DRIVER | DN_NETWORK_DRIVER); 3918275SEric Cheng UNLOCK_DEV_OPS(&dnp->dn_lock); 3928275SEric Cheng 3938275SEric Cheng if (mip->mi_minor < MAC_MAX_MINOR + 1) { 3948275SEric Cheng /* Create a style-2 DLPI device */ 3958275SEric Cheng if (ddi_create_minor_node(mip->mi_dip, driver, S_IFCHR, 0, 3968275SEric Cheng DDI_NT_NET, CLONE_DEV) != DDI_SUCCESS) 3978275SEric Cheng goto fail; 3988275SEric Cheng style2_created = B_TRUE; 3998275SEric Cheng 4008275SEric Cheng /* Create a style-1 DLPI device */ 4018275SEric Cheng if (ddi_create_minor_node(mip->mi_dip, mip->mi_name, S_IFCHR, 4028275SEric Cheng mip->mi_minor, DDI_NT_NET, 0) != DDI_SUCCESS) 4038275SEric Cheng goto fail; 4048275SEric Cheng style1_created = B_TRUE; 4058275SEric Cheng } 4068275SEric Cheng 4078275SEric Cheng mac_flow_l2tab_create(mip, &mip->mi_flow_tab); 4088275SEric Cheng 4098275SEric Cheng rw_enter(&i_mac_impl_lock, RW_WRITER); 4108275SEric Cheng if (mod_hash_insert(i_mac_impl_hash, 4118275SEric Cheng (mod_hash_key_t)mip->mi_name, (mod_hash_val_t)mip) != 0) { 4128275SEric Cheng rw_exit(&i_mac_impl_lock); 4138275SEric Cheng err = EEXIST; 4148275SEric Cheng goto fail; 4158275SEric Cheng } 4168275SEric Cheng 4178275SEric Cheng DTRACE_PROBE2(mac__register, struct devnames *, dnp, 4188275SEric Cheng (mac_impl_t *), mip); 4198275SEric Cheng 4208275SEric Cheng /* 4218275SEric Cheng * Mark the MAC to be ready for open. 4228275SEric Cheng */ 4238275SEric Cheng mip->mi_state_flags &= ~MIS_DISABLED; 4248275SEric Cheng rw_exit(&i_mac_impl_lock); 4258275SEric Cheng 4268275SEric Cheng atomic_inc_32(&i_mac_impl_count); 4278275SEric Cheng 4288275SEric Cheng cmn_err(CE_NOTE, "!%s registered", mip->mi_name); 4298275SEric Cheng *mhp = (mac_handle_t)mip; 4308275SEric Cheng return (0); 4318275SEric Cheng 4328275SEric Cheng fail: 4338275SEric Cheng if (style1_created) 4348275SEric Cheng ddi_remove_minor_node(mip->mi_dip, mip->mi_name); 4358275SEric Cheng 4368275SEric Cheng if (style2_created) 4378275SEric Cheng ddi_remove_minor_node(mip->mi_dip, driver); 4388275SEric Cheng 4398275SEric Cheng mac_addr_factory_fini(mip); 4408275SEric Cheng 4418275SEric Cheng /* Clean up registered MAC addresses */ 4428275SEric Cheng mac_fini_macaddr(mip); 4438275SEric Cheng 4448275SEric Cheng /* Clean up registered rings */ 4458275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_RX); 4468275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_TX); 4478275SEric Cheng 4488275SEric Cheng /* Clean up notification thread */ 4498275SEric Cheng if (mip->mi_notify_thread != NULL) 4508275SEric Cheng i_mac_notify_exit(mip); 4518275SEric Cheng 4528275SEric Cheng if (mip->mi_info.mi_unicst_addr != NULL) { 4538275SEric Cheng kmem_free(mip->mi_info.mi_unicst_addr, 4548275SEric Cheng mip->mi_type->mt_addr_length); 4558275SEric Cheng mip->mi_info.mi_unicst_addr = NULL; 4568275SEric Cheng } 4578275SEric Cheng 4588275SEric Cheng mac_stat_destroy(mip); 4598275SEric Cheng 4608275SEric Cheng if (mip->mi_type != NULL) { 4618275SEric Cheng atomic_dec_32(&mip->mi_type->mt_ref); 4628275SEric Cheng mip->mi_type = NULL; 4638275SEric Cheng } 4648275SEric Cheng 4658275SEric Cheng if (mip->mi_pdata != NULL) { 4668275SEric Cheng kmem_free(mip->mi_pdata, mip->mi_pdata_size); 4678275SEric Cheng mip->mi_pdata = NULL; 4688275SEric Cheng mip->mi_pdata_size = 0; 4698275SEric Cheng } 4708275SEric Cheng 4718275SEric Cheng if (minor != 0) { 4728275SEric Cheng ASSERT(minor > MAC_MAX_MINOR); 4738275SEric Cheng mac_minor_rele(minor); 4748275SEric Cheng } 4758275SEric Cheng 4768275SEric Cheng mac_unregister_priv_prop(mip); 4778275SEric Cheng 4788275SEric Cheng kmem_cache_free(i_mac_impl_cachep, mip); 4798275SEric Cheng return (err); 4808275SEric Cheng } 4818275SEric Cheng 4828275SEric Cheng /* 4838275SEric Cheng * Unregister from the GLDv3 framework 4848275SEric Cheng */ 4858275SEric Cheng int 4868275SEric Cheng mac_unregister(mac_handle_t mh) 4878275SEric Cheng { 4888275SEric Cheng int err; 4898275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 4908275SEric Cheng mod_hash_val_t val; 4918275SEric Cheng mac_margin_req_t *mmr, *nextmmr; 4928275SEric Cheng 4938275SEric Cheng /* Fail the unregister if there are any open references to this mac. */ 4948275SEric Cheng if ((err = mac_disable_nowait(mh)) != 0) 4958275SEric Cheng return (err); 4968275SEric Cheng 4978275SEric Cheng /* 4988275SEric Cheng * Clean up notification thread and wait for it to exit. 4998275SEric Cheng */ 5008275SEric Cheng i_mac_notify_exit(mip); 5018275SEric Cheng 5028275SEric Cheng i_mac_perim_enter(mip); 5038275SEric Cheng 5049073SCathy.Zhou@Sun.COM /* 5059073SCathy.Zhou@Sun.COM * There is still resource properties configured over this mac. 5069073SCathy.Zhou@Sun.COM */ 5079073SCathy.Zhou@Sun.COM if (mip->mi_resource_props.mrp_mask != 0) 5089073SCathy.Zhou@Sun.COM mac_fastpath_enable((mac_handle_t)mip); 5099073SCathy.Zhou@Sun.COM 5108275SEric Cheng if (mip->mi_minor < MAC_MAX_MINOR + 1) { 5118275SEric Cheng ddi_remove_minor_node(mip->mi_dip, mip->mi_name); 5128275SEric Cheng ddi_remove_minor_node(mip->mi_dip, 5138275SEric Cheng (char *)ddi_driver_name(mip->mi_dip)); 5148275SEric Cheng } 5158275SEric Cheng 5168275SEric Cheng ASSERT(mip->mi_nactiveclients == 0 && !(mip->mi_state_flags & 5178275SEric Cheng MIS_EXCLUSIVE)); 5188275SEric Cheng 5198275SEric Cheng mac_stat_destroy(mip); 5208275SEric Cheng 5218275SEric Cheng (void) mod_hash_remove(i_mac_impl_hash, 5228275SEric Cheng (mod_hash_key_t)mip->mi_name, &val); 5238275SEric Cheng ASSERT(mip == (mac_impl_t *)val); 5248275SEric Cheng 5258275SEric Cheng ASSERT(i_mac_impl_count > 0); 5268275SEric Cheng atomic_dec_32(&i_mac_impl_count); 5278275SEric Cheng 5288275SEric Cheng if (mip->mi_pdata != NULL) 5298275SEric Cheng kmem_free(mip->mi_pdata, mip->mi_pdata_size); 5308275SEric Cheng mip->mi_pdata = NULL; 5318275SEric Cheng mip->mi_pdata_size = 0; 5328275SEric Cheng 5338275SEric Cheng /* 5348275SEric Cheng * Free the list of margin request. 5358275SEric Cheng */ 5368275SEric Cheng for (mmr = mip->mi_mmrp; mmr != NULL; mmr = nextmmr) { 5378275SEric Cheng nextmmr = mmr->mmr_nextp; 5388275SEric Cheng kmem_free(mmr, sizeof (mac_margin_req_t)); 5398275SEric Cheng } 5408275SEric Cheng mip->mi_mmrp = NULL; 5418275SEric Cheng 542*10491SRishi.Srivatsavai@Sun.COM mip->mi_linkstate = mip->mi_lowlinkstate = LINK_STATE_UNKNOWN; 5438275SEric Cheng kmem_free(mip->mi_info.mi_unicst_addr, mip->mi_type->mt_addr_length); 5448275SEric Cheng mip->mi_info.mi_unicst_addr = NULL; 5458275SEric Cheng 5468275SEric Cheng atomic_dec_32(&mip->mi_type->mt_ref); 5478275SEric Cheng mip->mi_type = NULL; 5488275SEric Cheng 5498275SEric Cheng /* 5508275SEric Cheng * Free the primary MAC address. 5518275SEric Cheng */ 5528275SEric Cheng mac_fini_macaddr(mip); 5538275SEric Cheng 5548275SEric Cheng /* 5558275SEric Cheng * free all rings 5568275SEric Cheng */ 5578275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_RX); 5588275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_TX); 5598275SEric Cheng 5608275SEric Cheng mac_addr_factory_fini(mip); 5618275SEric Cheng 5628275SEric Cheng bzero(mip->mi_addr, MAXMACADDRLEN); 5638275SEric Cheng bzero(mip->mi_dstaddr, MAXMACADDRLEN); 5648275SEric Cheng 5658275SEric Cheng /* and the flows */ 5668275SEric Cheng mac_flow_tab_destroy(mip->mi_flow_tab); 5678275SEric Cheng mip->mi_flow_tab = NULL; 5688275SEric Cheng 5698275SEric Cheng if (mip->mi_minor > MAC_MAX_MINOR) 5708275SEric Cheng mac_minor_rele(mip->mi_minor); 5718275SEric Cheng 5728275SEric Cheng cmn_err(CE_NOTE, "!%s unregistered", mip->mi_name); 5738275SEric Cheng 5748275SEric Cheng /* 5758275SEric Cheng * Reset the perim related fields to default values before 5768275SEric Cheng * kmem_cache_free 5778275SEric Cheng */ 5788275SEric Cheng i_mac_perim_exit(mip); 5798275SEric Cheng mip->mi_state_flags = 0; 5808275SEric Cheng 5818275SEric Cheng mac_unregister_priv_prop(mip); 582*10491SRishi.Srivatsavai@Sun.COM 583*10491SRishi.Srivatsavai@Sun.COM ASSERT(mip->mi_bridge_link == NULL); 5848275SEric Cheng kmem_cache_free(i_mac_impl_cachep, mip); 5858275SEric Cheng 5868275SEric Cheng return (0); 5878275SEric Cheng } 5888275SEric Cheng 5898275SEric Cheng /* DATA RECEPTION */ 5908275SEric Cheng 5918275SEric Cheng /* 5928275SEric Cheng * This function is invoked for packets received by the MAC driver in 5938275SEric Cheng * interrupt context. The ring generation number provided by the driver 5948275SEric Cheng * is matched with the ring generation number held in MAC. If they do not 5958275SEric Cheng * match, received packets are considered stale packets coming from an older 5968275SEric Cheng * assignment of the ring. Drop them. 5978275SEric Cheng */ 5988275SEric Cheng void 5998275SEric Cheng mac_rx_ring(mac_handle_t mh, mac_ring_handle_t mrh, mblk_t *mp_chain, 6008275SEric Cheng uint64_t mr_gen_num) 6018275SEric Cheng { 6028275SEric Cheng mac_ring_t *mr = (mac_ring_t *)mrh; 6038275SEric Cheng 6048275SEric Cheng if ((mr != NULL) && (mr->mr_gen_num != mr_gen_num)) { 6058275SEric Cheng DTRACE_PROBE2(mac__rx__rings__stale__packet, uint64_t, 6068275SEric Cheng mr->mr_gen_num, uint64_t, mr_gen_num); 6078275SEric Cheng freemsgchain(mp_chain); 6088275SEric Cheng return; 6098275SEric Cheng } 6108275SEric Cheng mac_rx(mh, (mac_resource_handle_t)mrh, mp_chain); 6118275SEric Cheng } 6128275SEric Cheng 6138275SEric Cheng /* 614*10491SRishi.Srivatsavai@Sun.COM * This function is invoked for each packet received by the underlying driver. 6158275SEric Cheng */ 6168275SEric Cheng void 6178275SEric Cheng mac_rx(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain) 6188275SEric Cheng { 619*10491SRishi.Srivatsavai@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 620*10491SRishi.Srivatsavai@Sun.COM 621*10491SRishi.Srivatsavai@Sun.COM /* 622*10491SRishi.Srivatsavai@Sun.COM * Check if the link is part of a bridge. If not, then we don't need 623*10491SRishi.Srivatsavai@Sun.COM * to take the lock to remain consistent. Make this common case 624*10491SRishi.Srivatsavai@Sun.COM * lock-free and tail-call optimized. 625*10491SRishi.Srivatsavai@Sun.COM */ 626*10491SRishi.Srivatsavai@Sun.COM if (mip->mi_bridge_link == NULL) { 627*10491SRishi.Srivatsavai@Sun.COM mac_rx_common(mh, mrh, mp_chain); 628*10491SRishi.Srivatsavai@Sun.COM } else { 629*10491SRishi.Srivatsavai@Sun.COM /* 630*10491SRishi.Srivatsavai@Sun.COM * Once we take a reference on the bridge link, the bridge 631*10491SRishi.Srivatsavai@Sun.COM * module itself can't unload, so the callback pointers are 632*10491SRishi.Srivatsavai@Sun.COM * stable. 633*10491SRishi.Srivatsavai@Sun.COM */ 634*10491SRishi.Srivatsavai@Sun.COM mutex_enter(&mip->mi_bridge_lock); 635*10491SRishi.Srivatsavai@Sun.COM if ((mh = mip->mi_bridge_link) != NULL) 636*10491SRishi.Srivatsavai@Sun.COM mac_bridge_ref_cb(mh, B_TRUE); 637*10491SRishi.Srivatsavai@Sun.COM mutex_exit(&mip->mi_bridge_lock); 638*10491SRishi.Srivatsavai@Sun.COM if (mh == NULL) { 639*10491SRishi.Srivatsavai@Sun.COM mac_rx_common((mac_handle_t)mip, mrh, mp_chain); 640*10491SRishi.Srivatsavai@Sun.COM } else { 641*10491SRishi.Srivatsavai@Sun.COM mac_bridge_rx_cb(mh, mrh, mp_chain); 642*10491SRishi.Srivatsavai@Sun.COM mac_bridge_ref_cb(mh, B_FALSE); 643*10491SRishi.Srivatsavai@Sun.COM } 644*10491SRishi.Srivatsavai@Sun.COM } 645*10491SRishi.Srivatsavai@Sun.COM } 646*10491SRishi.Srivatsavai@Sun.COM 647*10491SRishi.Srivatsavai@Sun.COM /* 648*10491SRishi.Srivatsavai@Sun.COM * Special case function: this allows snooping of packets transmitted and 649*10491SRishi.Srivatsavai@Sun.COM * received by TRILL. By design, they go directly into the TRILL module. 650*10491SRishi.Srivatsavai@Sun.COM */ 651*10491SRishi.Srivatsavai@Sun.COM void 652*10491SRishi.Srivatsavai@Sun.COM mac_trill_snoop(mac_handle_t mh, mblk_t *mp) 653*10491SRishi.Srivatsavai@Sun.COM { 654*10491SRishi.Srivatsavai@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 655*10491SRishi.Srivatsavai@Sun.COM 656*10491SRishi.Srivatsavai@Sun.COM if (mip->mi_promisc_list != NULL) 657*10491SRishi.Srivatsavai@Sun.COM mac_promisc_dispatch(mip, mp, NULL); 658*10491SRishi.Srivatsavai@Sun.COM } 659*10491SRishi.Srivatsavai@Sun.COM 660*10491SRishi.Srivatsavai@Sun.COM /* 661*10491SRishi.Srivatsavai@Sun.COM * This is the upward reentry point for packets arriving from the bridging 662*10491SRishi.Srivatsavai@Sun.COM * module and from mac_rx for links not part of a bridge. 663*10491SRishi.Srivatsavai@Sun.COM */ 664*10491SRishi.Srivatsavai@Sun.COM void 665*10491SRishi.Srivatsavai@Sun.COM mac_rx_common(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain) 666*10491SRishi.Srivatsavai@Sun.COM { 6678275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 6688275SEric Cheng mac_ring_t *mr = (mac_ring_t *)mrh; 6698275SEric Cheng mac_soft_ring_set_t *mac_srs; 6708275SEric Cheng mblk_t *bp = mp_chain; 6718275SEric Cheng boolean_t hw_classified = B_FALSE; 6728275SEric Cheng 6738275SEric Cheng /* 6748275SEric Cheng * If there are any promiscuous mode callbacks defined for 6758275SEric Cheng * this MAC, pass them a copy if appropriate. 6768275SEric Cheng */ 6778275SEric Cheng if (mip->mi_promisc_list != NULL) 6788275SEric Cheng mac_promisc_dispatch(mip, mp_chain, NULL); 6798275SEric Cheng 6808275SEric Cheng if (mr != NULL) { 6818275SEric Cheng /* 6828275SEric Cheng * If the SRS teardown has started, just return. The 'mr' 6838275SEric Cheng * continues to be valid until the driver unregisters the mac. 6848275SEric Cheng * Hardware classified packets will not make their way up 6858275SEric Cheng * beyond this point once the teardown has started. The driver 6868275SEric Cheng * is never passed a pointer to a flow entry or SRS or any 6878275SEric Cheng * structure that can be freed much before mac_unregister. 6888275SEric Cheng */ 6898275SEric Cheng mutex_enter(&mr->mr_lock); 6908275SEric Cheng if ((mr->mr_state != MR_INUSE) || (mr->mr_flag & 6918275SEric Cheng (MR_INCIPIENT | MR_CONDEMNED | MR_QUIESCE))) { 6928275SEric Cheng mutex_exit(&mr->mr_lock); 6938275SEric Cheng freemsgchain(mp_chain); 6948275SEric Cheng return; 6958275SEric Cheng } 6968275SEric Cheng if (mr->mr_classify_type == MAC_HW_CLASSIFIER) { 6978275SEric Cheng hw_classified = B_TRUE; 6988275SEric Cheng MR_REFHOLD_LOCKED(mr); 6998275SEric Cheng } 7008275SEric Cheng mutex_exit(&mr->mr_lock); 7018275SEric Cheng 7028275SEric Cheng /* 7038275SEric Cheng * We check if an SRS is controlling this ring. 7048275SEric Cheng * If so, we can directly call the srs_lower_proc 7058275SEric Cheng * routine otherwise we need to go through mac_rx_classify 7068275SEric Cheng * to reach the right place. 7078275SEric Cheng */ 7088275SEric Cheng if (hw_classified) { 7098275SEric Cheng mac_srs = mr->mr_srs; 7108275SEric Cheng /* 7118275SEric Cheng * This is supposed to be the fast path. 7128275SEric Cheng * All packets received though here were steered by 7138275SEric Cheng * the hardware classifier, and share the same 7148275SEric Cheng * MAC header info. 7158275SEric Cheng */ 7168275SEric Cheng mac_srs->srs_rx.sr_lower_proc(mh, 7178275SEric Cheng (mac_resource_handle_t)mac_srs, mp_chain, B_FALSE); 7188275SEric Cheng MR_REFRELE(mr); 7198275SEric Cheng return; 7208275SEric Cheng } 7218275SEric Cheng /* We'll fall through to software classification */ 7228833SVenu.Iyer@Sun.COM } else { 7238833SVenu.Iyer@Sun.COM flow_entry_t *flent; 7248833SVenu.Iyer@Sun.COM int err; 7258833SVenu.Iyer@Sun.COM 7268833SVenu.Iyer@Sun.COM rw_enter(&mip->mi_rw_lock, RW_READER); 7278833SVenu.Iyer@Sun.COM if (mip->mi_single_active_client != NULL) { 7288833SVenu.Iyer@Sun.COM flent = mip->mi_single_active_client->mci_flent_list; 7298833SVenu.Iyer@Sun.COM FLOW_TRY_REFHOLD(flent, err); 7308833SVenu.Iyer@Sun.COM rw_exit(&mip->mi_rw_lock); 7318833SVenu.Iyer@Sun.COM if (err == 0) { 7328833SVenu.Iyer@Sun.COM (flent->fe_cb_fn)(flent->fe_cb_arg1, 7338833SVenu.Iyer@Sun.COM flent->fe_cb_arg2, mp_chain, B_FALSE); 7348833SVenu.Iyer@Sun.COM FLOW_REFRELE(flent); 7358833SVenu.Iyer@Sun.COM return; 7368833SVenu.Iyer@Sun.COM } 7378833SVenu.Iyer@Sun.COM } else { 7388833SVenu.Iyer@Sun.COM rw_exit(&mip->mi_rw_lock); 7398833SVenu.Iyer@Sun.COM } 7408275SEric Cheng } 7418275SEric Cheng 7428275SEric Cheng if (!FLOW_TAB_EMPTY(mip->mi_flow_tab)) { 7438275SEric Cheng if ((bp = mac_rx_flow(mh, mrh, bp)) == NULL) 7448275SEric Cheng return; 7458275SEric Cheng } 7468275SEric Cheng 7478275SEric Cheng freemsgchain(bp); 7488275SEric Cheng } 7498275SEric Cheng 7508275SEric Cheng /* DATA TRANSMISSION */ 7518275SEric Cheng 7528275SEric Cheng /* 7538275SEric Cheng * A driver's notification to resume transmission, in case of a provider 7548275SEric Cheng * without TX rings. 7558275SEric Cheng */ 7568275SEric Cheng void 7578275SEric Cheng mac_tx_update(mac_handle_t mh) 7588275SEric Cheng { 7598275SEric Cheng /* 7608275SEric Cheng * Walk the list of MAC clients (mac_client_handle) 7618275SEric Cheng * and update 7628275SEric Cheng */ 7638275SEric Cheng i_mac_tx_srs_notify((mac_impl_t *)mh, NULL); 7648275SEric Cheng } 7658275SEric Cheng 7668275SEric Cheng /* 7678275SEric Cheng * A driver's notification to resume transmission on the specified TX ring. 7688275SEric Cheng */ 7698275SEric Cheng void 7708275SEric Cheng mac_tx_ring_update(mac_handle_t mh, mac_ring_handle_t rh) 7718275SEric Cheng { 7728275SEric Cheng i_mac_tx_srs_notify((mac_impl_t *)mh, rh); 7738275SEric Cheng } 7748275SEric Cheng 7758275SEric Cheng /* LINK STATE */ 7768275SEric Cheng /* 7778275SEric Cheng * Notify the MAC layer about a link state change 7788275SEric Cheng */ 7798275SEric Cheng void 7808275SEric Cheng mac_link_update(mac_handle_t mh, link_state_t link) 7818275SEric Cheng { 7828275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 7838275SEric Cheng 7848275SEric Cheng /* 7858275SEric Cheng * Save the link state. 7868275SEric Cheng */ 787*10491SRishi.Srivatsavai@Sun.COM mip->mi_lowlinkstate = link; 788*10491SRishi.Srivatsavai@Sun.COM 789*10491SRishi.Srivatsavai@Sun.COM /* 790*10491SRishi.Srivatsavai@Sun.COM * Send a MAC_NOTE_LOWLINK notification. This tells the notification 791*10491SRishi.Srivatsavai@Sun.COM * thread to deliver both lower and upper notifications. 792*10491SRishi.Srivatsavai@Sun.COM */ 793*10491SRishi.Srivatsavai@Sun.COM i_mac_notify(mip, MAC_NOTE_LOWLINK); 794*10491SRishi.Srivatsavai@Sun.COM } 795*10491SRishi.Srivatsavai@Sun.COM 796*10491SRishi.Srivatsavai@Sun.COM /* 797*10491SRishi.Srivatsavai@Sun.COM * Notify the MAC layer about a link state change due to bridging. 798*10491SRishi.Srivatsavai@Sun.COM */ 799*10491SRishi.Srivatsavai@Sun.COM void 800*10491SRishi.Srivatsavai@Sun.COM mac_link_redo(mac_handle_t mh, link_state_t link) 801*10491SRishi.Srivatsavai@Sun.COM { 802*10491SRishi.Srivatsavai@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 803*10491SRishi.Srivatsavai@Sun.COM 804*10491SRishi.Srivatsavai@Sun.COM /* 805*10491SRishi.Srivatsavai@Sun.COM * Save the link state. 806*10491SRishi.Srivatsavai@Sun.COM */ 8078275SEric Cheng mip->mi_linkstate = link; 8088275SEric Cheng 8098275SEric Cheng /* 810*10491SRishi.Srivatsavai@Sun.COM * Send a MAC_NOTE_LINK notification. Only upper notifications are 811*10491SRishi.Srivatsavai@Sun.COM * made. 8128275SEric Cheng */ 8138275SEric Cheng i_mac_notify(mip, MAC_NOTE_LINK); 8148275SEric Cheng } 8158275SEric Cheng 8168275SEric Cheng /* OTHER CONTROL INFORMATION */ 8178275SEric Cheng 8188275SEric Cheng /* 8198275SEric Cheng * A driver notified us that its primary MAC address has changed. 8208275SEric Cheng */ 8218275SEric Cheng void 8228275SEric Cheng mac_unicst_update(mac_handle_t mh, const uint8_t *addr) 8238275SEric Cheng { 8248275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 8258275SEric Cheng 8268275SEric Cheng if (mip->mi_type->mt_addr_length == 0) 8278275SEric Cheng return; 8288275SEric Cheng 8298275SEric Cheng i_mac_perim_enter(mip); 8308275SEric Cheng /* 8318275SEric Cheng * If address doesn't change, do nothing. 8328275SEric Cheng */ 8338275SEric Cheng if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) { 8348275SEric Cheng i_mac_perim_exit(mip); 8358275SEric Cheng return; 8368275SEric Cheng } 8378275SEric Cheng 8388275SEric Cheng /* 8398275SEric Cheng * Freshen the MAC address value and update all MAC clients that 8408275SEric Cheng * share this MAC address. 8418275SEric Cheng */ 8428275SEric Cheng mac_freshen_macaddr(mac_find_macaddr(mip, mip->mi_addr), 8438275SEric Cheng (uint8_t *)addr); 8448275SEric Cheng 8458275SEric Cheng i_mac_perim_exit(mip); 8468275SEric Cheng 8478275SEric Cheng /* 8488275SEric Cheng * Send a MAC_NOTE_UNICST notification. 8498275SEric Cheng */ 8508275SEric Cheng i_mac_notify(mip, MAC_NOTE_UNICST); 8518275SEric Cheng } 8528275SEric Cheng 8538275SEric Cheng /* 8548275SEric Cheng * MAC plugin information changed. 8558275SEric Cheng */ 8568275SEric Cheng int 8578275SEric Cheng mac_pdata_update(mac_handle_t mh, void *mac_pdata, size_t dsize) 8588275SEric Cheng { 8598275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 8608275SEric Cheng 8618275SEric Cheng /* 8628275SEric Cheng * Verify that the plugin supports MAC plugin data and that the 8638275SEric Cheng * supplied data is valid. 8648275SEric Cheng */ 8658275SEric Cheng if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY)) 8668275SEric Cheng return (EINVAL); 8678275SEric Cheng if (!mip->mi_type->mt_ops.mtops_pdata_verify(mac_pdata, dsize)) 8688275SEric Cheng return (EINVAL); 8698275SEric Cheng 8708275SEric Cheng if (mip->mi_pdata != NULL) 8718275SEric Cheng kmem_free(mip->mi_pdata, mip->mi_pdata_size); 8728275SEric Cheng 8738275SEric Cheng mip->mi_pdata = kmem_alloc(dsize, KM_SLEEP); 8748275SEric Cheng bcopy(mac_pdata, mip->mi_pdata, dsize); 8758275SEric Cheng mip->mi_pdata_size = dsize; 8768275SEric Cheng 8778275SEric Cheng /* 8788275SEric Cheng * Since the MAC plugin data is used to construct MAC headers that 8798275SEric Cheng * were cached in fast-path headers, we need to flush fast-path 8808275SEric Cheng * information for links associated with this mac. 8818275SEric Cheng */ 8828275SEric Cheng i_mac_notify(mip, MAC_NOTE_FASTPATH_FLUSH); 8838275SEric Cheng return (0); 8848275SEric Cheng } 8858275SEric Cheng 8868275SEric Cheng /* 8878275SEric Cheng * Invoked by driver as well as the framework to notify its capability change. 8888275SEric Cheng */ 8898275SEric Cheng void 8908275SEric Cheng mac_capab_update(mac_handle_t mh) 8918275SEric Cheng { 8928275SEric Cheng /* Send MAC_NOTE_CAPAB_CHG notification */ 8938275SEric Cheng i_mac_notify((mac_impl_t *)mh, MAC_NOTE_CAPAB_CHG); 8948275SEric Cheng } 8958275SEric Cheng 8968275SEric Cheng int 8978275SEric Cheng mac_maxsdu_update(mac_handle_t mh, uint_t sdu_max) 8988275SEric Cheng { 8998275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 9008275SEric Cheng 9019514SGirish.Moodalbail@Sun.COM if (sdu_max == 0 || sdu_max < mip->mi_sdu_min) 9028275SEric Cheng return (EINVAL); 9038275SEric Cheng mip->mi_sdu_max = sdu_max; 9048275SEric Cheng 9058275SEric Cheng /* Send a MAC_NOTE_SDU_SIZE notification. */ 9068275SEric Cheng i_mac_notify(mip, MAC_NOTE_SDU_SIZE); 9078275SEric Cheng return (0); 9088275SEric Cheng } 9098275SEric Cheng 9108275SEric Cheng /* PRIVATE FUNCTIONS, FOR INTERNAL USE ONLY */ 9118275SEric Cheng 9128275SEric Cheng /* 9138275SEric Cheng * Updates the mac_impl structure with the current state of the link 9148275SEric Cheng */ 9158275SEric Cheng static void 9168275SEric Cheng i_mac_log_link_state(mac_impl_t *mip) 9178275SEric Cheng { 9188275SEric Cheng /* 9198275SEric Cheng * If no change, then it is not interesting. 9208275SEric Cheng */ 921*10491SRishi.Srivatsavai@Sun.COM if (mip->mi_lastlowlinkstate == mip->mi_lowlinkstate) 9228275SEric Cheng return; 9238275SEric Cheng 924*10491SRishi.Srivatsavai@Sun.COM switch (mip->mi_lowlinkstate) { 9258275SEric Cheng case LINK_STATE_UP: 9268275SEric Cheng if (mip->mi_type->mt_ops.mtops_ops & MTOPS_LINK_DETAILS) { 9278275SEric Cheng char det[200]; 9288275SEric Cheng 9298275SEric Cheng mip->mi_type->mt_ops.mtops_link_details(det, 9308275SEric Cheng sizeof (det), (mac_handle_t)mip, mip->mi_pdata); 9318275SEric Cheng 9328275SEric Cheng cmn_err(CE_NOTE, "!%s link up, %s", mip->mi_name, det); 9338275SEric Cheng } else { 9348275SEric Cheng cmn_err(CE_NOTE, "!%s link up", mip->mi_name); 9358275SEric Cheng } 9368275SEric Cheng break; 9378275SEric Cheng 9388275SEric Cheng case LINK_STATE_DOWN: 9398275SEric Cheng /* 9408275SEric Cheng * Only transitions from UP to DOWN are interesting 9418275SEric Cheng */ 942*10491SRishi.Srivatsavai@Sun.COM if (mip->mi_lastlowlinkstate != LINK_STATE_UNKNOWN) 9438275SEric Cheng cmn_err(CE_NOTE, "!%s link down", mip->mi_name); 9448275SEric Cheng break; 9458275SEric Cheng 9468275SEric Cheng case LINK_STATE_UNKNOWN: 9478275SEric Cheng /* 9488275SEric Cheng * This case is normally not interesting. 9498275SEric Cheng */ 9508275SEric Cheng break; 9518275SEric Cheng } 952*10491SRishi.Srivatsavai@Sun.COM mip->mi_lastlowlinkstate = mip->mi_lowlinkstate; 9538275SEric Cheng } 9548275SEric Cheng 9558275SEric Cheng /* 9568275SEric Cheng * Main routine for the callbacks notifications thread 9578275SEric Cheng */ 9588275SEric Cheng static void 9598275SEric Cheng i_mac_notify_thread(void *arg) 9608275SEric Cheng { 9618275SEric Cheng mac_impl_t *mip = arg; 9628275SEric Cheng callb_cpr_t cprinfo; 9638275SEric Cheng mac_cb_t *mcb; 9648275SEric Cheng mac_cb_info_t *mcbi; 9658275SEric Cheng mac_notify_cb_t *mncb; 9668275SEric Cheng 9678275SEric Cheng mcbi = &mip->mi_notify_cb_info; 9688275SEric Cheng CALLB_CPR_INIT(&cprinfo, mcbi->mcbi_lockp, callb_generic_cpr, 9698275SEric Cheng "i_mac_notify_thread"); 9708275SEric Cheng 9718275SEric Cheng mutex_enter(mcbi->mcbi_lockp); 9728275SEric Cheng 9738275SEric Cheng for (;;) { 9748275SEric Cheng uint32_t bits; 9758275SEric Cheng uint32_t type; 9768275SEric Cheng 9778275SEric Cheng bits = mip->mi_notify_bits; 9788275SEric Cheng if (bits == 0) { 9798275SEric Cheng CALLB_CPR_SAFE_BEGIN(&cprinfo); 9808275SEric Cheng cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 9818275SEric Cheng CALLB_CPR_SAFE_END(&cprinfo, mcbi->mcbi_lockp); 9828275SEric Cheng continue; 9838275SEric Cheng } 9848275SEric Cheng mip->mi_notify_bits = 0; 9858275SEric Cheng if ((bits & (1 << MAC_NNOTE)) != 0) { 9868275SEric Cheng /* request to quit */ 9878275SEric Cheng ASSERT(mip->mi_state_flags & MIS_DISABLED); 9888275SEric Cheng break; 9898275SEric Cheng } 9908275SEric Cheng 9918275SEric Cheng mutex_exit(mcbi->mcbi_lockp); 9928275SEric Cheng 9938275SEric Cheng /* 994*10491SRishi.Srivatsavai@Sun.COM * Log link changes on the actual link, but then do reports on 995*10491SRishi.Srivatsavai@Sun.COM * synthetic state (if part of a bridge). 9968275SEric Cheng */ 997*10491SRishi.Srivatsavai@Sun.COM if ((bits & (1 << MAC_NOTE_LOWLINK)) != 0) { 998*10491SRishi.Srivatsavai@Sun.COM link_state_t newstate; 999*10491SRishi.Srivatsavai@Sun.COM mac_handle_t mh; 1000*10491SRishi.Srivatsavai@Sun.COM 10018275SEric Cheng i_mac_log_link_state(mip); 1002*10491SRishi.Srivatsavai@Sun.COM newstate = mip->mi_lowlinkstate; 1003*10491SRishi.Srivatsavai@Sun.COM if (mip->mi_bridge_link != NULL) { 1004*10491SRishi.Srivatsavai@Sun.COM mutex_enter(&mip->mi_bridge_lock); 1005*10491SRishi.Srivatsavai@Sun.COM if ((mh = mip->mi_bridge_link) != NULL) { 1006*10491SRishi.Srivatsavai@Sun.COM newstate = mac_bridge_ls_cb(mh, 1007*10491SRishi.Srivatsavai@Sun.COM newstate); 1008*10491SRishi.Srivatsavai@Sun.COM } 1009*10491SRishi.Srivatsavai@Sun.COM mutex_exit(&mip->mi_bridge_lock); 1010*10491SRishi.Srivatsavai@Sun.COM } 1011*10491SRishi.Srivatsavai@Sun.COM if (newstate != mip->mi_linkstate) { 1012*10491SRishi.Srivatsavai@Sun.COM mip->mi_linkstate = newstate; 1013*10491SRishi.Srivatsavai@Sun.COM bits |= 1 << MAC_NOTE_LINK; 1014*10491SRishi.Srivatsavai@Sun.COM } 1015*10491SRishi.Srivatsavai@Sun.COM } 10168275SEric Cheng 10178275SEric Cheng /* 10188275SEric Cheng * Do notification callbacks for each notification type. 10198275SEric Cheng */ 10208275SEric Cheng for (type = 0; type < MAC_NNOTE; type++) { 10218275SEric Cheng if ((bits & (1 << type)) == 0) { 10228275SEric Cheng continue; 10238275SEric Cheng } 10248275SEric Cheng 1025*10491SRishi.Srivatsavai@Sun.COM if (mac_notify_cb_list[type] != NULL) 1026*10491SRishi.Srivatsavai@Sun.COM (*mac_notify_cb_list[type])(mip); 10278275SEric Cheng 10288275SEric Cheng /* 10298275SEric Cheng * Walk the list of notifications. 10308275SEric Cheng */ 10318275SEric Cheng MAC_CALLBACK_WALKER_INC(&mip->mi_notify_cb_info); 10328275SEric Cheng for (mcb = mip->mi_notify_cb_list; mcb != NULL; 10338275SEric Cheng mcb = mcb->mcb_nextp) { 10348275SEric Cheng mncb = (mac_notify_cb_t *)mcb->mcb_objp; 10358275SEric Cheng mncb->mncb_fn(mncb->mncb_arg, type); 10368275SEric Cheng } 10378275SEric Cheng MAC_CALLBACK_WALKER_DCR(&mip->mi_notify_cb_info, 10388275SEric Cheng &mip->mi_notify_cb_list); 10398275SEric Cheng } 10408275SEric Cheng 10418275SEric Cheng mutex_enter(mcbi->mcbi_lockp); 10428275SEric Cheng } 10438275SEric Cheng 10448275SEric Cheng mip->mi_state_flags |= MIS_NOTIFY_DONE; 10458275SEric Cheng cv_broadcast(&mcbi->mcbi_cv); 10468275SEric Cheng 10478275SEric Cheng /* CALLB_CPR_EXIT drops the lock */ 10488275SEric Cheng CALLB_CPR_EXIT(&cprinfo); 10498275SEric Cheng thread_exit(); 10508275SEric Cheng } 10518275SEric Cheng 10528275SEric Cheng /* 10538275SEric Cheng * Signal the i_mac_notify_thread asking it to quit. 10548275SEric Cheng * Then wait till it is done. 10558275SEric Cheng */ 10568275SEric Cheng void 10578275SEric Cheng i_mac_notify_exit(mac_impl_t *mip) 10588275SEric Cheng { 10598275SEric Cheng mac_cb_info_t *mcbi; 10608275SEric Cheng 10618275SEric Cheng mcbi = &mip->mi_notify_cb_info; 10628275SEric Cheng 10638275SEric Cheng mutex_enter(mcbi->mcbi_lockp); 10648275SEric Cheng mip->mi_notify_bits = (1 << MAC_NNOTE); 10658275SEric Cheng cv_broadcast(&mcbi->mcbi_cv); 10668275SEric Cheng 10678275SEric Cheng 10688275SEric Cheng while ((mip->mi_notify_thread != NULL) && 10698275SEric Cheng !(mip->mi_state_flags & MIS_NOTIFY_DONE)) { 10708275SEric Cheng cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 10718275SEric Cheng } 10728275SEric Cheng 10738275SEric Cheng /* Necessary clean up before doing kmem_cache_free */ 10748275SEric Cheng mip->mi_state_flags &= ~MIS_NOTIFY_DONE; 10758275SEric Cheng mip->mi_notify_bits = 0; 10768275SEric Cheng mip->mi_notify_thread = NULL; 10778275SEric Cheng mutex_exit(mcbi->mcbi_lockp); 10788275SEric Cheng } 10798275SEric Cheng 10808275SEric Cheng /* 10818275SEric Cheng * Entry point invoked by drivers to dynamically add a ring to an 10828275SEric Cheng * existing group. 10838275SEric Cheng */ 10848275SEric Cheng int 10858275SEric Cheng mac_group_add_ring(mac_group_handle_t gh, int index) 10868275SEric Cheng { 10878275SEric Cheng mac_group_t *group = (mac_group_t *)gh; 10888275SEric Cheng mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 10898275SEric Cheng int ret; 10908275SEric Cheng 10918275SEric Cheng i_mac_perim_enter(mip); 10928275SEric Cheng 10938275SEric Cheng /* 10948275SEric Cheng * Only RX rings can be added or removed by drivers currently. 10958275SEric Cheng */ 10968275SEric Cheng ASSERT(group->mrg_type == MAC_RING_TYPE_RX); 10978275SEric Cheng 10988275SEric Cheng ret = i_mac_group_add_ring(group, NULL, index); 10998275SEric Cheng 11008275SEric Cheng i_mac_perim_exit(mip); 11018275SEric Cheng 11028275SEric Cheng return (ret); 11038275SEric Cheng } 11048275SEric Cheng 11058275SEric Cheng /* 11068275SEric Cheng * Entry point invoked by drivers to dynamically remove a ring 11078275SEric Cheng * from an existing group. The specified ring handle must no longer 11088275SEric Cheng * be used by the driver after a call to this function. 11098275SEric Cheng */ 11108275SEric Cheng void 11118275SEric Cheng mac_group_rem_ring(mac_group_handle_t gh, mac_ring_handle_t rh) 11128275SEric Cheng { 11138275SEric Cheng mac_group_t *group = (mac_group_t *)gh; 11148275SEric Cheng mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 11158275SEric Cheng 11168275SEric Cheng i_mac_perim_enter(mip); 11178275SEric Cheng 11188275SEric Cheng /* 11198275SEric Cheng * Only RX rings can be added or removed by drivers currently. 11208275SEric Cheng */ 11218275SEric Cheng ASSERT(group->mrg_type == MAC_RING_TYPE_RX); 11228275SEric Cheng 11238275SEric Cheng i_mac_group_rem_ring(group, (mac_ring_t *)rh, B_TRUE); 11248275SEric Cheng 11258275SEric Cheng i_mac_perim_exit(mip); 11268275SEric Cheng } 1127