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> 43*10654SGarrett.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 1318275SEric Cheng /* Find the required MAC-Type plugin. */ 1328275SEric Cheng if ((mtype = mactype_getplugin(mregp->m_type_ident)) == NULL) 1338275SEric Cheng return (EINVAL); 1348275SEric Cheng 1358275SEric Cheng /* Create a mac_impl_t to represent this MAC. */ 1368275SEric Cheng mip = kmem_cache_alloc(i_mac_impl_cachep, KM_SLEEP); 1378275SEric Cheng 1388275SEric Cheng /* 1398275SEric Cheng * The mac is not ready for open yet. 1408275SEric Cheng */ 1418275SEric Cheng mip->mi_state_flags |= MIS_DISABLED; 1428275SEric Cheng 1438275SEric Cheng /* 1448275SEric Cheng * When a mac is registered, the m_instance field can be set to: 1458275SEric Cheng * 1468275SEric Cheng * 0: Get the mac's instance number from m_dip. 1478275SEric Cheng * This is usually used for physical device dips. 1488275SEric Cheng * 1498275SEric Cheng * [1 .. MAC_MAX_MINOR-1]: Use the value as the mac's instance number. 1508275SEric Cheng * For example, when an aggregation is created with the key option, 1518275SEric Cheng * "key" will be used as the instance number. 1528275SEric Cheng * 1538275SEric Cheng * -1: Assign an instance number from [MAC_MAX_MINOR .. MAXMIN-1]. 1548275SEric Cheng * This is often used when a MAC of a virtual link is registered 1558275SEric Cheng * (e.g., aggregation when "key" is not specified, or vnic). 1568275SEric Cheng * 1578275SEric Cheng * Note that the instance number is used to derive the mi_minor field 1588275SEric Cheng * of mac_impl_t, which will then be used to derive the name of kstats 1598275SEric Cheng * and the devfs nodes. The first 2 cases are needed to preserve 1608275SEric Cheng * backward compatibility. 1618275SEric Cheng */ 1628275SEric Cheng switch (mregp->m_instance) { 1638275SEric Cheng case 0: 1648275SEric Cheng instance = ddi_get_instance(mregp->m_dip); 1658275SEric Cheng break; 1668275SEric Cheng case ((uint_t)-1): 1678275SEric Cheng minor = mac_minor_hold(B_TRUE); 1688275SEric Cheng if (minor == 0) { 1698275SEric Cheng err = ENOSPC; 1708275SEric Cheng goto fail; 1718275SEric Cheng } 1728275SEric Cheng instance = minor - 1; 1738275SEric Cheng break; 1748275SEric Cheng default: 1758275SEric Cheng instance = mregp->m_instance; 1768275SEric Cheng if (instance >= MAC_MAX_MINOR) { 1778275SEric Cheng err = EINVAL; 1788275SEric Cheng goto fail; 1798275SEric Cheng } 1808275SEric Cheng break; 1818275SEric Cheng } 1828275SEric Cheng 1838275SEric Cheng mip->mi_minor = (minor_t)(instance + 1); 1848275SEric Cheng mip->mi_dip = mregp->m_dip; 1858275SEric Cheng mip->mi_clients_list = NULL; 1868275SEric Cheng mip->mi_nclients = 0; 1878275SEric Cheng 18810491SRishi.Srivatsavai@Sun.COM /* Set the default IEEE Port VLAN Identifier */ 18910491SRishi.Srivatsavai@Sun.COM mip->mi_pvid = 1; 19010491SRishi.Srivatsavai@Sun.COM 19110491SRishi.Srivatsavai@Sun.COM /* Default bridge link learning protection values */ 19210491SRishi.Srivatsavai@Sun.COM mip->mi_llimit = 1000; 19310491SRishi.Srivatsavai@Sun.COM mip->mi_ldecay = 200; 19410491SRishi.Srivatsavai@Sun.COM 1958275SEric Cheng driver = (char *)ddi_driver_name(mip->mi_dip); 1968275SEric Cheng 1978275SEric Cheng /* Construct the MAC name as <drvname><instance> */ 1988275SEric Cheng (void) snprintf(mip->mi_name, sizeof (mip->mi_name), "%s%d", 1998275SEric Cheng driver, instance); 2008275SEric Cheng 2018275SEric Cheng mip->mi_driver = mregp->m_driver; 2028275SEric Cheng 2038275SEric Cheng mip->mi_type = mtype; 2048275SEric Cheng mip->mi_margin = mregp->m_margin; 2058275SEric Cheng mip->mi_info.mi_media = mtype->mt_type; 2068275SEric Cheng mip->mi_info.mi_nativemedia = mtype->mt_nativetype; 2078275SEric Cheng if (mregp->m_max_sdu <= mregp->m_min_sdu) 2088275SEric Cheng goto fail; 2098275SEric Cheng mip->mi_sdu_min = mregp->m_min_sdu; 2108275SEric Cheng mip->mi_sdu_max = mregp->m_max_sdu; 2118275SEric Cheng mip->mi_info.mi_addr_length = mip->mi_type->mt_addr_length; 2128275SEric Cheng /* 2138275SEric Cheng * If the media supports a broadcast address, cache a pointer to it 2148275SEric Cheng * in the mac_info_t so that upper layers can use it. 2158275SEric Cheng */ 2168275SEric Cheng mip->mi_info.mi_brdcst_addr = mip->mi_type->mt_brdcst_addr; 2178275SEric Cheng 2188275SEric Cheng mip->mi_v12n_level = mregp->m_v12n; 2198275SEric Cheng 2208275SEric Cheng /* 2218275SEric Cheng * Copy the unicast source address into the mac_info_t, but only if 2228275SEric Cheng * the MAC-Type defines a non-zero address length. We need to 2238275SEric Cheng * handle MAC-Types that have an address length of 0 2248275SEric Cheng * (point-to-point protocol MACs for example). 2258275SEric Cheng */ 2268275SEric Cheng if (mip->mi_type->mt_addr_length > 0) { 2278275SEric Cheng if (mregp->m_src_addr == NULL) 2288275SEric Cheng goto fail; 2298275SEric Cheng mip->mi_info.mi_unicst_addr = 2308275SEric Cheng kmem_alloc(mip->mi_type->mt_addr_length, KM_SLEEP); 2318275SEric Cheng bcopy(mregp->m_src_addr, mip->mi_info.mi_unicst_addr, 2328275SEric Cheng mip->mi_type->mt_addr_length); 2338275SEric Cheng 2348275SEric Cheng /* 2358275SEric Cheng * Copy the fixed 'factory' MAC address from the immutable 2368275SEric Cheng * info. This is taken to be the MAC address currently in 2378275SEric Cheng * use. 2388275SEric Cheng */ 2398275SEric Cheng bcopy(mip->mi_info.mi_unicst_addr, mip->mi_addr, 2408275SEric Cheng mip->mi_type->mt_addr_length); 2418275SEric Cheng 2428275SEric Cheng /* 2438275SEric Cheng * At this point, we should set up the classification 2448275SEric Cheng * rules etc but we delay it till mac_open() so that 2458275SEric Cheng * the resource discovery has taken place and we 2468275SEric Cheng * know someone wants to use the device. Otherwise 2478275SEric Cheng * memory gets allocated for Rx ring structures even 2488275SEric Cheng * during probe. 2498275SEric Cheng */ 2508275SEric Cheng 2518275SEric Cheng /* Copy the destination address if one is provided. */ 2528275SEric Cheng if (mregp->m_dst_addr != NULL) { 2538275SEric Cheng bcopy(mregp->m_dst_addr, mip->mi_dstaddr, 2548275SEric Cheng mip->mi_type->mt_addr_length); 25510616SSebastien.Roy@Sun.COM mip->mi_dstaddr_set = B_TRUE; 2568275SEric Cheng } 2578275SEric Cheng } else if (mregp->m_src_addr != NULL) { 2588275SEric Cheng goto fail; 2598275SEric Cheng } 2608275SEric Cheng 2618275SEric Cheng /* 2628275SEric Cheng * The format of the m_pdata is specific to the plugin. It is 2638275SEric Cheng * passed in as an argument to all of the plugin callbacks. The 2648275SEric Cheng * driver can update this information by calling 2658275SEric Cheng * mac_pdata_update(). 2668275SEric Cheng */ 26710616SSebastien.Roy@Sun.COM if (mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY) { 2688275SEric Cheng /* 26910616SSebastien.Roy@Sun.COM * Verify if the supplied plugin data is valid. Note that 27010616SSebastien.Roy@Sun.COM * even if the caller passed in a NULL pointer as plugin data, 27110616SSebastien.Roy@Sun.COM * we still need to verify if that's valid as the plugin may 27210616SSebastien.Roy@Sun.COM * require plugin data to function. 2738275SEric Cheng */ 2748275SEric Cheng if (!mip->mi_type->mt_ops.mtops_pdata_verify(mregp->m_pdata, 2758275SEric Cheng mregp->m_pdata_size)) { 2768275SEric Cheng goto fail; 2778275SEric Cheng } 27810616SSebastien.Roy@Sun.COM if (mregp->m_pdata != NULL) { 27910616SSebastien.Roy@Sun.COM mip->mi_pdata = 28010616SSebastien.Roy@Sun.COM kmem_alloc(mregp->m_pdata_size, KM_SLEEP); 28110616SSebastien.Roy@Sun.COM bcopy(mregp->m_pdata, mip->mi_pdata, 28210616SSebastien.Roy@Sun.COM mregp->m_pdata_size); 28310616SSebastien.Roy@Sun.COM mip->mi_pdata_size = mregp->m_pdata_size; 28410616SSebastien.Roy@Sun.COM } 28510616SSebastien.Roy@Sun.COM } else if (mregp->m_pdata != NULL) { 28610616SSebastien.Roy@Sun.COM /* 28710616SSebastien.Roy@Sun.COM * The caller supplied non-NULL plugin data, but the plugin 28810616SSebastien.Roy@Sun.COM * does not recognize plugin data. 28910616SSebastien.Roy@Sun.COM */ 29010616SSebastien.Roy@Sun.COM err = EINVAL; 29110616SSebastien.Roy@Sun.COM goto fail; 2928275SEric Cheng } 2938275SEric Cheng 2948275SEric Cheng /* 2958275SEric Cheng * Register the private properties. 2968275SEric Cheng */ 2978275SEric Cheng mac_register_priv_prop(mip, mregp->m_priv_props, 2988275SEric Cheng mregp->m_priv_prop_count); 2998275SEric Cheng 3008275SEric Cheng /* 3018275SEric Cheng * Stash the driver callbacks into the mac_impl_t, but first sanity 3028275SEric Cheng * check to make sure all mandatory callbacks are set. 3038275SEric Cheng */ 3048275SEric Cheng if (mregp->m_callbacks->mc_getstat == NULL || 3058275SEric Cheng mregp->m_callbacks->mc_start == NULL || 3068275SEric Cheng mregp->m_callbacks->mc_stop == NULL || 3078275SEric Cheng mregp->m_callbacks->mc_setpromisc == NULL || 3088275SEric Cheng mregp->m_callbacks->mc_multicst == NULL) { 3098275SEric Cheng goto fail; 3108275SEric Cheng } 3118275SEric Cheng mip->mi_callbacks = mregp->m_callbacks; 3128275SEric Cheng 3139073SCathy.Zhou@Sun.COM if (mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LEGACY, 3149073SCathy.Zhou@Sun.COM &mip->mi_capab_legacy)) { 3158275SEric Cheng mip->mi_state_flags |= MIS_LEGACY; 3169073SCathy.Zhou@Sun.COM mip->mi_phy_dev = mip->mi_capab_legacy.ml_dev; 3178275SEric Cheng } else { 3188275SEric Cheng mip->mi_phy_dev = makedevice(ddi_driver_major(mip->mi_dip), 319*10654SGarrett.Damore@Sun.COM mip->mi_minor); 3208275SEric Cheng } 3218275SEric Cheng 3228275SEric Cheng /* 3238275SEric Cheng * Allocate a notification thread. thread_create blocks for memory 3248275SEric Cheng * if needed, it never fails. 3258275SEric Cheng */ 3268275SEric Cheng mip->mi_notify_thread = thread_create(NULL, 0, i_mac_notify_thread, 3278275SEric Cheng mip, 0, &p0, TS_RUN, minclsyspri); 3288275SEric Cheng 3298275SEric Cheng /* 3308275SEric Cheng * Initialize the capabilities 3318275SEric Cheng */ 3328275SEric Cheng 3338275SEric Cheng if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, NULL)) 3348275SEric Cheng mip->mi_state_flags |= MIS_IS_VNIC; 3358275SEric Cheng 3368275SEric Cheng if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR, NULL)) 3378275SEric Cheng mip->mi_state_flags |= MIS_IS_AGGR; 3388275SEric Cheng 3398275SEric Cheng mac_addr_factory_init(mip); 3408275SEric Cheng 3418275SEric Cheng /* 3428275SEric Cheng * Enforce the virtrualization level registered. 3438275SEric Cheng */ 3448275SEric Cheng if (mip->mi_v12n_level & MAC_VIRT_LEVEL1) { 3458275SEric Cheng if (mac_init_rings(mip, MAC_RING_TYPE_RX) != 0 || 3468275SEric Cheng mac_init_rings(mip, MAC_RING_TYPE_TX) != 0) 3478275SEric Cheng goto fail; 3488275SEric Cheng 3498275SEric Cheng /* 3508275SEric Cheng * The driver needs to register at least rx rings for this 3518275SEric Cheng * virtualization level. 3528275SEric Cheng */ 3538275SEric Cheng if (mip->mi_rx_groups == NULL) 3548275SEric Cheng goto fail; 3558275SEric Cheng } 3568275SEric Cheng 3578275SEric Cheng /* 3588275SEric Cheng * The driver must set mc_unicst entry point to NULL when it advertises 3598275SEric Cheng * CAP_RINGS for rx groups. 3608275SEric Cheng */ 3618275SEric Cheng if (mip->mi_rx_groups != NULL) { 3628275SEric Cheng if (mregp->m_callbacks->mc_unicst != NULL) 3638275SEric Cheng goto fail; 3648275SEric Cheng } else { 3658275SEric Cheng if (mregp->m_callbacks->mc_unicst == NULL) 3668275SEric Cheng goto fail; 3678275SEric Cheng } 3688275SEric Cheng 3698275SEric Cheng /* 3708275SEric Cheng * The driver must set mc_tx entry point to NULL when it advertises 3718275SEric Cheng * CAP_RINGS for tx rings. 3728275SEric Cheng */ 3738275SEric Cheng if (mip->mi_tx_groups != NULL) { 3748275SEric Cheng if (mregp->m_callbacks->mc_tx != NULL) 3758275SEric Cheng goto fail; 3768275SEric Cheng } else { 3778275SEric Cheng if (mregp->m_callbacks->mc_tx == NULL) 3788275SEric Cheng goto fail; 3798275SEric Cheng } 3808275SEric Cheng 3818275SEric Cheng /* 3828275SEric Cheng * Initialize MAC addresses. Must be called after mac_init_rings(). 3838275SEric Cheng */ 3848275SEric Cheng mac_init_macaddr(mip); 3858275SEric Cheng 3868275SEric Cheng mip->mi_share_capab.ms_snum = 0; 3878275SEric Cheng if (mip->mi_v12n_level & MAC_VIRT_HIO) { 3888275SEric Cheng (void) mac_capab_get((mac_handle_t)mip, MAC_CAPAB_SHARES, 3898275SEric Cheng &mip->mi_share_capab); 3908275SEric Cheng } 3918275SEric Cheng 3928275SEric Cheng /* 3938275SEric Cheng * Initialize the kstats for this device. 3948275SEric Cheng */ 3958275SEric Cheng mac_stat_create(mip); 3968275SEric Cheng 3978275SEric Cheng /* Zero out any properties. */ 3988275SEric Cheng bzero(&mip->mi_resource_props, sizeof (mac_resource_props_t)); 3998275SEric Cheng 4008275SEric Cheng /* set the gldv3 flag in dn_flags */ 4018275SEric Cheng dnp = &devnamesp[ddi_driver_major(mip->mi_dip)]; 4028275SEric Cheng LOCK_DEV_OPS(&dnp->dn_lock); 4038275SEric Cheng dnp->dn_flags |= (DN_GLDV3_DRIVER | DN_NETWORK_DRIVER); 4048275SEric Cheng UNLOCK_DEV_OPS(&dnp->dn_lock); 4058275SEric Cheng 4068275SEric Cheng if (mip->mi_minor < MAC_MAX_MINOR + 1) { 4078275SEric Cheng /* Create a style-2 DLPI device */ 4088275SEric Cheng if (ddi_create_minor_node(mip->mi_dip, driver, S_IFCHR, 0, 4098275SEric Cheng DDI_NT_NET, CLONE_DEV) != DDI_SUCCESS) 4108275SEric Cheng goto fail; 4118275SEric Cheng style2_created = B_TRUE; 4128275SEric Cheng 4138275SEric Cheng /* Create a style-1 DLPI device */ 4148275SEric Cheng if (ddi_create_minor_node(mip->mi_dip, mip->mi_name, S_IFCHR, 4158275SEric Cheng mip->mi_minor, DDI_NT_NET, 0) != DDI_SUCCESS) 4168275SEric Cheng goto fail; 4178275SEric Cheng style1_created = B_TRUE; 4188275SEric Cheng } 4198275SEric Cheng 4208275SEric Cheng mac_flow_l2tab_create(mip, &mip->mi_flow_tab); 4218275SEric Cheng 4228275SEric Cheng rw_enter(&i_mac_impl_lock, RW_WRITER); 4238275SEric Cheng if (mod_hash_insert(i_mac_impl_hash, 4248275SEric Cheng (mod_hash_key_t)mip->mi_name, (mod_hash_val_t)mip) != 0) { 4258275SEric Cheng rw_exit(&i_mac_impl_lock); 4268275SEric Cheng err = EEXIST; 4278275SEric Cheng goto fail; 4288275SEric Cheng } 4298275SEric Cheng 4308275SEric Cheng DTRACE_PROBE2(mac__register, struct devnames *, dnp, 4318275SEric Cheng (mac_impl_t *), mip); 4328275SEric Cheng 4338275SEric Cheng /* 4348275SEric Cheng * Mark the MAC to be ready for open. 4358275SEric Cheng */ 4368275SEric Cheng mip->mi_state_flags &= ~MIS_DISABLED; 4378275SEric Cheng rw_exit(&i_mac_impl_lock); 4388275SEric Cheng 4398275SEric Cheng atomic_inc_32(&i_mac_impl_count); 4408275SEric Cheng 4418275SEric Cheng cmn_err(CE_NOTE, "!%s registered", mip->mi_name); 4428275SEric Cheng *mhp = (mac_handle_t)mip; 4438275SEric Cheng return (0); 4448275SEric Cheng 4458275SEric Cheng fail: 4468275SEric Cheng if (style1_created) 4478275SEric Cheng ddi_remove_minor_node(mip->mi_dip, mip->mi_name); 4488275SEric Cheng 4498275SEric Cheng if (style2_created) 4508275SEric Cheng ddi_remove_minor_node(mip->mi_dip, driver); 4518275SEric Cheng 4528275SEric Cheng mac_addr_factory_fini(mip); 4538275SEric Cheng 4548275SEric Cheng /* Clean up registered MAC addresses */ 4558275SEric Cheng mac_fini_macaddr(mip); 4568275SEric Cheng 4578275SEric Cheng /* Clean up registered rings */ 4588275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_RX); 4598275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_TX); 4608275SEric Cheng 4618275SEric Cheng /* Clean up notification thread */ 4628275SEric Cheng if (mip->mi_notify_thread != NULL) 4638275SEric Cheng i_mac_notify_exit(mip); 4648275SEric Cheng 4658275SEric Cheng if (mip->mi_info.mi_unicst_addr != NULL) { 4668275SEric Cheng kmem_free(mip->mi_info.mi_unicst_addr, 4678275SEric Cheng mip->mi_type->mt_addr_length); 4688275SEric Cheng mip->mi_info.mi_unicst_addr = NULL; 4698275SEric Cheng } 4708275SEric Cheng 4718275SEric Cheng mac_stat_destroy(mip); 4728275SEric Cheng 4738275SEric Cheng if (mip->mi_type != NULL) { 4748275SEric Cheng atomic_dec_32(&mip->mi_type->mt_ref); 4758275SEric Cheng mip->mi_type = NULL; 4768275SEric Cheng } 4778275SEric Cheng 4788275SEric Cheng if (mip->mi_pdata != NULL) { 4798275SEric Cheng kmem_free(mip->mi_pdata, mip->mi_pdata_size); 4808275SEric Cheng mip->mi_pdata = NULL; 4818275SEric Cheng mip->mi_pdata_size = 0; 4828275SEric Cheng } 4838275SEric Cheng 4848275SEric Cheng if (minor != 0) { 4858275SEric Cheng ASSERT(minor > MAC_MAX_MINOR); 4868275SEric Cheng mac_minor_rele(minor); 4878275SEric Cheng } 4888275SEric Cheng 4898275SEric Cheng mac_unregister_priv_prop(mip); 4908275SEric Cheng 4918275SEric Cheng kmem_cache_free(i_mac_impl_cachep, mip); 4928275SEric Cheng return (err); 4938275SEric Cheng } 4948275SEric Cheng 4958275SEric Cheng /* 4968275SEric Cheng * Unregister from the GLDv3 framework 4978275SEric Cheng */ 4988275SEric Cheng int 4998275SEric Cheng mac_unregister(mac_handle_t mh) 5008275SEric Cheng { 5018275SEric Cheng int err; 5028275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 5038275SEric Cheng mod_hash_val_t val; 5048275SEric Cheng mac_margin_req_t *mmr, *nextmmr; 5058275SEric Cheng 5068275SEric Cheng /* Fail the unregister if there are any open references to this mac. */ 5078275SEric Cheng if ((err = mac_disable_nowait(mh)) != 0) 5088275SEric Cheng return (err); 5098275SEric Cheng 5108275SEric Cheng /* 5118275SEric Cheng * Clean up notification thread and wait for it to exit. 5128275SEric Cheng */ 5138275SEric Cheng i_mac_notify_exit(mip); 5148275SEric Cheng 5158275SEric Cheng i_mac_perim_enter(mip); 5168275SEric Cheng 5179073SCathy.Zhou@Sun.COM /* 5189073SCathy.Zhou@Sun.COM * There is still resource properties configured over this mac. 5199073SCathy.Zhou@Sun.COM */ 5209073SCathy.Zhou@Sun.COM if (mip->mi_resource_props.mrp_mask != 0) 5219073SCathy.Zhou@Sun.COM mac_fastpath_enable((mac_handle_t)mip); 5229073SCathy.Zhou@Sun.COM 5238275SEric Cheng if (mip->mi_minor < MAC_MAX_MINOR + 1) { 5248275SEric Cheng ddi_remove_minor_node(mip->mi_dip, mip->mi_name); 5258275SEric Cheng ddi_remove_minor_node(mip->mi_dip, 5268275SEric Cheng (char *)ddi_driver_name(mip->mi_dip)); 5278275SEric Cheng } 5288275SEric Cheng 5298275SEric Cheng ASSERT(mip->mi_nactiveclients == 0 && !(mip->mi_state_flags & 5308275SEric Cheng MIS_EXCLUSIVE)); 5318275SEric Cheng 5328275SEric Cheng mac_stat_destroy(mip); 5338275SEric Cheng 5348275SEric Cheng (void) mod_hash_remove(i_mac_impl_hash, 5358275SEric Cheng (mod_hash_key_t)mip->mi_name, &val); 5368275SEric Cheng ASSERT(mip == (mac_impl_t *)val); 5378275SEric Cheng 5388275SEric Cheng ASSERT(i_mac_impl_count > 0); 5398275SEric Cheng atomic_dec_32(&i_mac_impl_count); 5408275SEric Cheng 5418275SEric Cheng if (mip->mi_pdata != NULL) 5428275SEric Cheng kmem_free(mip->mi_pdata, mip->mi_pdata_size); 5438275SEric Cheng mip->mi_pdata = NULL; 5448275SEric Cheng mip->mi_pdata_size = 0; 5458275SEric Cheng 5468275SEric Cheng /* 5478275SEric Cheng * Free the list of margin request. 5488275SEric Cheng */ 5498275SEric Cheng for (mmr = mip->mi_mmrp; mmr != NULL; mmr = nextmmr) { 5508275SEric Cheng nextmmr = mmr->mmr_nextp; 5518275SEric Cheng kmem_free(mmr, sizeof (mac_margin_req_t)); 5528275SEric Cheng } 5538275SEric Cheng mip->mi_mmrp = NULL; 5548275SEric Cheng 55510491SRishi.Srivatsavai@Sun.COM mip->mi_linkstate = mip->mi_lowlinkstate = LINK_STATE_UNKNOWN; 5568275SEric Cheng kmem_free(mip->mi_info.mi_unicst_addr, mip->mi_type->mt_addr_length); 5578275SEric Cheng mip->mi_info.mi_unicst_addr = NULL; 5588275SEric Cheng 5598275SEric Cheng atomic_dec_32(&mip->mi_type->mt_ref); 5608275SEric Cheng mip->mi_type = NULL; 5618275SEric Cheng 5628275SEric Cheng /* 5638275SEric Cheng * Free the primary MAC address. 5648275SEric Cheng */ 5658275SEric Cheng mac_fini_macaddr(mip); 5668275SEric Cheng 5678275SEric Cheng /* 5688275SEric Cheng * free all rings 5698275SEric Cheng */ 5708275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_RX); 5718275SEric Cheng mac_free_rings(mip, MAC_RING_TYPE_TX); 5728275SEric Cheng 5738275SEric Cheng mac_addr_factory_fini(mip); 5748275SEric Cheng 5758275SEric Cheng bzero(mip->mi_addr, MAXMACADDRLEN); 5768275SEric Cheng bzero(mip->mi_dstaddr, MAXMACADDRLEN); 5778275SEric Cheng 5788275SEric Cheng /* and the flows */ 5798275SEric Cheng mac_flow_tab_destroy(mip->mi_flow_tab); 5808275SEric Cheng mip->mi_flow_tab = NULL; 5818275SEric Cheng 5828275SEric Cheng if (mip->mi_minor > MAC_MAX_MINOR) 5838275SEric Cheng mac_minor_rele(mip->mi_minor); 5848275SEric Cheng 5858275SEric Cheng cmn_err(CE_NOTE, "!%s unregistered", mip->mi_name); 5868275SEric Cheng 5878275SEric Cheng /* 5888275SEric Cheng * Reset the perim related fields to default values before 5898275SEric Cheng * kmem_cache_free 5908275SEric Cheng */ 5918275SEric Cheng i_mac_perim_exit(mip); 5928275SEric Cheng mip->mi_state_flags = 0; 5938275SEric Cheng 5948275SEric Cheng mac_unregister_priv_prop(mip); 59510491SRishi.Srivatsavai@Sun.COM 59610491SRishi.Srivatsavai@Sun.COM ASSERT(mip->mi_bridge_link == NULL); 5978275SEric Cheng kmem_cache_free(i_mac_impl_cachep, mip); 5988275SEric Cheng 5998275SEric Cheng return (0); 6008275SEric Cheng } 6018275SEric Cheng 6028275SEric Cheng /* DATA RECEPTION */ 6038275SEric Cheng 6048275SEric Cheng /* 6058275SEric Cheng * This function is invoked for packets received by the MAC driver in 6068275SEric Cheng * interrupt context. The ring generation number provided by the driver 6078275SEric Cheng * is matched with the ring generation number held in MAC. If they do not 6088275SEric Cheng * match, received packets are considered stale packets coming from an older 6098275SEric Cheng * assignment of the ring. Drop them. 6108275SEric Cheng */ 6118275SEric Cheng void 6128275SEric Cheng mac_rx_ring(mac_handle_t mh, mac_ring_handle_t mrh, mblk_t *mp_chain, 6138275SEric Cheng uint64_t mr_gen_num) 6148275SEric Cheng { 6158275SEric Cheng mac_ring_t *mr = (mac_ring_t *)mrh; 6168275SEric Cheng 6178275SEric Cheng if ((mr != NULL) && (mr->mr_gen_num != mr_gen_num)) { 6188275SEric Cheng DTRACE_PROBE2(mac__rx__rings__stale__packet, uint64_t, 6198275SEric Cheng mr->mr_gen_num, uint64_t, mr_gen_num); 6208275SEric Cheng freemsgchain(mp_chain); 6218275SEric Cheng return; 6228275SEric Cheng } 6238275SEric Cheng mac_rx(mh, (mac_resource_handle_t)mrh, mp_chain); 6248275SEric Cheng } 6258275SEric Cheng 6268275SEric Cheng /* 62710491SRishi.Srivatsavai@Sun.COM * This function is invoked for each packet received by the underlying driver. 6288275SEric Cheng */ 6298275SEric Cheng void 6308275SEric Cheng mac_rx(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain) 6318275SEric Cheng { 63210491SRishi.Srivatsavai@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 63310491SRishi.Srivatsavai@Sun.COM 63410491SRishi.Srivatsavai@Sun.COM /* 63510491SRishi.Srivatsavai@Sun.COM * Check if the link is part of a bridge. If not, then we don't need 63610491SRishi.Srivatsavai@Sun.COM * to take the lock to remain consistent. Make this common case 63710491SRishi.Srivatsavai@Sun.COM * lock-free and tail-call optimized. 63810491SRishi.Srivatsavai@Sun.COM */ 63910491SRishi.Srivatsavai@Sun.COM if (mip->mi_bridge_link == NULL) { 64010491SRishi.Srivatsavai@Sun.COM mac_rx_common(mh, mrh, mp_chain); 64110491SRishi.Srivatsavai@Sun.COM } else { 64210491SRishi.Srivatsavai@Sun.COM /* 64310491SRishi.Srivatsavai@Sun.COM * Once we take a reference on the bridge link, the bridge 64410491SRishi.Srivatsavai@Sun.COM * module itself can't unload, so the callback pointers are 64510491SRishi.Srivatsavai@Sun.COM * stable. 64610491SRishi.Srivatsavai@Sun.COM */ 64710491SRishi.Srivatsavai@Sun.COM mutex_enter(&mip->mi_bridge_lock); 64810491SRishi.Srivatsavai@Sun.COM if ((mh = mip->mi_bridge_link) != NULL) 64910491SRishi.Srivatsavai@Sun.COM mac_bridge_ref_cb(mh, B_TRUE); 65010491SRishi.Srivatsavai@Sun.COM mutex_exit(&mip->mi_bridge_lock); 65110491SRishi.Srivatsavai@Sun.COM if (mh == NULL) { 65210491SRishi.Srivatsavai@Sun.COM mac_rx_common((mac_handle_t)mip, mrh, mp_chain); 65310491SRishi.Srivatsavai@Sun.COM } else { 65410491SRishi.Srivatsavai@Sun.COM mac_bridge_rx_cb(mh, mrh, mp_chain); 65510491SRishi.Srivatsavai@Sun.COM mac_bridge_ref_cb(mh, B_FALSE); 65610491SRishi.Srivatsavai@Sun.COM } 65710491SRishi.Srivatsavai@Sun.COM } 65810491SRishi.Srivatsavai@Sun.COM } 65910491SRishi.Srivatsavai@Sun.COM 66010491SRishi.Srivatsavai@Sun.COM /* 66110491SRishi.Srivatsavai@Sun.COM * Special case function: this allows snooping of packets transmitted and 66210491SRishi.Srivatsavai@Sun.COM * received by TRILL. By design, they go directly into the TRILL module. 66310491SRishi.Srivatsavai@Sun.COM */ 66410491SRishi.Srivatsavai@Sun.COM void 66510491SRishi.Srivatsavai@Sun.COM mac_trill_snoop(mac_handle_t mh, mblk_t *mp) 66610491SRishi.Srivatsavai@Sun.COM { 66710491SRishi.Srivatsavai@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 66810491SRishi.Srivatsavai@Sun.COM 66910491SRishi.Srivatsavai@Sun.COM if (mip->mi_promisc_list != NULL) 67010491SRishi.Srivatsavai@Sun.COM mac_promisc_dispatch(mip, mp, NULL); 67110491SRishi.Srivatsavai@Sun.COM } 67210491SRishi.Srivatsavai@Sun.COM 67310491SRishi.Srivatsavai@Sun.COM /* 67410491SRishi.Srivatsavai@Sun.COM * This is the upward reentry point for packets arriving from the bridging 67510491SRishi.Srivatsavai@Sun.COM * module and from mac_rx for links not part of a bridge. 67610491SRishi.Srivatsavai@Sun.COM */ 67710491SRishi.Srivatsavai@Sun.COM void 67810491SRishi.Srivatsavai@Sun.COM mac_rx_common(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain) 67910491SRishi.Srivatsavai@Sun.COM { 6808275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 6818275SEric Cheng mac_ring_t *mr = (mac_ring_t *)mrh; 6828275SEric Cheng mac_soft_ring_set_t *mac_srs; 6838275SEric Cheng mblk_t *bp = mp_chain; 6848275SEric Cheng boolean_t hw_classified = B_FALSE; 6858275SEric Cheng 6868275SEric Cheng /* 6878275SEric Cheng * If there are any promiscuous mode callbacks defined for 6888275SEric Cheng * this MAC, pass them a copy if appropriate. 6898275SEric Cheng */ 6908275SEric Cheng if (mip->mi_promisc_list != NULL) 6918275SEric Cheng mac_promisc_dispatch(mip, mp_chain, NULL); 6928275SEric Cheng 6938275SEric Cheng if (mr != NULL) { 6948275SEric Cheng /* 6958275SEric Cheng * If the SRS teardown has started, just return. The 'mr' 6968275SEric Cheng * continues to be valid until the driver unregisters the mac. 6978275SEric Cheng * Hardware classified packets will not make their way up 6988275SEric Cheng * beyond this point once the teardown has started. The driver 6998275SEric Cheng * is never passed a pointer to a flow entry or SRS or any 7008275SEric Cheng * structure that can be freed much before mac_unregister. 7018275SEric Cheng */ 7028275SEric Cheng mutex_enter(&mr->mr_lock); 7038275SEric Cheng if ((mr->mr_state != MR_INUSE) || (mr->mr_flag & 7048275SEric Cheng (MR_INCIPIENT | MR_CONDEMNED | MR_QUIESCE))) { 7058275SEric Cheng mutex_exit(&mr->mr_lock); 7068275SEric Cheng freemsgchain(mp_chain); 7078275SEric Cheng return; 7088275SEric Cheng } 7098275SEric Cheng if (mr->mr_classify_type == MAC_HW_CLASSIFIER) { 7108275SEric Cheng hw_classified = B_TRUE; 7118275SEric Cheng MR_REFHOLD_LOCKED(mr); 7128275SEric Cheng } 7138275SEric Cheng mutex_exit(&mr->mr_lock); 7148275SEric Cheng 7158275SEric Cheng /* 7168275SEric Cheng * We check if an SRS is controlling this ring. 7178275SEric Cheng * If so, we can directly call the srs_lower_proc 7188275SEric Cheng * routine otherwise we need to go through mac_rx_classify 7198275SEric Cheng * to reach the right place. 7208275SEric Cheng */ 7218275SEric Cheng if (hw_classified) { 7228275SEric Cheng mac_srs = mr->mr_srs; 7238275SEric Cheng /* 7248275SEric Cheng * This is supposed to be the fast path. 7258275SEric Cheng * All packets received though here were steered by 7268275SEric Cheng * the hardware classifier, and share the same 7278275SEric Cheng * MAC header info. 7288275SEric Cheng */ 7298275SEric Cheng mac_srs->srs_rx.sr_lower_proc(mh, 7308275SEric Cheng (mac_resource_handle_t)mac_srs, mp_chain, B_FALSE); 7318275SEric Cheng MR_REFRELE(mr); 7328275SEric Cheng return; 7338275SEric Cheng } 7348275SEric Cheng /* We'll fall through to software classification */ 7358833SVenu.Iyer@Sun.COM } else { 7368833SVenu.Iyer@Sun.COM flow_entry_t *flent; 7378833SVenu.Iyer@Sun.COM int err; 7388833SVenu.Iyer@Sun.COM 7398833SVenu.Iyer@Sun.COM rw_enter(&mip->mi_rw_lock, RW_READER); 7408833SVenu.Iyer@Sun.COM if (mip->mi_single_active_client != NULL) { 7418833SVenu.Iyer@Sun.COM flent = mip->mi_single_active_client->mci_flent_list; 7428833SVenu.Iyer@Sun.COM FLOW_TRY_REFHOLD(flent, err); 7438833SVenu.Iyer@Sun.COM rw_exit(&mip->mi_rw_lock); 7448833SVenu.Iyer@Sun.COM if (err == 0) { 7458833SVenu.Iyer@Sun.COM (flent->fe_cb_fn)(flent->fe_cb_arg1, 7468833SVenu.Iyer@Sun.COM flent->fe_cb_arg2, mp_chain, B_FALSE); 7478833SVenu.Iyer@Sun.COM FLOW_REFRELE(flent); 7488833SVenu.Iyer@Sun.COM return; 7498833SVenu.Iyer@Sun.COM } 7508833SVenu.Iyer@Sun.COM } else { 7518833SVenu.Iyer@Sun.COM rw_exit(&mip->mi_rw_lock); 7528833SVenu.Iyer@Sun.COM } 7538275SEric Cheng } 7548275SEric Cheng 7558275SEric Cheng if (!FLOW_TAB_EMPTY(mip->mi_flow_tab)) { 7568275SEric Cheng if ((bp = mac_rx_flow(mh, mrh, bp)) == NULL) 7578275SEric Cheng return; 7588275SEric Cheng } 7598275SEric Cheng 7608275SEric Cheng freemsgchain(bp); 7618275SEric Cheng } 7628275SEric Cheng 7638275SEric Cheng /* DATA TRANSMISSION */ 7648275SEric Cheng 7658275SEric Cheng /* 7668275SEric Cheng * A driver's notification to resume transmission, in case of a provider 7678275SEric Cheng * without TX rings. 7688275SEric Cheng */ 7698275SEric Cheng void 7708275SEric Cheng mac_tx_update(mac_handle_t mh) 7718275SEric Cheng { 7728275SEric Cheng /* 7738275SEric Cheng * Walk the list of MAC clients (mac_client_handle) 7748275SEric Cheng * and update 7758275SEric Cheng */ 7768275SEric Cheng i_mac_tx_srs_notify((mac_impl_t *)mh, NULL); 7778275SEric Cheng } 7788275SEric Cheng 7798275SEric Cheng /* 7808275SEric Cheng * A driver's notification to resume transmission on the specified TX ring. 7818275SEric Cheng */ 7828275SEric Cheng void 7838275SEric Cheng mac_tx_ring_update(mac_handle_t mh, mac_ring_handle_t rh) 7848275SEric Cheng { 7858275SEric Cheng i_mac_tx_srs_notify((mac_impl_t *)mh, rh); 7868275SEric Cheng } 7878275SEric Cheng 7888275SEric Cheng /* LINK STATE */ 7898275SEric Cheng /* 7908275SEric Cheng * Notify the MAC layer about a link state change 7918275SEric Cheng */ 7928275SEric Cheng void 7938275SEric Cheng mac_link_update(mac_handle_t mh, link_state_t link) 7948275SEric Cheng { 7958275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 7968275SEric Cheng 7978275SEric Cheng /* 7988275SEric Cheng * Save the link state. 7998275SEric Cheng */ 80010491SRishi.Srivatsavai@Sun.COM mip->mi_lowlinkstate = link; 80110491SRishi.Srivatsavai@Sun.COM 80210491SRishi.Srivatsavai@Sun.COM /* 80310491SRishi.Srivatsavai@Sun.COM * Send a MAC_NOTE_LOWLINK notification. This tells the notification 80410491SRishi.Srivatsavai@Sun.COM * thread to deliver both lower and upper notifications. 80510491SRishi.Srivatsavai@Sun.COM */ 80610491SRishi.Srivatsavai@Sun.COM i_mac_notify(mip, MAC_NOTE_LOWLINK); 80710491SRishi.Srivatsavai@Sun.COM } 80810491SRishi.Srivatsavai@Sun.COM 80910491SRishi.Srivatsavai@Sun.COM /* 81010491SRishi.Srivatsavai@Sun.COM * Notify the MAC layer about a link state change due to bridging. 81110491SRishi.Srivatsavai@Sun.COM */ 81210491SRishi.Srivatsavai@Sun.COM void 81310491SRishi.Srivatsavai@Sun.COM mac_link_redo(mac_handle_t mh, link_state_t link) 81410491SRishi.Srivatsavai@Sun.COM { 81510491SRishi.Srivatsavai@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 81610491SRishi.Srivatsavai@Sun.COM 81710491SRishi.Srivatsavai@Sun.COM /* 81810491SRishi.Srivatsavai@Sun.COM * Save the link state. 81910491SRishi.Srivatsavai@Sun.COM */ 8208275SEric Cheng mip->mi_linkstate = link; 8218275SEric Cheng 8228275SEric Cheng /* 82310491SRishi.Srivatsavai@Sun.COM * Send a MAC_NOTE_LINK notification. Only upper notifications are 82410491SRishi.Srivatsavai@Sun.COM * made. 8258275SEric Cheng */ 8268275SEric Cheng i_mac_notify(mip, MAC_NOTE_LINK); 8278275SEric Cheng } 8288275SEric Cheng 829*10654SGarrett.Damore@Sun.COM /* MINOR NODE HANDLING */ 830*10654SGarrett.Damore@Sun.COM 831*10654SGarrett.Damore@Sun.COM /* 832*10654SGarrett.Damore@Sun.COM * Given a dev_t, return the instance number (PPA) associated with it. 833*10654SGarrett.Damore@Sun.COM * Drivers can use this in their getinfo(9e) implementation to lookup 834*10654SGarrett.Damore@Sun.COM * the instance number (i.e. PPA) of the device, to use as an index to 835*10654SGarrett.Damore@Sun.COM * their own array of soft state structures. 836*10654SGarrett.Damore@Sun.COM * 837*10654SGarrett.Damore@Sun.COM * Returns -1 on error. 838*10654SGarrett.Damore@Sun.COM */ 839*10654SGarrett.Damore@Sun.COM int 840*10654SGarrett.Damore@Sun.COM mac_devt_to_instance(dev_t devt) 841*10654SGarrett.Damore@Sun.COM { 842*10654SGarrett.Damore@Sun.COM return (dld_devt_to_instance(devt)); 843*10654SGarrett.Damore@Sun.COM } 844*10654SGarrett.Damore@Sun.COM 845*10654SGarrett.Damore@Sun.COM /* 846*10654SGarrett.Damore@Sun.COM * This function returns the first minor number that is available for 847*10654SGarrett.Damore@Sun.COM * driver private use. All minor numbers smaller than this are 848*10654SGarrett.Damore@Sun.COM * reserved for GLDv3 use. 849*10654SGarrett.Damore@Sun.COM */ 850*10654SGarrett.Damore@Sun.COM minor_t 851*10654SGarrett.Damore@Sun.COM mac_private_minor(void) 852*10654SGarrett.Damore@Sun.COM { 853*10654SGarrett.Damore@Sun.COM return (MAC_PRIVATE_MINOR); 854*10654SGarrett.Damore@Sun.COM } 855*10654SGarrett.Damore@Sun.COM 8568275SEric Cheng /* OTHER CONTROL INFORMATION */ 8578275SEric Cheng 8588275SEric Cheng /* 8598275SEric Cheng * A driver notified us that its primary MAC address has changed. 8608275SEric Cheng */ 8618275SEric Cheng void 8628275SEric Cheng mac_unicst_update(mac_handle_t mh, const uint8_t *addr) 8638275SEric Cheng { 8648275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 8658275SEric Cheng 8668275SEric Cheng if (mip->mi_type->mt_addr_length == 0) 8678275SEric Cheng return; 8688275SEric Cheng 8698275SEric Cheng i_mac_perim_enter(mip); 8708275SEric Cheng /* 8718275SEric Cheng * If address doesn't change, do nothing. 8728275SEric Cheng */ 8738275SEric Cheng if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) { 8748275SEric Cheng i_mac_perim_exit(mip); 8758275SEric Cheng return; 8768275SEric Cheng } 8778275SEric Cheng 8788275SEric Cheng /* 8798275SEric Cheng * Freshen the MAC address value and update all MAC clients that 8808275SEric Cheng * share this MAC address. 8818275SEric Cheng */ 8828275SEric Cheng mac_freshen_macaddr(mac_find_macaddr(mip, mip->mi_addr), 8838275SEric Cheng (uint8_t *)addr); 8848275SEric Cheng 8858275SEric Cheng i_mac_perim_exit(mip); 8868275SEric Cheng 8878275SEric Cheng /* 8888275SEric Cheng * Send a MAC_NOTE_UNICST notification. 8898275SEric Cheng */ 8908275SEric Cheng i_mac_notify(mip, MAC_NOTE_UNICST); 8918275SEric Cheng } 8928275SEric Cheng 89310616SSebastien.Roy@Sun.COM void 89410616SSebastien.Roy@Sun.COM mac_dst_update(mac_handle_t mh, const uint8_t *addr) 89510616SSebastien.Roy@Sun.COM { 89610616SSebastien.Roy@Sun.COM mac_impl_t *mip = (mac_impl_t *)mh; 89710616SSebastien.Roy@Sun.COM 89810616SSebastien.Roy@Sun.COM if (mip->mi_type->mt_addr_length == 0) 89910616SSebastien.Roy@Sun.COM return; 90010616SSebastien.Roy@Sun.COM 90110616SSebastien.Roy@Sun.COM i_mac_perim_enter(mip); 90210616SSebastien.Roy@Sun.COM bcopy(addr, mip->mi_dstaddr, mip->mi_type->mt_addr_length); 90310616SSebastien.Roy@Sun.COM i_mac_perim_exit(mip); 90410616SSebastien.Roy@Sun.COM i_mac_notify(mip, MAC_NOTE_DEST); 90510616SSebastien.Roy@Sun.COM } 90610616SSebastien.Roy@Sun.COM 9078275SEric Cheng /* 9088275SEric Cheng * MAC plugin information changed. 9098275SEric Cheng */ 9108275SEric Cheng int 9118275SEric Cheng mac_pdata_update(mac_handle_t mh, void *mac_pdata, size_t dsize) 9128275SEric Cheng { 9138275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 9148275SEric Cheng 9158275SEric Cheng /* 9168275SEric Cheng * Verify that the plugin supports MAC plugin data and that the 9178275SEric Cheng * supplied data is valid. 9188275SEric Cheng */ 9198275SEric Cheng if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY)) 9208275SEric Cheng return (EINVAL); 9218275SEric Cheng if (!mip->mi_type->mt_ops.mtops_pdata_verify(mac_pdata, dsize)) 9228275SEric Cheng return (EINVAL); 9238275SEric Cheng 9248275SEric Cheng if (mip->mi_pdata != NULL) 9258275SEric Cheng kmem_free(mip->mi_pdata, mip->mi_pdata_size); 9268275SEric Cheng 9278275SEric Cheng mip->mi_pdata = kmem_alloc(dsize, KM_SLEEP); 9288275SEric Cheng bcopy(mac_pdata, mip->mi_pdata, dsize); 9298275SEric Cheng mip->mi_pdata_size = dsize; 9308275SEric Cheng 9318275SEric Cheng /* 9328275SEric Cheng * Since the MAC plugin data is used to construct MAC headers that 9338275SEric Cheng * were cached in fast-path headers, we need to flush fast-path 9348275SEric Cheng * information for links associated with this mac. 9358275SEric Cheng */ 9368275SEric Cheng i_mac_notify(mip, MAC_NOTE_FASTPATH_FLUSH); 9378275SEric Cheng return (0); 9388275SEric Cheng } 9398275SEric Cheng 9408275SEric Cheng /* 9418275SEric Cheng * Invoked by driver as well as the framework to notify its capability change. 9428275SEric Cheng */ 9438275SEric Cheng void 9448275SEric Cheng mac_capab_update(mac_handle_t mh) 9458275SEric Cheng { 9468275SEric Cheng /* Send MAC_NOTE_CAPAB_CHG notification */ 9478275SEric Cheng i_mac_notify((mac_impl_t *)mh, MAC_NOTE_CAPAB_CHG); 9488275SEric Cheng } 9498275SEric Cheng 9508275SEric Cheng int 9518275SEric Cheng mac_maxsdu_update(mac_handle_t mh, uint_t sdu_max) 9528275SEric Cheng { 9538275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 9548275SEric Cheng 9559514SGirish.Moodalbail@Sun.COM if (sdu_max == 0 || sdu_max < mip->mi_sdu_min) 9568275SEric Cheng return (EINVAL); 9578275SEric Cheng mip->mi_sdu_max = sdu_max; 9588275SEric Cheng 9598275SEric Cheng /* Send a MAC_NOTE_SDU_SIZE notification. */ 9608275SEric Cheng i_mac_notify(mip, MAC_NOTE_SDU_SIZE); 9618275SEric Cheng return (0); 9628275SEric Cheng } 9638275SEric Cheng 9648275SEric Cheng /* PRIVATE FUNCTIONS, FOR INTERNAL USE ONLY */ 9658275SEric Cheng 9668275SEric Cheng /* 9678275SEric Cheng * Updates the mac_impl structure with the current state of the link 9688275SEric Cheng */ 9698275SEric Cheng static void 9708275SEric Cheng i_mac_log_link_state(mac_impl_t *mip) 9718275SEric Cheng { 9728275SEric Cheng /* 9738275SEric Cheng * If no change, then it is not interesting. 9748275SEric Cheng */ 97510491SRishi.Srivatsavai@Sun.COM if (mip->mi_lastlowlinkstate == mip->mi_lowlinkstate) 9768275SEric Cheng return; 9778275SEric Cheng 97810491SRishi.Srivatsavai@Sun.COM switch (mip->mi_lowlinkstate) { 9798275SEric Cheng case LINK_STATE_UP: 9808275SEric Cheng if (mip->mi_type->mt_ops.mtops_ops & MTOPS_LINK_DETAILS) { 9818275SEric Cheng char det[200]; 9828275SEric Cheng 9838275SEric Cheng mip->mi_type->mt_ops.mtops_link_details(det, 9848275SEric Cheng sizeof (det), (mac_handle_t)mip, mip->mi_pdata); 9858275SEric Cheng 9868275SEric Cheng cmn_err(CE_NOTE, "!%s link up, %s", mip->mi_name, det); 9878275SEric Cheng } else { 9888275SEric Cheng cmn_err(CE_NOTE, "!%s link up", mip->mi_name); 9898275SEric Cheng } 9908275SEric Cheng break; 9918275SEric Cheng 9928275SEric Cheng case LINK_STATE_DOWN: 9938275SEric Cheng /* 9948275SEric Cheng * Only transitions from UP to DOWN are interesting 9958275SEric Cheng */ 99610491SRishi.Srivatsavai@Sun.COM if (mip->mi_lastlowlinkstate != LINK_STATE_UNKNOWN) 9978275SEric Cheng cmn_err(CE_NOTE, "!%s link down", mip->mi_name); 9988275SEric Cheng break; 9998275SEric Cheng 10008275SEric Cheng case LINK_STATE_UNKNOWN: 10018275SEric Cheng /* 10028275SEric Cheng * This case is normally not interesting. 10038275SEric Cheng */ 10048275SEric Cheng break; 10058275SEric Cheng } 100610491SRishi.Srivatsavai@Sun.COM mip->mi_lastlowlinkstate = mip->mi_lowlinkstate; 10078275SEric Cheng } 10088275SEric Cheng 10098275SEric Cheng /* 10108275SEric Cheng * Main routine for the callbacks notifications thread 10118275SEric Cheng */ 10128275SEric Cheng static void 10138275SEric Cheng i_mac_notify_thread(void *arg) 10148275SEric Cheng { 10158275SEric Cheng mac_impl_t *mip = arg; 10168275SEric Cheng callb_cpr_t cprinfo; 10178275SEric Cheng mac_cb_t *mcb; 10188275SEric Cheng mac_cb_info_t *mcbi; 10198275SEric Cheng mac_notify_cb_t *mncb; 10208275SEric Cheng 10218275SEric Cheng mcbi = &mip->mi_notify_cb_info; 10228275SEric Cheng CALLB_CPR_INIT(&cprinfo, mcbi->mcbi_lockp, callb_generic_cpr, 10238275SEric Cheng "i_mac_notify_thread"); 10248275SEric Cheng 10258275SEric Cheng mutex_enter(mcbi->mcbi_lockp); 10268275SEric Cheng 10278275SEric Cheng for (;;) { 10288275SEric Cheng uint32_t bits; 10298275SEric Cheng uint32_t type; 10308275SEric Cheng 10318275SEric Cheng bits = mip->mi_notify_bits; 10328275SEric Cheng if (bits == 0) { 10338275SEric Cheng CALLB_CPR_SAFE_BEGIN(&cprinfo); 10348275SEric Cheng cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 10358275SEric Cheng CALLB_CPR_SAFE_END(&cprinfo, mcbi->mcbi_lockp); 10368275SEric Cheng continue; 10378275SEric Cheng } 10388275SEric Cheng mip->mi_notify_bits = 0; 10398275SEric Cheng if ((bits & (1 << MAC_NNOTE)) != 0) { 10408275SEric Cheng /* request to quit */ 10418275SEric Cheng ASSERT(mip->mi_state_flags & MIS_DISABLED); 10428275SEric Cheng break; 10438275SEric Cheng } 10448275SEric Cheng 10458275SEric Cheng mutex_exit(mcbi->mcbi_lockp); 10468275SEric Cheng 10478275SEric Cheng /* 104810491SRishi.Srivatsavai@Sun.COM * Log link changes on the actual link, but then do reports on 104910491SRishi.Srivatsavai@Sun.COM * synthetic state (if part of a bridge). 10508275SEric Cheng */ 105110491SRishi.Srivatsavai@Sun.COM if ((bits & (1 << MAC_NOTE_LOWLINK)) != 0) { 105210491SRishi.Srivatsavai@Sun.COM link_state_t newstate; 105310491SRishi.Srivatsavai@Sun.COM mac_handle_t mh; 105410491SRishi.Srivatsavai@Sun.COM 10558275SEric Cheng i_mac_log_link_state(mip); 105610491SRishi.Srivatsavai@Sun.COM newstate = mip->mi_lowlinkstate; 105710491SRishi.Srivatsavai@Sun.COM if (mip->mi_bridge_link != NULL) { 105810491SRishi.Srivatsavai@Sun.COM mutex_enter(&mip->mi_bridge_lock); 105910491SRishi.Srivatsavai@Sun.COM if ((mh = mip->mi_bridge_link) != NULL) { 106010491SRishi.Srivatsavai@Sun.COM newstate = mac_bridge_ls_cb(mh, 106110491SRishi.Srivatsavai@Sun.COM newstate); 106210491SRishi.Srivatsavai@Sun.COM } 106310491SRishi.Srivatsavai@Sun.COM mutex_exit(&mip->mi_bridge_lock); 106410491SRishi.Srivatsavai@Sun.COM } 106510491SRishi.Srivatsavai@Sun.COM if (newstate != mip->mi_linkstate) { 106610491SRishi.Srivatsavai@Sun.COM mip->mi_linkstate = newstate; 106710491SRishi.Srivatsavai@Sun.COM bits |= 1 << MAC_NOTE_LINK; 106810491SRishi.Srivatsavai@Sun.COM } 106910491SRishi.Srivatsavai@Sun.COM } 10708275SEric Cheng 10718275SEric Cheng /* 10728275SEric Cheng * Do notification callbacks for each notification type. 10738275SEric Cheng */ 10748275SEric Cheng for (type = 0; type < MAC_NNOTE; type++) { 10758275SEric Cheng if ((bits & (1 << type)) == 0) { 10768275SEric Cheng continue; 10778275SEric Cheng } 10788275SEric Cheng 107910491SRishi.Srivatsavai@Sun.COM if (mac_notify_cb_list[type] != NULL) 108010491SRishi.Srivatsavai@Sun.COM (*mac_notify_cb_list[type])(mip); 10818275SEric Cheng 10828275SEric Cheng /* 10838275SEric Cheng * Walk the list of notifications. 10848275SEric Cheng */ 10858275SEric Cheng MAC_CALLBACK_WALKER_INC(&mip->mi_notify_cb_info); 10868275SEric Cheng for (mcb = mip->mi_notify_cb_list; mcb != NULL; 10878275SEric Cheng mcb = mcb->mcb_nextp) { 10888275SEric Cheng mncb = (mac_notify_cb_t *)mcb->mcb_objp; 10898275SEric Cheng mncb->mncb_fn(mncb->mncb_arg, type); 10908275SEric Cheng } 10918275SEric Cheng MAC_CALLBACK_WALKER_DCR(&mip->mi_notify_cb_info, 10928275SEric Cheng &mip->mi_notify_cb_list); 10938275SEric Cheng } 10948275SEric Cheng 10958275SEric Cheng mutex_enter(mcbi->mcbi_lockp); 10968275SEric Cheng } 10978275SEric Cheng 10988275SEric Cheng mip->mi_state_flags |= MIS_NOTIFY_DONE; 10998275SEric Cheng cv_broadcast(&mcbi->mcbi_cv); 11008275SEric Cheng 11018275SEric Cheng /* CALLB_CPR_EXIT drops the lock */ 11028275SEric Cheng CALLB_CPR_EXIT(&cprinfo); 11038275SEric Cheng thread_exit(); 11048275SEric Cheng } 11058275SEric Cheng 11068275SEric Cheng /* 11078275SEric Cheng * Signal the i_mac_notify_thread asking it to quit. 11088275SEric Cheng * Then wait till it is done. 11098275SEric Cheng */ 11108275SEric Cheng void 11118275SEric Cheng i_mac_notify_exit(mac_impl_t *mip) 11128275SEric Cheng { 11138275SEric Cheng mac_cb_info_t *mcbi; 11148275SEric Cheng 11158275SEric Cheng mcbi = &mip->mi_notify_cb_info; 11168275SEric Cheng 11178275SEric Cheng mutex_enter(mcbi->mcbi_lockp); 11188275SEric Cheng mip->mi_notify_bits = (1 << MAC_NNOTE); 11198275SEric Cheng cv_broadcast(&mcbi->mcbi_cv); 11208275SEric Cheng 11218275SEric Cheng 11228275SEric Cheng while ((mip->mi_notify_thread != NULL) && 11238275SEric Cheng !(mip->mi_state_flags & MIS_NOTIFY_DONE)) { 11248275SEric Cheng cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 11258275SEric Cheng } 11268275SEric Cheng 11278275SEric Cheng /* Necessary clean up before doing kmem_cache_free */ 11288275SEric Cheng mip->mi_state_flags &= ~MIS_NOTIFY_DONE; 11298275SEric Cheng mip->mi_notify_bits = 0; 11308275SEric Cheng mip->mi_notify_thread = NULL; 11318275SEric Cheng mutex_exit(mcbi->mcbi_lockp); 11328275SEric Cheng } 11338275SEric Cheng 11348275SEric Cheng /* 11358275SEric Cheng * Entry point invoked by drivers to dynamically add a ring to an 11368275SEric Cheng * existing group. 11378275SEric Cheng */ 11388275SEric Cheng int 11398275SEric Cheng mac_group_add_ring(mac_group_handle_t gh, int index) 11408275SEric Cheng { 11418275SEric Cheng mac_group_t *group = (mac_group_t *)gh; 11428275SEric Cheng mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 11438275SEric Cheng int ret; 11448275SEric Cheng 11458275SEric Cheng i_mac_perim_enter(mip); 11468275SEric Cheng 11478275SEric Cheng /* 11488275SEric Cheng * Only RX rings can be added or removed by drivers currently. 11498275SEric Cheng */ 11508275SEric Cheng ASSERT(group->mrg_type == MAC_RING_TYPE_RX); 11518275SEric Cheng 11528275SEric Cheng ret = i_mac_group_add_ring(group, NULL, index); 11538275SEric Cheng 11548275SEric Cheng i_mac_perim_exit(mip); 11558275SEric Cheng 11568275SEric Cheng return (ret); 11578275SEric Cheng } 11588275SEric Cheng 11598275SEric Cheng /* 11608275SEric Cheng * Entry point invoked by drivers to dynamically remove a ring 11618275SEric Cheng * from an existing group. The specified ring handle must no longer 11628275SEric Cheng * be used by the driver after a call to this function. 11638275SEric Cheng */ 11648275SEric Cheng void 11658275SEric Cheng mac_group_rem_ring(mac_group_handle_t gh, mac_ring_handle_t rh) 11668275SEric Cheng { 11678275SEric Cheng mac_group_t *group = (mac_group_t *)gh; 11688275SEric Cheng mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 11698275SEric Cheng 11708275SEric Cheng i_mac_perim_enter(mip); 11718275SEric Cheng 11728275SEric Cheng /* 11738275SEric Cheng * Only RX rings can be added or removed by drivers currently. 11748275SEric Cheng */ 11758275SEric Cheng ASSERT(group->mrg_type == MAC_RING_TYPE_RX); 11768275SEric Cheng 11778275SEric Cheng i_mac_group_rem_ring(group, (mac_ring_t *)rh, B_TRUE); 11788275SEric Cheng 11798275SEric Cheng i_mac_perim_exit(mip); 11808275SEric Cheng } 1181