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 /* 2311544SMichael.Speer@Sun.COM * Copyright 2010 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> 4310654SGarrett.Damore@Sun.COM #include <sys/dld.h> 448275SEric Cheng #include <sys/modctl.h> 458275SEric Cheng #include <sys/fs/dv_node.h> 468275SEric Cheng #include <sys/thread.h> 478275SEric Cheng #include <sys/proc.h> 488275SEric Cheng #include <sys/callb.h> 498275SEric Cheng #include <sys/cpuvar.h> 508275SEric Cheng #include <sys/atomic.h> 518275SEric Cheng #include <sys/sdt.h> 528275SEric Cheng #include <sys/mac_flow.h> 538275SEric Cheng #include <sys/ddi_intr_impl.h> 548275SEric Cheng #include <sys/disp.h> 558275SEric Cheng #include <sys/sdt.h> 568275SEric Cheng 578275SEric Cheng /* 588275SEric Cheng * MAC Provider Interface. 598275SEric Cheng * 608275SEric Cheng * Interface for GLDv3 compatible NIC drivers. 618275SEric Cheng */ 628275SEric Cheng 638275SEric Cheng static void i_mac_notify_thread(void *); 648275SEric Cheng 658275SEric Cheng typedef void (*mac_notify_default_cb_fn_t)(mac_impl_t *); 668275SEric Cheng 6710491SRishi.Srivatsavai@Sun.COM static const mac_notify_default_cb_fn_t mac_notify_cb_list[MAC_NNOTE] = { 6810491SRishi.Srivatsavai@Sun.COM mac_fanout_recompute, /* MAC_NOTE_LINK */ 6910491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_UNICST */ 7010491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_TX */ 7110491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_DEVPROMISC */ 7210491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_FASTPATH_FLUSH */ 7310491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_SDU_SIZE */ 7410491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_MARGIN */ 7510491SRishi.Srivatsavai@Sun.COM NULL, /* MAC_NOTE_CAPAB_CHG */ 7610491SRishi.Srivatsavai@Sun.COM NULL /* MAC_NOTE_LOWLINK */ 778275SEric Cheng }; 788275SEric Cheng 798275SEric Cheng /* 808275SEric Cheng * Driver support functions. 818275SEric Cheng */ 828275SEric Cheng 838275SEric Cheng /* REGISTRATION */ 848275SEric Cheng 858275SEric Cheng mac_register_t * 868275SEric Cheng mac_alloc(uint_t mac_version) 878275SEric Cheng { 888275SEric Cheng mac_register_t *mregp; 898275SEric Cheng 908275SEric Cheng /* 918275SEric Cheng * Make sure there isn't a version mismatch between the driver and 928275SEric Cheng * the framework. In the future, if multiple versions are 938275SEric Cheng * supported, this check could become more sophisticated. 948275SEric Cheng */ 958275SEric Cheng if (mac_version != MAC_VERSION) 968275SEric Cheng return (NULL); 978275SEric Cheng 988275SEric Cheng mregp = kmem_zalloc(sizeof (mac_register_t), KM_SLEEP); 998275SEric Cheng mregp->m_version = mac_version; 1008275SEric Cheng return (mregp); 1018275SEric Cheng } 1028275SEric Cheng 1038275SEric Cheng void 1048275SEric Cheng mac_free(mac_register_t *mregp) 1058275SEric Cheng { 1068275SEric Cheng kmem_free(mregp, sizeof (mac_register_t)); 1078275SEric Cheng } 1088275SEric Cheng 1098275SEric Cheng /* 1108275SEric Cheng * mac_register() is how drivers register new MACs with the GLDv3 1118275SEric Cheng * framework. The mregp argument is allocated by drivers using the 1128275SEric Cheng * mac_alloc() function, and can be freed using mac_free() immediately upon 1138275SEric Cheng * return from mac_register(). Upon success (0 return value), the mhp 1148275SEric Cheng * opaque pointer becomes the driver's handle to its MAC interface, and is 1158275SEric Cheng * the argument to all other mac module entry points. 1168275SEric Cheng */ 1178275SEric Cheng /* ARGSUSED */ 1188275SEric Cheng int 1198275SEric Cheng mac_register(mac_register_t *mregp, mac_handle_t *mhp) 1208275SEric Cheng { 1218275SEric Cheng mac_impl_t *mip; 1228275SEric Cheng mactype_t *mtype; 1238275SEric Cheng int err = EINVAL; 1248275SEric Cheng struct devnames *dnp = NULL; 1258275SEric Cheng uint_t instance; 1268275SEric Cheng boolean_t style1_created = B_FALSE; 1278275SEric Cheng boolean_t style2_created = B_FALSE; 1288275SEric Cheng char *driver; 1298275SEric Cheng minor_t minor = 0; 1308275SEric Cheng 13110986SSebastien.Roy@Sun.COM /* A successful call to mac_init_ops() sets the DN_GLDV3_DRIVER flag. */ 13210986SSebastien.Roy@Sun.COM if (!GLDV3_DRV(ddi_driver_major(mregp->m_dip))) 13310986SSebastien.Roy@Sun.COM return (EINVAL); 13410986SSebastien.Roy@Sun.COM 1358275SEric Cheng /* Find the required MAC-Type plugin. */ 1368275SEric Cheng if ((mtype = mactype_getplugin(mregp->m_type_ident)) == NULL) 1378275SEric Cheng return (EINVAL); 1388275SEric Cheng 1398275SEric Cheng /* Create a mac_impl_t to represent this MAC. */ 1408275SEric Cheng mip = kmem_cache_alloc(i_mac_impl_cachep, KM_SLEEP); 1418275SEric Cheng 1428275SEric Cheng /* 1438275SEric Cheng * The mac is not ready for open yet. 1448275SEric Cheng */ 1458275SEric Cheng mip->mi_state_flags |= MIS_DISABLED; 1468275SEric Cheng 1478275SEric Cheng /* 1488275SEric Cheng * When a mac is registered, the m_instance field can be set to: 1498275SEric Cheng * 1508275SEric Cheng * 0: Get the mac's instance number from m_dip. 1518275SEric Cheng * This is usually used for physical device dips. 1528275SEric Cheng * 1538275SEric Cheng * [1 .. MAC_MAX_MINOR-1]: Use the value as the mac's instance number. 1548275SEric Cheng * For example, when an aggregation is created with the key option, 1558275SEric Cheng * "key" will be used as the instance number. 1568275SEric Cheng * 1578275SEric Cheng * -1: Assign an instance number from [MAC_MAX_MINOR .. MAXMIN-1]. 1588275SEric Cheng * This is often used when a MAC of a virtual link is registered 1598275SEric Cheng * (e.g., aggregation when "key" is not specified, or vnic). 1608275SEric Cheng * 1618275SEric Cheng * Note that the instance number is used to derive the mi_minor field 1628275SEric Cheng * of mac_impl_t, which will then be used to derive the name of kstats 1638275SEric Cheng * and the devfs nodes. The first 2 cases are needed to preserve 1648275SEric Cheng * backward compatibility. 1658275SEric Cheng */ 1668275SEric Cheng switch (mregp->m_instance) { 1678275SEric Cheng case 0: 1688275SEric Cheng instance = ddi_get_instance(mregp->m_dip); 1698275SEric Cheng break; 1708275SEric Cheng case ((uint_t)-1): 1718275SEric Cheng minor = mac_minor_hold(B_TRUE); 1728275SEric Cheng if (minor == 0) { 1738275SEric Cheng err = ENOSPC; 1748275SEric Cheng goto fail; 1758275SEric Cheng } 1768275SEric Cheng instance = minor - 1; 1778275SEric Cheng break; 1788275SEric Cheng default: 1798275SEric Cheng instance = mregp->m_instance; 1808275SEric Cheng if (instance >= MAC_MAX_MINOR) { 1818275SEric Cheng err = EINVAL; 1828275SEric Cheng goto fail; 1838275SEric Cheng } 1848275SEric Cheng break; 1858275SEric Cheng } 1868275SEric Cheng 1878275SEric Cheng mip->mi_minor = (minor_t)(instance + 1); 1888275SEric Cheng mip->mi_dip = mregp->m_dip; 1898275SEric Cheng mip->mi_clients_list = NULL; 1908275SEric Cheng mip->mi_nclients = 0; 1918275SEric Cheng 19210491SRishi.Srivatsavai@Sun.COM /* Set the default IEEE Port VLAN Identifier */ 19310491SRishi.Srivatsavai@Sun.COM mip->mi_pvid = 1; 19410491SRishi.Srivatsavai@Sun.COM 19510491SRishi.Srivatsavai@Sun.COM /* Default bridge link learning protection values */ 19610491SRishi.Srivatsavai@Sun.COM mip->mi_llimit = 1000; 19710491SRishi.Srivatsavai@Sun.COM mip->mi_ldecay = 200; 19810491SRishi.Srivatsavai@Sun.COM 1998275SEric Cheng driver = (char *)ddi_driver_name(mip->mi_dip); 2008275SEric Cheng 2018275SEric Cheng /* Construct the MAC name as <drvname><instance> */ 2028275SEric Cheng (void) snprintf(mip->mi_name, sizeof (mip->mi_name), "%s%d", 2038275SEric Cheng driver, instance); 2048275SEric Cheng 2058275SEric Cheng mip->mi_driver = mregp->m_driver; 2068275SEric Cheng 2078275SEric Cheng mip->mi_type = mtype; 2088275SEric Cheng mip->mi_margin = mregp->m_margin; 2098275SEric Cheng mip->mi_info.mi_media = mtype->mt_type; 2108275SEric Cheng mip->mi_info.mi_nativemedia = mtype->mt_nativetype; 2118275SEric Cheng if (mregp->m_max_sdu <= mregp->m_min_sdu) 2128275SEric Cheng goto fail; 2138275SEric Cheng mip->mi_sdu_min = mregp->m_min_sdu; 2148275SEric Cheng mip->mi_sdu_max = mregp->m_max_sdu; 2158275SEric Cheng mip->mi_info.mi_addr_length = mip->mi_type->mt_addr_length; 2168275SEric Cheng /* 2178275SEric Cheng * If the media supports a broadcast address, cache a pointer to it 2188275SEric Cheng * in the mac_info_t so that upper layers can use it. 2198275SEric Cheng */ 2208275SEric Cheng mip->mi_info.mi_brdcst_addr = mip->mi_type->mt_brdcst_addr; 2218275SEric Cheng 2228275SEric Cheng mip->mi_v12n_level = mregp->m_v12n; 2238275SEric Cheng 2248275SEric Cheng /* 2258275SEric Cheng * Copy the unicast source address into the mac_info_t, but only if 2268275SEric Cheng * the MAC-Type defines a non-zero address length. We need to 2278275SEric Cheng * handle MAC-Types that have an address length of 0 2288275SEric Cheng * (point-to-point protocol MACs for example). 2298275SEric Cheng */ 2308275SEric Cheng if (mip->mi_type->mt_addr_length > 0) { 2318275SEric Cheng if (mregp->m_src_addr == NULL) 2328275SEric Cheng goto fail; 2338275SEric Cheng mip->mi_info.mi_unicst_addr = 2348275SEric Cheng kmem_alloc(mip->mi_type->mt_addr_length, KM_SLEEP); 2358275SEric Cheng bcopy(mregp->m_src_addr, mip->mi_info.mi_unicst_addr, 2368275SEric Cheng mip->mi_type->mt_addr_length); 2378275SEric Cheng 2388275SEric Cheng /* 2398275SEric Cheng * Copy the fixed 'factory' MAC address from the immutable 2408275SEric Cheng * info. This is taken to be the MAC address currently in 2418275SEric Cheng * use. 2428275SEric Cheng */ 2438275SEric Cheng bcopy(mip->mi_info.mi_unicst_addr, mip->mi_addr, 2448275SEric Cheng mip->mi_type->mt_addr_length); 2458275SEric Cheng 2468275SEric Cheng /* 2478275SEric Cheng * At this point, we should set up the classification 2488275SEric Cheng * rules etc but we delay it till mac_open() so that 2498275SEric Cheng * the resource discovery has taken place and we 2508275SEric Cheng * know someone wants to use the device. Otherwise 2518275SEric Cheng * memory gets allocated for Rx ring structures even 2528275SEric Cheng * during probe. 2538275SEric Cheng */ 2548275SEric Cheng 2558275SEric Cheng /* Copy the destination address if one is provided. */ 2568275SEric Cheng if (mregp->m_dst_addr != NULL) { 2578275SEric Cheng bcopy(mregp->m_dst_addr, mip->mi_dstaddr, 2588275SEric Cheng mip->mi_type->mt_addr_length); 25910616SSebastien.Roy@Sun.COM mip->mi_dstaddr_set = B_TRUE; 2608275SEric Cheng } 2618275SEric Cheng } else if (mregp->m_src_addr != NULL) { 2628275SEric Cheng goto fail; 2638275SEric Cheng } 2648275SEric Cheng 2658275SEric Cheng /* 2668275SEric Cheng * The format of the m_pdata is specific to the plugin. It is 2678275SEric Cheng * passed in as an argument to all of the plugin callbacks. The 2688275SEric Cheng * driver can update this information by calling 2698275SEric Cheng * mac_pdata_update(). 2708275SEric Cheng */ 27110616SSebastien.Roy@Sun.COM if (mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY) { 2728275SEric Cheng /* 27310616SSebastien.Roy@Sun.COM * Verify if the supplied plugin data is valid. Note that 27410616SSebastien.Roy@Sun.COM * even if the caller passed in a NULL pointer as plugin data, 27510616SSebastien.Roy@Sun.COM * we still need to verify if that's valid as the plugin may 27610616SSebastien.Roy@Sun.COM * require plugin data to function. 2778275SEric Cheng */ 2788275SEric Cheng if (!mip->mi_type->mt_ops.mtops_pdata_verify(mregp->m_pdata, 2798275SEric Cheng mregp->m_pdata_size)) { 2808275SEric Cheng goto fail; 2818275SEric Cheng } 28210616SSebastien.Roy@Sun.COM if (mregp->m_pdata != NULL) { 28310616SSebastien.Roy@Sun.COM mip->mi_pdata = 28410616SSebastien.Roy@Sun.COM kmem_alloc(mregp->m_pdata_size, KM_SLEEP); 28510616SSebastien.Roy@Sun.COM bcopy(mregp->m_pdata, mip->mi_pdata, 28610616SSebastien.Roy@Sun.COM mregp->m_pdata_size); 28710616SSebastien.Roy@Sun.COM mip->mi_pdata_size = mregp->m_pdata_size; 28810616SSebastien.Roy@Sun.COM } 28910616SSebastien.Roy@Sun.COM } else if (mregp->m_pdata != NULL) { 29010616SSebastien.Roy@Sun.COM /* 29110616SSebastien.Roy@Sun.COM * The caller supplied non-NULL plugin data, but the plugin 29210616SSebastien.Roy@Sun.COM * does not recognize plugin data. 29310616SSebastien.Roy@Sun.COM */ 29410616SSebastien.Roy@Sun.COM err = EINVAL; 29510616SSebastien.Roy@Sun.COM goto fail; 2968275SEric Cheng } 2978275SEric Cheng 2988275SEric Cheng /* 2998275SEric Cheng * Register the private properties. 3008275SEric Cheng */ 3018275SEric Cheng mac_register_priv_prop(mip, mregp->m_priv_props, 3028275SEric Cheng mregp->m_priv_prop_count); 3038275SEric Cheng 3048275SEric Cheng /* 3058275SEric Cheng * Stash the driver callbacks into the mac_impl_t, but first sanity 3068275SEric Cheng * check to make sure all mandatory callbacks are set. 3078275SEric Cheng */ 3088275SEric Cheng if (mregp->m_callbacks->mc_getstat == NULL || 3098275SEric Cheng mregp->m_callbacks->mc_start == NULL || 3108275SEric Cheng mregp->m_callbacks->mc_stop == NULL || 3118275SEric Cheng mregp->m_callbacks->mc_setpromisc == NULL || 3128275SEric Cheng mregp->m_callbacks->mc_multicst == NULL) { 3138275SEric Cheng goto fail; 3148275SEric Cheng } 3158275SEric Cheng mip->mi_callbacks = mregp->m_callbacks; 3168275SEric Cheng 3179073SCathy.Zhou@Sun.COM if (mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LEGACY, 3189073SCathy.Zhou@Sun.COM &mip->mi_capab_legacy)) { 3198275SEric Cheng mip->mi_state_flags |= MIS_LEGACY; 3209073SCathy.Zhou@Sun.COM mip->mi_phy_dev = mip->mi_capab_legacy.ml_dev; 3218275SEric Cheng } else { 3228275SEric Cheng mip->mi_phy_dev = makedevice(ddi_driver_major(mip->mi_dip), 32310654SGarrett.Damore@Sun.COM mip->mi_minor); 3248275SEric Cheng } 3258275SEric Cheng 3268275SEric Cheng /* 3278275SEric Cheng * Allocate a notification thread. thread_create blocks for memory 3288275SEric Cheng * if needed, it never fails. 3298275SEric Cheng */ 3308275SEric Cheng mip->mi_notify_thread = thread_create(NULL, 0, i_mac_notify_thread, 3318275SEric Cheng mip, 0, &p0, TS_RUN, minclsyspri); 3328275SEric Cheng 3338275SEric Cheng /* 3348275SEric Cheng * Initialize the capabilities 3358275SEric Cheng */ 3368275SEric Cheng 3378275SEric Cheng if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, NULL)) 3388275SEric Cheng mip->mi_state_flags |= MIS_IS_VNIC; 3398275SEric Cheng 3408275SEric Cheng if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR, NULL)) 3418275SEric Cheng mip->mi_state_flags |= MIS_IS_AGGR; 3428275SEric Cheng 3438275SEric Cheng mac_addr_factory_init(mip); 3448275SEric Cheng 3458275SEric Cheng /* 3468275SEric Cheng * Enforce the virtrualization level registered. 3478275SEric Cheng */ 3488275SEric Cheng if (mip->mi_v12n_level & MAC_VIRT_LEVEL1) { 3498275SEric Cheng if (mac_init_rings(mip, MAC_RING_TYPE_RX) != 0 || 3508275SEric Cheng mac_init_rings(mip, MAC_RING_TYPE_TX) != 0) 3518275SEric Cheng goto fail; 3528275SEric Cheng 3538275SEric Cheng /* 3548275SEric Cheng * The driver needs to register at least rx rings for this 3558275SEric Cheng * virtualization level. 3568275SEric Cheng */ 3578275SEric Cheng if (mip->mi_rx_groups == NULL) 3588275SEric Cheng goto fail; 3598275SEric Cheng } 3608275SEric Cheng 3618275SEric Cheng /* 3628275SEric Cheng * The driver must set mc_unicst entry point to NULL when it advertises 3638275SEric Cheng * CAP_RINGS for rx groups. 3648275SEric Cheng */ 3658275SEric Cheng if (mip->mi_rx_groups != NULL) { 3668275SEric Cheng if (mregp->m_callbacks->mc_unicst != NULL) 3678275SEric Cheng goto fail; 3688275SEric Cheng } else { 3698275SEric Cheng if (mregp->m_callbacks->mc_unicst == NULL) 3708275SEric Cheng goto fail; 3718275SEric Cheng } 3728275SEric Cheng 3738275SEric Cheng /* 3748275SEric Cheng * The driver must set mc_tx entry point to NULL when it advertises 3758275SEric Cheng * CAP_RINGS for tx rings. 3768275SEric Cheng */ 3778275SEric Cheng if (mip->mi_tx_groups != NULL) { 3788275SEric Cheng if (mregp->m_callbacks->mc_tx != NULL) 3798275SEric Cheng goto fail; 3808275SEric Cheng } else { 3818275SEric Cheng if (mregp->m_callbacks->mc_tx == NULL) 3828275SEric Cheng goto fail; 3838275SEric Cheng } 3848275SEric Cheng 3858275SEric Cheng /* 3868275SEric Cheng * Initialize MAC addresses. Must be called after mac_init_rings(). 3878275SEric Cheng */ 3888275SEric Cheng mac_init_macaddr(mip); 3898275SEric Cheng 3908275SEric Cheng mip->mi_share_capab.ms_snum = 0; 3918275SEric Cheng if (mip->mi_v12n_level & MAC_VIRT_HIO) { 3928275SEric Cheng (void) mac_capab_get((mac_handle_t)mip, MAC_CAPAB_SHARES, 3938275SEric Cheng &mip->mi_share_capab); 3948275SEric Cheng } 3958275SEric Cheng 3968275SEric Cheng /* 3978275SEric Cheng * Initialize the kstats for this device. 3988275SEric Cheng */ 3998275SEric Cheng mac_stat_create(mip); 4008275SEric Cheng 4018275SEric Cheng /* Zero out any properties. */ 4028275SEric Cheng bzero(&mip->mi_resource_props, sizeof (mac_resource_props_t)); 4038275SEric Cheng 40410727Sgdamore@opensolaris.org if (mip->mi_minor <= MAC_MAX_MINOR) { 4058275SEric Cheng /* Create a style-2 DLPI device */ 4068275SEric Cheng if (ddi_create_minor_node(mip->mi_dip, driver, S_IFCHR, 0, 4078275SEric Cheng DDI_NT_NET, CLONE_DEV) != DDI_SUCCESS) 4088275SEric Cheng goto fail; 4098275SEric Cheng style2_created = B_TRUE; 4108275SEric Cheng 4118275SEric Cheng /* Create a style-1 DLPI device */ 4128275SEric Cheng if (ddi_create_minor_node(mip->mi_dip, mip->mi_name, S_IFCHR, 4138275SEric Cheng mip->mi_minor, DDI_NT_NET, 0) != DDI_SUCCESS) 4148275SEric Cheng goto fail; 4158275SEric Cheng style1_created = B_TRUE; 4168275SEric Cheng } 4178275SEric Cheng 4188275SEric Cheng mac_flow_l2tab_create(mip, &mip->mi_flow_tab); 4198275SEric Cheng 4208275SEric Cheng rw_enter(&i_mac_impl_lock, RW_WRITER); 4218275SEric Cheng if (mod_hash_insert(i_mac_impl_hash, 4228275SEric Cheng (mod_hash_key_t)mip->mi_name, (mod_hash_val_t)mip) != 0) { 4238275SEric Cheng rw_exit(&i_mac_impl_lock); 4248275SEric Cheng err = EEXIST; 4258275SEric Cheng goto fail; 4268275SEric Cheng } 4278275SEric Cheng 4288275SEric Cheng DTRACE_PROBE2(mac__register, struct devnames *, dnp, 4298275SEric Cheng (mac_impl_t *), mip); 4308275SEric Cheng 4318275SEric Cheng /* 4328275SEric Cheng * Mark the MAC to be ready for open. 4338275SEric Cheng */ 4348275SEric Cheng mip->mi_state_flags &= ~MIS_DISABLED; 4358275SEric Cheng rw_exit(&i_mac_impl_lock); 4368275SEric Cheng 4378275SEric Cheng atomic_inc_32(&i_mac_impl_count); 4388275SEric Cheng 4398275SEric Cheng cmn_err(CE_NOTE, "!%s registered", mip->mi_name); 4408275SEric Cheng *mhp = (mac_handle_t)mip; 4418275SEric Cheng return (0); 4428275SEric Cheng 4438275SEric Cheng fail: 4448275SEric Cheng if (style1_created) 4458275SEric Cheng ddi_remove_minor_node(mip->mi_dip, mip->mi_name); 4468275SEric Cheng 4478275SEric Cheng if (style2_created) 4488275SEric Cheng ddi_remove_minor_node(mip->mi_dip, driver); 4498275SEric Cheng 4508275SEric Cheng mac_addr_factory_fini(mip); 4518275SEric Cheng 4528275SEric Cheng /* Clean up registered MAC addresses */ 4538275SEric Cheng mac_fini_macaddr(mip); 4548275SEric Cheng 4558275SEric Cheng /* Clean up registered rings */ 4568275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_RX); 4578275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_TX); 4588275SEric Cheng 4598275SEric Cheng /* Clean up notification thread */ 4608275SEric Cheng if (mip->mi_notify_thread != NULL) 4618275SEric Cheng i_mac_notify_exit(mip); 4628275SEric Cheng 4638275SEric Cheng if (mip->mi_info.mi_unicst_addr != NULL) { 4648275SEric Cheng kmem_free(mip->mi_info.mi_unicst_addr, 4658275SEric Cheng mip->mi_type->mt_addr_length); 4668275SEric Cheng mip->mi_info.mi_unicst_addr = NULL; 4678275SEric Cheng } 4688275SEric Cheng 4698275SEric Cheng mac_stat_destroy(mip); 4708275SEric Cheng 4718275SEric Cheng if (mip->mi_type != NULL) { 4728275SEric Cheng atomic_dec_32(&mip->mi_type->mt_ref); 4738275SEric Cheng mip->mi_type = NULL; 4748275SEric Cheng } 4758275SEric Cheng 4768275SEric Cheng if (mip->mi_pdata != NULL) { 4778275SEric Cheng kmem_free(mip->mi_pdata, mip->mi_pdata_size); 4788275SEric Cheng mip->mi_pdata = NULL; 4798275SEric Cheng mip->mi_pdata_size = 0; 4808275SEric Cheng } 4818275SEric Cheng 4828275SEric Cheng if (minor != 0) { 4838275SEric Cheng ASSERT(minor > MAC_MAX_MINOR); 4848275SEric Cheng mac_minor_rele(minor); 4858275SEric Cheng } 4868275SEric Cheng 4878275SEric Cheng mac_unregister_priv_prop(mip); 4888275SEric Cheng 48911544SMichael.Speer@Sun.COM /* 49011544SMichael.Speer@Sun.COM * Clear the state before destroying the mac_impl_t 49111544SMichael.Speer@Sun.COM */ 49211544SMichael.Speer@Sun.COM mip->mi_state_flags = 0; 49311544SMichael.Speer@Sun.COM 4948275SEric Cheng kmem_cache_free(i_mac_impl_cachep, mip); 4958275SEric Cheng return (err); 4968275SEric Cheng } 4978275SEric Cheng 4988275SEric Cheng /* 4998275SEric Cheng * Unregister from the GLDv3 framework 5008275SEric Cheng */ 5018275SEric Cheng int 5028275SEric Cheng mac_unregister(mac_handle_t mh) 5038275SEric Cheng { 5048275SEric Cheng int err; 5058275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 5068275SEric Cheng mod_hash_val_t val; 5078275SEric Cheng mac_margin_req_t *mmr, *nextmmr; 5088275SEric Cheng 5098275SEric Cheng /* Fail the unregister if there are any open references to this mac. */ 5108275SEric Cheng if ((err = mac_disable_nowait(mh)) != 0) 5118275SEric Cheng return (err); 5128275SEric Cheng 5138275SEric Cheng /* 5148275SEric Cheng * Clean up notification thread and wait for it to exit. 5158275SEric Cheng */ 5168275SEric Cheng i_mac_notify_exit(mip); 5178275SEric Cheng 5188275SEric Cheng i_mac_perim_enter(mip); 5198275SEric Cheng 5209073SCathy.Zhou@Sun.COM /* 5219073SCathy.Zhou@Sun.COM * There is still resource properties configured over this mac. 5229073SCathy.Zhou@Sun.COM */ 5239073SCathy.Zhou@Sun.COM if (mip->mi_resource_props.mrp_mask != 0) 5249073SCathy.Zhou@Sun.COM mac_fastpath_enable((mac_handle_t)mip); 5259073SCathy.Zhou@Sun.COM 5268275SEric Cheng if (mip->mi_minor < MAC_MAX_MINOR + 1) { 5278275SEric Cheng ddi_remove_minor_node(mip->mi_dip, mip->mi_name); 5288275SEric Cheng ddi_remove_minor_node(mip->mi_dip, 5298275SEric Cheng (char *)ddi_driver_name(mip->mi_dip)); 5308275SEric Cheng } 5318275SEric Cheng 5328275SEric Cheng ASSERT(mip->mi_nactiveclients == 0 && !(mip->mi_state_flags & 5338275SEric Cheng MIS_EXCLUSIVE)); 5348275SEric Cheng 5358275SEric Cheng mac_stat_destroy(mip); 5368275SEric Cheng 5378275SEric Cheng (void) mod_hash_remove(i_mac_impl_hash, 5388275SEric Cheng (mod_hash_key_t)mip->mi_name, &val); 5398275SEric Cheng ASSERT(mip == (mac_impl_t *)val); 5408275SEric Cheng 5418275SEric Cheng ASSERT(i_mac_impl_count > 0); 5428275SEric Cheng atomic_dec_32(&i_mac_impl_count); 5438275SEric Cheng 5448275SEric Cheng if (mip->mi_pdata != NULL) 5458275SEric Cheng kmem_free(mip->mi_pdata, mip->mi_pdata_size); 5468275SEric Cheng mip->mi_pdata = NULL; 5478275SEric Cheng mip->mi_pdata_size = 0; 5488275SEric Cheng 5498275SEric Cheng /* 5508275SEric Cheng * Free the list of margin request. 5518275SEric Cheng */ 5528275SEric Cheng for (mmr = mip->mi_mmrp; mmr != NULL; mmr = nextmmr) { 5538275SEric Cheng nextmmr = mmr->mmr_nextp; 5548275SEric Cheng kmem_free(mmr, sizeof (mac_margin_req_t)); 5558275SEric Cheng } 5568275SEric Cheng mip->mi_mmrp = NULL; 5578275SEric Cheng 55810491SRishi.Srivatsavai@Sun.COM mip->mi_linkstate = mip->mi_lowlinkstate = LINK_STATE_UNKNOWN; 5598275SEric Cheng kmem_free(mip->mi_info.mi_unicst_addr, mip->mi_type->mt_addr_length); 5608275SEric Cheng mip->mi_info.mi_unicst_addr = NULL; 5618275SEric Cheng 5628275SEric Cheng atomic_dec_32(&mip->mi_type->mt_ref); 5638275SEric Cheng mip->mi_type = NULL; 5648275SEric Cheng 5658275SEric Cheng /* 5668275SEric Cheng * Free the primary MAC address. 5678275SEric Cheng */ 5688275SEric Cheng mac_fini_macaddr(mip); 5698275SEric Cheng 5708275SEric Cheng /* 5718275SEric Cheng * free all rings 5728275SEric Cheng */ 5738275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_RX); 5748275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_TX); 5758275SEric Cheng 5768275SEric Cheng mac_addr_factory_fini(mip); 5778275SEric Cheng 5788275SEric Cheng bzero(mip->mi_addr, MAXMACADDRLEN); 5798275SEric Cheng bzero(mip->mi_dstaddr, MAXMACADDRLEN); 5808275SEric Cheng 5818275SEric Cheng /* and the flows */ 5828275SEric Cheng mac_flow_tab_destroy(mip->mi_flow_tab); 5838275SEric Cheng mip->mi_flow_tab = NULL; 5848275SEric Cheng 5858275SEric Cheng if (mip->mi_minor > MAC_MAX_MINOR) 5868275SEric Cheng mac_minor_rele(mip->mi_minor); 5878275SEric Cheng 5888275SEric Cheng cmn_err(CE_NOTE, "!%s unregistered", mip->mi_name); 5898275SEric Cheng 5908275SEric Cheng /* 5918275SEric Cheng * Reset the perim related fields to default values before 5928275SEric Cheng * kmem_cache_free 5938275SEric Cheng */ 5948275SEric Cheng i_mac_perim_exit(mip); 5958275SEric Cheng mip->mi_state_flags = 0; 5968275SEric Cheng 5978275SEric Cheng mac_unregister_priv_prop(mip); 59810491SRishi.Srivatsavai@Sun.COM 59910491SRishi.Srivatsavai@Sun.COM ASSERT(mip->mi_bridge_link == NULL); 6008275SEric Cheng kmem_cache_free(i_mac_impl_cachep, mip); 6018275SEric Cheng 6028275SEric Cheng return (0); 6038275SEric Cheng } 6048275SEric Cheng 6058275SEric Cheng /* DATA RECEPTION */ 6068275SEric Cheng 6078275SEric Cheng /* 6088275SEric Cheng * This function is invoked for packets received by the MAC driver in 6098275SEric Cheng * interrupt context. The ring generation number provided by the driver 6108275SEric Cheng * is matched with the ring generation number held in MAC. If they do not 6118275SEric Cheng * match, received packets are considered stale packets coming from an older 6128275SEric Cheng * assignment of the ring. Drop them. 6138275SEric Cheng */ 6148275SEric Cheng void 6158275SEric Cheng mac_rx_ring(mac_handle_t mh, mac_ring_handle_t mrh, mblk_t *mp_chain, 6168275SEric Cheng uint64_t mr_gen_num) 6178275SEric Cheng { 6188275SEric Cheng mac_ring_t *mr = (mac_ring_t *)mrh; 6198275SEric Cheng 6208275SEric Cheng if ((mr != NULL) && (mr->mr_gen_num != mr_gen_num)) { 6218275SEric Cheng DTRACE_PROBE2(mac__rx__rings__stale__packet, uint64_t, 6228275SEric Cheng mr->mr_gen_num, uint64_t, mr_gen_num); 6238275SEric Cheng freemsgchain(mp_chain); 6248275SEric Cheng return; 6258275SEric Cheng } 6268275SEric Cheng mac_rx(mh, (mac_resource_handle_t)mrh, mp_chain); 6278275SEric Cheng } 6288275SEric Cheng 6298275SEric Cheng /* 63010491SRishi.Srivatsavai@Sun.COM * This function is invoked for each packet received by the underlying driver. 6318275SEric Cheng */ 6328275SEric Cheng void 6338275SEric Cheng mac_rx(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain) 6348275SEric Cheng { 63510491SRishi.Srivatsavai@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 63610491SRishi.Srivatsavai@Sun.COM 63710491SRishi.Srivatsavai@Sun.COM /* 63810491SRishi.Srivatsavai@Sun.COM * Check if the link is part of a bridge. If not, then we don't need 63910491SRishi.Srivatsavai@Sun.COM * to take the lock to remain consistent. Make this common case 64010491SRishi.Srivatsavai@Sun.COM * lock-free and tail-call optimized. 64110491SRishi.Srivatsavai@Sun.COM */ 64210491SRishi.Srivatsavai@Sun.COM if (mip->mi_bridge_link == NULL) { 64310491SRishi.Srivatsavai@Sun.COM mac_rx_common(mh, mrh, mp_chain); 64410491SRishi.Srivatsavai@Sun.COM } else { 64510491SRishi.Srivatsavai@Sun.COM /* 64610491SRishi.Srivatsavai@Sun.COM * Once we take a reference on the bridge link, the bridge 64710491SRishi.Srivatsavai@Sun.COM * module itself can't unload, so the callback pointers are 64810491SRishi.Srivatsavai@Sun.COM * stable. 64910491SRishi.Srivatsavai@Sun.COM */ 65010491SRishi.Srivatsavai@Sun.COM mutex_enter(&mip->mi_bridge_lock); 65110491SRishi.Srivatsavai@Sun.COM if ((mh = mip->mi_bridge_link) != NULL) 65210491SRishi.Srivatsavai@Sun.COM mac_bridge_ref_cb(mh, B_TRUE); 65310491SRishi.Srivatsavai@Sun.COM mutex_exit(&mip->mi_bridge_lock); 65410491SRishi.Srivatsavai@Sun.COM if (mh == NULL) { 65510491SRishi.Srivatsavai@Sun.COM mac_rx_common((mac_handle_t)mip, mrh, mp_chain); 65610491SRishi.Srivatsavai@Sun.COM } else { 65710491SRishi.Srivatsavai@Sun.COM mac_bridge_rx_cb(mh, mrh, mp_chain); 65810491SRishi.Srivatsavai@Sun.COM mac_bridge_ref_cb(mh, B_FALSE); 65910491SRishi.Srivatsavai@Sun.COM } 66010491SRishi.Srivatsavai@Sun.COM } 66110491SRishi.Srivatsavai@Sun.COM } 66210491SRishi.Srivatsavai@Sun.COM 66310491SRishi.Srivatsavai@Sun.COM /* 66410491SRishi.Srivatsavai@Sun.COM * Special case function: this allows snooping of packets transmitted and 66510491SRishi.Srivatsavai@Sun.COM * received by TRILL. By design, they go directly into the TRILL module. 66610491SRishi.Srivatsavai@Sun.COM */ 66710491SRishi.Srivatsavai@Sun.COM void 66810491SRishi.Srivatsavai@Sun.COM mac_trill_snoop(mac_handle_t mh, mblk_t *mp) 66910491SRishi.Srivatsavai@Sun.COM { 67010491SRishi.Srivatsavai@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 67110491SRishi.Srivatsavai@Sun.COM 67210491SRishi.Srivatsavai@Sun.COM if (mip->mi_promisc_list != NULL) 67310491SRishi.Srivatsavai@Sun.COM mac_promisc_dispatch(mip, mp, NULL); 67410491SRishi.Srivatsavai@Sun.COM } 67510491SRishi.Srivatsavai@Sun.COM 67610491SRishi.Srivatsavai@Sun.COM /* 67710491SRishi.Srivatsavai@Sun.COM * This is the upward reentry point for packets arriving from the bridging 67810491SRishi.Srivatsavai@Sun.COM * module and from mac_rx for links not part of a bridge. 67910491SRishi.Srivatsavai@Sun.COM */ 68010491SRishi.Srivatsavai@Sun.COM void 68110491SRishi.Srivatsavai@Sun.COM mac_rx_common(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain) 68210491SRishi.Srivatsavai@Sun.COM { 6838275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 6848275SEric Cheng mac_ring_t *mr = (mac_ring_t *)mrh; 6858275SEric Cheng mac_soft_ring_set_t *mac_srs; 6868275SEric Cheng mblk_t *bp = mp_chain; 6878275SEric Cheng boolean_t hw_classified = B_FALSE; 6888275SEric Cheng 6898275SEric Cheng /* 6908275SEric Cheng * If there are any promiscuous mode callbacks defined for 6918275SEric Cheng * this MAC, pass them a copy if appropriate. 6928275SEric Cheng */ 6938275SEric Cheng if (mip->mi_promisc_list != NULL) 6948275SEric Cheng mac_promisc_dispatch(mip, mp_chain, NULL); 6958275SEric Cheng 6968275SEric Cheng if (mr != NULL) { 6978275SEric Cheng /* 6988275SEric Cheng * If the SRS teardown has started, just return. The 'mr' 6998275SEric Cheng * continues to be valid until the driver unregisters the mac. 7008275SEric Cheng * Hardware classified packets will not make their way up 7018275SEric Cheng * beyond this point once the teardown has started. The driver 7028275SEric Cheng * is never passed a pointer to a flow entry or SRS or any 7038275SEric Cheng * structure that can be freed much before mac_unregister. 7048275SEric Cheng */ 7058275SEric Cheng mutex_enter(&mr->mr_lock); 7068275SEric Cheng if ((mr->mr_state != MR_INUSE) || (mr->mr_flag & 7078275SEric Cheng (MR_INCIPIENT | MR_CONDEMNED | MR_QUIESCE))) { 7088275SEric Cheng mutex_exit(&mr->mr_lock); 7098275SEric Cheng freemsgchain(mp_chain); 7108275SEric Cheng return; 7118275SEric Cheng } 7128275SEric Cheng if (mr->mr_classify_type == MAC_HW_CLASSIFIER) { 7138275SEric Cheng hw_classified = B_TRUE; 7148275SEric Cheng MR_REFHOLD_LOCKED(mr); 7158275SEric Cheng } 7168275SEric Cheng mutex_exit(&mr->mr_lock); 7178275SEric Cheng 7188275SEric Cheng /* 7198275SEric Cheng * We check if an SRS is controlling this ring. 7208275SEric Cheng * If so, we can directly call the srs_lower_proc 7218275SEric Cheng * routine otherwise we need to go through mac_rx_classify 7228275SEric Cheng * to reach the right place. 7238275SEric Cheng */ 7248275SEric Cheng if (hw_classified) { 7258275SEric Cheng mac_srs = mr->mr_srs; 7268275SEric Cheng /* 7278275SEric Cheng * This is supposed to be the fast path. 7288275SEric Cheng * All packets received though here were steered by 7298275SEric Cheng * the hardware classifier, and share the same 7308275SEric Cheng * MAC header info. 7318275SEric Cheng */ 7328275SEric Cheng mac_srs->srs_rx.sr_lower_proc(mh, 7338275SEric Cheng (mac_resource_handle_t)mac_srs, mp_chain, B_FALSE); 7348275SEric Cheng MR_REFRELE(mr); 7358275SEric Cheng return; 7368275SEric Cheng } 7378275SEric Cheng /* We'll fall through to software classification */ 7388833SVenu.Iyer@Sun.COM } else { 7398833SVenu.Iyer@Sun.COM flow_entry_t *flent; 7408833SVenu.Iyer@Sun.COM int err; 7418833SVenu.Iyer@Sun.COM 7428833SVenu.Iyer@Sun.COM rw_enter(&mip->mi_rw_lock, RW_READER); 7438833SVenu.Iyer@Sun.COM if (mip->mi_single_active_client != NULL) { 7448833SVenu.Iyer@Sun.COM flent = mip->mi_single_active_client->mci_flent_list; 7458833SVenu.Iyer@Sun.COM FLOW_TRY_REFHOLD(flent, err); 7468833SVenu.Iyer@Sun.COM rw_exit(&mip->mi_rw_lock); 7478833SVenu.Iyer@Sun.COM if (err == 0) { 7488833SVenu.Iyer@Sun.COM (flent->fe_cb_fn)(flent->fe_cb_arg1, 7498833SVenu.Iyer@Sun.COM flent->fe_cb_arg2, mp_chain, B_FALSE); 7508833SVenu.Iyer@Sun.COM FLOW_REFRELE(flent); 7518833SVenu.Iyer@Sun.COM return; 7528833SVenu.Iyer@Sun.COM } 7538833SVenu.Iyer@Sun.COM } else { 7548833SVenu.Iyer@Sun.COM rw_exit(&mip->mi_rw_lock); 7558833SVenu.Iyer@Sun.COM } 7568275SEric Cheng } 7578275SEric Cheng 7588275SEric Cheng if (!FLOW_TAB_EMPTY(mip->mi_flow_tab)) { 7598275SEric Cheng if ((bp = mac_rx_flow(mh, mrh, bp)) == NULL) 7608275SEric Cheng return; 7618275SEric Cheng } 7628275SEric Cheng 7638275SEric Cheng freemsgchain(bp); 7648275SEric Cheng } 7658275SEric Cheng 7668275SEric Cheng /* DATA TRANSMISSION */ 7678275SEric Cheng 7688275SEric Cheng /* 7698275SEric Cheng * A driver's notification to resume transmission, in case of a provider 7708275SEric Cheng * without TX rings. 7718275SEric Cheng */ 7728275SEric Cheng void 7738275SEric Cheng mac_tx_update(mac_handle_t mh) 7748275SEric Cheng { 7758275SEric Cheng /* 7768275SEric Cheng * Walk the list of MAC clients (mac_client_handle) 7778275SEric Cheng * and update 7788275SEric Cheng */ 7798275SEric Cheng i_mac_tx_srs_notify((mac_impl_t *)mh, NULL); 7808275SEric Cheng } 7818275SEric Cheng 7828275SEric Cheng /* 7838275SEric Cheng * A driver's notification to resume transmission on the specified TX ring. 7848275SEric Cheng */ 7858275SEric Cheng void 7868275SEric Cheng mac_tx_ring_update(mac_handle_t mh, mac_ring_handle_t rh) 7878275SEric Cheng { 7888275SEric Cheng i_mac_tx_srs_notify((mac_impl_t *)mh, rh); 7898275SEric Cheng } 7908275SEric Cheng 7918275SEric Cheng /* LINK STATE */ 7928275SEric Cheng /* 7938275SEric Cheng * Notify the MAC layer about a link state change 7948275SEric Cheng */ 7958275SEric Cheng void 7968275SEric Cheng mac_link_update(mac_handle_t mh, link_state_t link) 7978275SEric Cheng { 7988275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 7998275SEric Cheng 8008275SEric Cheng /* 8018275SEric Cheng * Save the link state. 8028275SEric Cheng */ 80310491SRishi.Srivatsavai@Sun.COM mip->mi_lowlinkstate = link; 80410491SRishi.Srivatsavai@Sun.COM 80510491SRishi.Srivatsavai@Sun.COM /* 80610491SRishi.Srivatsavai@Sun.COM * Send a MAC_NOTE_LOWLINK notification. This tells the notification 80710491SRishi.Srivatsavai@Sun.COM * thread to deliver both lower and upper notifications. 80810491SRishi.Srivatsavai@Sun.COM */ 80910491SRishi.Srivatsavai@Sun.COM i_mac_notify(mip, MAC_NOTE_LOWLINK); 81010491SRishi.Srivatsavai@Sun.COM } 81110491SRishi.Srivatsavai@Sun.COM 81210491SRishi.Srivatsavai@Sun.COM /* 81310491SRishi.Srivatsavai@Sun.COM * Notify the MAC layer about a link state change due to bridging. 81410491SRishi.Srivatsavai@Sun.COM */ 81510491SRishi.Srivatsavai@Sun.COM void 81610491SRishi.Srivatsavai@Sun.COM mac_link_redo(mac_handle_t mh, link_state_t link) 81710491SRishi.Srivatsavai@Sun.COM { 81810491SRishi.Srivatsavai@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 81910491SRishi.Srivatsavai@Sun.COM 82010491SRishi.Srivatsavai@Sun.COM /* 82110491SRishi.Srivatsavai@Sun.COM * Save the link state. 82210491SRishi.Srivatsavai@Sun.COM */ 8238275SEric Cheng mip->mi_linkstate = link; 8248275SEric Cheng 8258275SEric Cheng /* 82610491SRishi.Srivatsavai@Sun.COM * Send a MAC_NOTE_LINK notification. Only upper notifications are 82710491SRishi.Srivatsavai@Sun.COM * made. 8288275SEric Cheng */ 8298275SEric Cheng i_mac_notify(mip, MAC_NOTE_LINK); 8308275SEric Cheng } 8318275SEric Cheng 83210654SGarrett.Damore@Sun.COM /* MINOR NODE HANDLING */ 83310654SGarrett.Damore@Sun.COM 83410654SGarrett.Damore@Sun.COM /* 83510654SGarrett.Damore@Sun.COM * Given a dev_t, return the instance number (PPA) associated with it. 83610654SGarrett.Damore@Sun.COM * Drivers can use this in their getinfo(9e) implementation to lookup 83710654SGarrett.Damore@Sun.COM * the instance number (i.e. PPA) of the device, to use as an index to 83810654SGarrett.Damore@Sun.COM * their own array of soft state structures. 83910654SGarrett.Damore@Sun.COM * 84010654SGarrett.Damore@Sun.COM * Returns -1 on error. 84110654SGarrett.Damore@Sun.COM */ 84210654SGarrett.Damore@Sun.COM int 84310654SGarrett.Damore@Sun.COM mac_devt_to_instance(dev_t devt) 84410654SGarrett.Damore@Sun.COM { 84510654SGarrett.Damore@Sun.COM return (dld_devt_to_instance(devt)); 84610654SGarrett.Damore@Sun.COM } 84710654SGarrett.Damore@Sun.COM 84810654SGarrett.Damore@Sun.COM /* 84910654SGarrett.Damore@Sun.COM * This function returns the first minor number that is available for 85010654SGarrett.Damore@Sun.COM * driver private use. All minor numbers smaller than this are 85110654SGarrett.Damore@Sun.COM * reserved for GLDv3 use. 85210654SGarrett.Damore@Sun.COM */ 85310654SGarrett.Damore@Sun.COM minor_t 85410654SGarrett.Damore@Sun.COM mac_private_minor(void) 85510654SGarrett.Damore@Sun.COM { 85610654SGarrett.Damore@Sun.COM return (MAC_PRIVATE_MINOR); 85710654SGarrett.Damore@Sun.COM } 85810654SGarrett.Damore@Sun.COM 8598275SEric Cheng /* OTHER CONTROL INFORMATION */ 8608275SEric Cheng 8618275SEric Cheng /* 8628275SEric Cheng * A driver notified us that its primary MAC address has changed. 8638275SEric Cheng */ 8648275SEric Cheng void 8658275SEric Cheng mac_unicst_update(mac_handle_t mh, const uint8_t *addr) 8668275SEric Cheng { 8678275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 8688275SEric Cheng 8698275SEric Cheng if (mip->mi_type->mt_addr_length == 0) 8708275SEric Cheng return; 8718275SEric Cheng 8728275SEric Cheng i_mac_perim_enter(mip); 8738275SEric Cheng 8748275SEric Cheng /* 875*11588Sdavid.edmondson@sun.com * If address changes, freshen the MAC address value and update 876*11588Sdavid.edmondson@sun.com * all MAC clients that share this MAC address. 8778275SEric Cheng */ 878*11588Sdavid.edmondson@sun.com if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) != 0) { 879*11588Sdavid.edmondson@sun.com mac_freshen_macaddr(mac_find_macaddr(mip, mip->mi_addr), 880*11588Sdavid.edmondson@sun.com (uint8_t *)addr); 881*11588Sdavid.edmondson@sun.com } 8828275SEric Cheng 8838275SEric Cheng i_mac_perim_exit(mip); 8848275SEric Cheng 8858275SEric Cheng /* 8868275SEric Cheng * Send a MAC_NOTE_UNICST notification. 8878275SEric Cheng */ 8888275SEric Cheng i_mac_notify(mip, MAC_NOTE_UNICST); 8898275SEric Cheng } 8908275SEric Cheng 89110616SSebastien.Roy@Sun.COM void 89210616SSebastien.Roy@Sun.COM mac_dst_update(mac_handle_t mh, const uint8_t *addr) 89310616SSebastien.Roy@Sun.COM { 89410616SSebastien.Roy@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 89510616SSebastien.Roy@Sun.COM 89610616SSebastien.Roy@Sun.COM if (mip->mi_type->mt_addr_length == 0) 89710616SSebastien.Roy@Sun.COM return; 89810616SSebastien.Roy@Sun.COM 89910616SSebastien.Roy@Sun.COM i_mac_perim_enter(mip); 90010616SSebastien.Roy@Sun.COM bcopy(addr, mip->mi_dstaddr, mip->mi_type->mt_addr_length); 90110616SSebastien.Roy@Sun.COM i_mac_perim_exit(mip); 90210616SSebastien.Roy@Sun.COM i_mac_notify(mip, MAC_NOTE_DEST); 90310616SSebastien.Roy@Sun.COM } 90410616SSebastien.Roy@Sun.COM 9058275SEric Cheng /* 9068275SEric Cheng * MAC plugin information changed. 9078275SEric Cheng */ 9088275SEric Cheng int 9098275SEric Cheng mac_pdata_update(mac_handle_t mh, void *mac_pdata, size_t dsize) 9108275SEric Cheng { 9118275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 9128275SEric Cheng 9138275SEric Cheng /* 9148275SEric Cheng * Verify that the plugin supports MAC plugin data and that the 9158275SEric Cheng * supplied data is valid. 9168275SEric Cheng */ 9178275SEric Cheng if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY)) 9188275SEric Cheng return (EINVAL); 9198275SEric Cheng if (!mip->mi_type->mt_ops.mtops_pdata_verify(mac_pdata, dsize)) 9208275SEric Cheng return (EINVAL); 9218275SEric Cheng 9228275SEric Cheng if (mip->mi_pdata != NULL) 9238275SEric Cheng kmem_free(mip->mi_pdata, mip->mi_pdata_size); 9248275SEric Cheng 9258275SEric Cheng mip->mi_pdata = kmem_alloc(dsize, KM_SLEEP); 9268275SEric Cheng bcopy(mac_pdata, mip->mi_pdata, dsize); 9278275SEric Cheng mip->mi_pdata_size = dsize; 9288275SEric Cheng 9298275SEric Cheng /* 9308275SEric Cheng * Since the MAC plugin data is used to construct MAC headers that 9318275SEric Cheng * were cached in fast-path headers, we need to flush fast-path 9328275SEric Cheng * information for links associated with this mac. 9338275SEric Cheng */ 9348275SEric Cheng i_mac_notify(mip, MAC_NOTE_FASTPATH_FLUSH); 9358275SEric Cheng return (0); 9368275SEric Cheng } 9378275SEric Cheng 9388275SEric Cheng /* 9398275SEric Cheng * Invoked by driver as well as the framework to notify its capability change. 9408275SEric Cheng */ 9418275SEric Cheng void 9428275SEric Cheng mac_capab_update(mac_handle_t mh) 9438275SEric Cheng { 9448275SEric Cheng /* Send MAC_NOTE_CAPAB_CHG notification */ 9458275SEric Cheng i_mac_notify((mac_impl_t *)mh, MAC_NOTE_CAPAB_CHG); 9468275SEric Cheng } 9478275SEric Cheng 9488275SEric Cheng int 9498275SEric Cheng mac_maxsdu_update(mac_handle_t mh, uint_t sdu_max) 9508275SEric Cheng { 9518275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 9528275SEric Cheng 9539514SGirish.Moodalbail@Sun.COM if (sdu_max == 0 || sdu_max < mip->mi_sdu_min) 9548275SEric Cheng return (EINVAL); 9558275SEric Cheng mip->mi_sdu_max = sdu_max; 9568275SEric Cheng 9578275SEric Cheng /* Send a MAC_NOTE_SDU_SIZE notification. */ 9588275SEric Cheng i_mac_notify(mip, MAC_NOTE_SDU_SIZE); 9598275SEric Cheng return (0); 9608275SEric Cheng } 9618275SEric Cheng 9628275SEric Cheng /* PRIVATE FUNCTIONS, FOR INTERNAL USE ONLY */ 9638275SEric Cheng 9648275SEric Cheng /* 9658275SEric Cheng * Updates the mac_impl structure with the current state of the link 9668275SEric Cheng */ 9678275SEric Cheng static void 9688275SEric Cheng i_mac_log_link_state(mac_impl_t *mip) 9698275SEric Cheng { 9708275SEric Cheng /* 9718275SEric Cheng * If no change, then it is not interesting. 9728275SEric Cheng */ 97310491SRishi.Srivatsavai@Sun.COM if (mip->mi_lastlowlinkstate == mip->mi_lowlinkstate) 9748275SEric Cheng return; 9758275SEric Cheng 97610491SRishi.Srivatsavai@Sun.COM switch (mip->mi_lowlinkstate) { 9778275SEric Cheng case LINK_STATE_UP: 9788275SEric Cheng if (mip->mi_type->mt_ops.mtops_ops & MTOPS_LINK_DETAILS) { 9798275SEric Cheng char det[200]; 9808275SEric Cheng 9818275SEric Cheng mip->mi_type->mt_ops.mtops_link_details(det, 9828275SEric Cheng sizeof (det), (mac_handle_t)mip, mip->mi_pdata); 9838275SEric Cheng 9848275SEric Cheng cmn_err(CE_NOTE, "!%s link up, %s", mip->mi_name, det); 9858275SEric Cheng } else { 9868275SEric Cheng cmn_err(CE_NOTE, "!%s link up", mip->mi_name); 9878275SEric Cheng } 9888275SEric Cheng break; 9898275SEric Cheng 9908275SEric Cheng case LINK_STATE_DOWN: 9918275SEric Cheng /* 9928275SEric Cheng * Only transitions from UP to DOWN are interesting 9938275SEric Cheng */ 99410491SRishi.Srivatsavai@Sun.COM if (mip->mi_lastlowlinkstate != LINK_STATE_UNKNOWN) 9958275SEric Cheng cmn_err(CE_NOTE, "!%s link down", mip->mi_name); 9968275SEric Cheng break; 9978275SEric Cheng 9988275SEric Cheng case LINK_STATE_UNKNOWN: 9998275SEric Cheng /* 10008275SEric Cheng * This case is normally not interesting. 10018275SEric Cheng */ 10028275SEric Cheng break; 10038275SEric Cheng } 100410491SRishi.Srivatsavai@Sun.COM mip->mi_lastlowlinkstate = mip->mi_lowlinkstate; 10058275SEric Cheng } 10068275SEric Cheng 10078275SEric Cheng /* 10088275SEric Cheng * Main routine for the callbacks notifications thread 10098275SEric Cheng */ 10108275SEric Cheng static void 10118275SEric Cheng i_mac_notify_thread(void *arg) 10128275SEric Cheng { 10138275SEric Cheng mac_impl_t *mip = arg; 10148275SEric Cheng callb_cpr_t cprinfo; 10158275SEric Cheng mac_cb_t *mcb; 10168275SEric Cheng mac_cb_info_t *mcbi; 10178275SEric Cheng mac_notify_cb_t *mncb; 10188275SEric Cheng 10198275SEric Cheng mcbi = &mip->mi_notify_cb_info; 10208275SEric Cheng CALLB_CPR_INIT(&cprinfo, mcbi->mcbi_lockp, callb_generic_cpr, 10218275SEric Cheng "i_mac_notify_thread"); 10228275SEric Cheng 10238275SEric Cheng mutex_enter(mcbi->mcbi_lockp); 10248275SEric Cheng 10258275SEric Cheng for (;;) { 10268275SEric Cheng uint32_t bits; 10278275SEric Cheng uint32_t type; 10288275SEric Cheng 10298275SEric Cheng bits = mip->mi_notify_bits; 10308275SEric Cheng if (bits == 0) { 10318275SEric Cheng CALLB_CPR_SAFE_BEGIN(&cprinfo); 10328275SEric Cheng cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 10338275SEric Cheng CALLB_CPR_SAFE_END(&cprinfo, mcbi->mcbi_lockp); 10348275SEric Cheng continue; 10358275SEric Cheng } 10368275SEric Cheng mip->mi_notify_bits = 0; 10378275SEric Cheng if ((bits & (1 << MAC_NNOTE)) != 0) { 10388275SEric Cheng /* request to quit */ 10398275SEric Cheng ASSERT(mip->mi_state_flags & MIS_DISABLED); 10408275SEric Cheng break; 10418275SEric Cheng } 10428275SEric Cheng 10438275SEric Cheng mutex_exit(mcbi->mcbi_lockp); 10448275SEric Cheng 10458275SEric Cheng /* 104610491SRishi.Srivatsavai@Sun.COM * Log link changes on the actual link, but then do reports on 104710491SRishi.Srivatsavai@Sun.COM * synthetic state (if part of a bridge). 10488275SEric Cheng */ 104910491SRishi.Srivatsavai@Sun.COM if ((bits & (1 << MAC_NOTE_LOWLINK)) != 0) { 105010491SRishi.Srivatsavai@Sun.COM link_state_t newstate; 105110491SRishi.Srivatsavai@Sun.COM mac_handle_t mh; 105210491SRishi.Srivatsavai@Sun.COM 10538275SEric Cheng i_mac_log_link_state(mip); 105410491SRishi.Srivatsavai@Sun.COM newstate = mip->mi_lowlinkstate; 105510491SRishi.Srivatsavai@Sun.COM if (mip->mi_bridge_link != NULL) { 105610491SRishi.Srivatsavai@Sun.COM mutex_enter(&mip->mi_bridge_lock); 105710491SRishi.Srivatsavai@Sun.COM if ((mh = mip->mi_bridge_link) != NULL) { 105810491SRishi.Srivatsavai@Sun.COM newstate = mac_bridge_ls_cb(mh, 105910491SRishi.Srivatsavai@Sun.COM newstate); 106010491SRishi.Srivatsavai@Sun.COM } 106110491SRishi.Srivatsavai@Sun.COM mutex_exit(&mip->mi_bridge_lock); 106210491SRishi.Srivatsavai@Sun.COM } 106310491SRishi.Srivatsavai@Sun.COM if (newstate != mip->mi_linkstate) { 106410491SRishi.Srivatsavai@Sun.COM mip->mi_linkstate = newstate; 106510491SRishi.Srivatsavai@Sun.COM bits |= 1 << MAC_NOTE_LINK; 106610491SRishi.Srivatsavai@Sun.COM } 106710491SRishi.Srivatsavai@Sun.COM } 10688275SEric Cheng 10698275SEric Cheng /* 10708275SEric Cheng * Do notification callbacks for each notification type. 10718275SEric Cheng */ 10728275SEric Cheng for (type = 0; type < MAC_NNOTE; type++) { 10738275SEric Cheng if ((bits & (1 << type)) == 0) { 10748275SEric Cheng continue; 10758275SEric Cheng } 10768275SEric Cheng 107710491SRishi.Srivatsavai@Sun.COM if (mac_notify_cb_list[type] != NULL) 107810491SRishi.Srivatsavai@Sun.COM (*mac_notify_cb_list[type])(mip); 10798275SEric Cheng 10808275SEric Cheng /* 10818275SEric Cheng * Walk the list of notifications. 10828275SEric Cheng */ 10838275SEric Cheng MAC_CALLBACK_WALKER_INC(&mip->mi_notify_cb_info); 10848275SEric Cheng for (mcb = mip->mi_notify_cb_list; mcb != NULL; 10858275SEric Cheng mcb = mcb->mcb_nextp) { 10868275SEric Cheng mncb = (mac_notify_cb_t *)mcb->mcb_objp; 10878275SEric Cheng mncb->mncb_fn(mncb->mncb_arg, type); 10888275SEric Cheng } 10898275SEric Cheng MAC_CALLBACK_WALKER_DCR(&mip->mi_notify_cb_info, 10908275SEric Cheng &mip->mi_notify_cb_list); 10918275SEric Cheng } 10928275SEric Cheng 10938275SEric Cheng mutex_enter(mcbi->mcbi_lockp); 10948275SEric Cheng } 10958275SEric Cheng 10968275SEric Cheng mip->mi_state_flags |= MIS_NOTIFY_DONE; 10978275SEric Cheng cv_broadcast(&mcbi->mcbi_cv); 10988275SEric Cheng 10998275SEric Cheng /* CALLB_CPR_EXIT drops the lock */ 11008275SEric Cheng CALLB_CPR_EXIT(&cprinfo); 11018275SEric Cheng thread_exit(); 11028275SEric Cheng } 11038275SEric Cheng 11048275SEric Cheng /* 11058275SEric Cheng * Signal the i_mac_notify_thread asking it to quit. 11068275SEric Cheng * Then wait till it is done. 11078275SEric Cheng */ 11088275SEric Cheng void 11098275SEric Cheng i_mac_notify_exit(mac_impl_t *mip) 11108275SEric Cheng { 11118275SEric Cheng mac_cb_info_t *mcbi; 11128275SEric Cheng 11138275SEric Cheng mcbi = &mip->mi_notify_cb_info; 11148275SEric Cheng 11158275SEric Cheng mutex_enter(mcbi->mcbi_lockp); 11168275SEric Cheng mip->mi_notify_bits = (1 << MAC_NNOTE); 11178275SEric Cheng cv_broadcast(&mcbi->mcbi_cv); 11188275SEric Cheng 11198275SEric Cheng 11208275SEric Cheng while ((mip->mi_notify_thread != NULL) && 11218275SEric Cheng !(mip->mi_state_flags & MIS_NOTIFY_DONE)) { 11228275SEric Cheng cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 11238275SEric Cheng } 11248275SEric Cheng 11258275SEric Cheng /* Necessary clean up before doing kmem_cache_free */ 11268275SEric Cheng mip->mi_state_flags &= ~MIS_NOTIFY_DONE; 11278275SEric Cheng mip->mi_notify_bits = 0; 11288275SEric Cheng mip->mi_notify_thread = NULL; 11298275SEric Cheng mutex_exit(mcbi->mcbi_lockp); 11308275SEric Cheng } 11318275SEric Cheng 11328275SEric Cheng /* 11338275SEric Cheng * Entry point invoked by drivers to dynamically add a ring to an 11348275SEric Cheng * existing group. 11358275SEric Cheng */ 11368275SEric Cheng int 11378275SEric Cheng mac_group_add_ring(mac_group_handle_t gh, int index) 11388275SEric Cheng { 11398275SEric Cheng mac_group_t *group = (mac_group_t *)gh; 11408275SEric Cheng mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 11418275SEric Cheng int ret; 11428275SEric Cheng 11438275SEric Cheng i_mac_perim_enter(mip); 11448275SEric Cheng 11458275SEric Cheng /* 11468275SEric Cheng * Only RX rings can be added or removed by drivers currently. 11478275SEric Cheng */ 11488275SEric Cheng ASSERT(group->mrg_type == MAC_RING_TYPE_RX); 11498275SEric Cheng 11508275SEric Cheng ret = i_mac_group_add_ring(group, NULL, index); 11518275SEric Cheng 11528275SEric Cheng i_mac_perim_exit(mip); 11538275SEric Cheng 11548275SEric Cheng return (ret); 11558275SEric Cheng } 11568275SEric Cheng 11578275SEric Cheng /* 11588275SEric Cheng * Entry point invoked by drivers to dynamically remove a ring 11598275SEric Cheng * from an existing group. The specified ring handle must no longer 11608275SEric Cheng * be used by the driver after a call to this function. 11618275SEric Cheng */ 11628275SEric Cheng void 11638275SEric Cheng mac_group_rem_ring(mac_group_handle_t gh, mac_ring_handle_t rh) 11648275SEric Cheng { 11658275SEric Cheng mac_group_t *group = (mac_group_t *)gh; 11668275SEric Cheng mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 11678275SEric Cheng 11688275SEric Cheng i_mac_perim_enter(mip); 11698275SEric Cheng 11708275SEric Cheng /* 11718275SEric Cheng * Only RX rings can be added or removed by drivers currently. 11728275SEric Cheng */ 11738275SEric Cheng ASSERT(group->mrg_type == MAC_RING_TYPE_RX); 11748275SEric Cheng 11758275SEric Cheng i_mac_group_rem_ring(group, (mac_ring_t *)rh, B_TRUE); 11768275SEric Cheng 11778275SEric Cheng i_mac_perim_exit(mip); 11788275SEric Cheng } 1179