xref: /onnv-gate/usr/src/uts/common/io/mac/mac_datapath_setup.c (revision 12564:c898fb4dec77)
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 /*
2212062SRajagopal.Kunhappan@Sun.COM  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
238275SEric Cheng  */
248275SEric Cheng 
258275SEric Cheng #include <sys/types.h>
268275SEric Cheng #include <sys/callb.h>
2711878SVenu.Iyer@Sun.COM #include <sys/cpupart.h>
2811878SVenu.Iyer@Sun.COM #include <sys/pool.h>
2911878SVenu.Iyer@Sun.COM #include <sys/pool_pset.h>
308275SEric Cheng #include <sys/sdt.h>
318275SEric Cheng #include <sys/strsubr.h>
328275SEric Cheng #include <sys/strsun.h>
338275SEric Cheng #include <sys/vlan.h>
348275SEric Cheng #include <inet/ipsec_impl.h>
358275SEric Cheng #include <inet/ip_impl.h>
368275SEric Cheng #include <inet/sadb.h>
378275SEric Cheng #include <inet/ipsecesp.h>
388275SEric Cheng #include <inet/ipsecah.h>
398275SEric Cheng 
408275SEric Cheng #include <sys/mac_impl.h>
418275SEric Cheng #include <sys/mac_client_impl.h>
428275SEric Cheng #include <sys/mac_client_priv.h>
438275SEric Cheng #include <sys/mac_soft_ring.h>
448275SEric Cheng #include <sys/mac_flow_impl.h>
4511878SVenu.Iyer@Sun.COM #include <sys/mac_stat.h>
468275SEric Cheng 
478275SEric Cheng static void mac_srs_soft_rings_signal(mac_soft_ring_set_t *, uint_t);
488275SEric Cheng static void mac_srs_update_fanout_list(mac_soft_ring_set_t *);
498275SEric Cheng static void mac_srs_poll_unbind(mac_soft_ring_set_t *);
508275SEric Cheng static void mac_srs_worker_unbind(mac_soft_ring_set_t *);
518275SEric Cheng static void mac_srs_soft_rings_quiesce(mac_soft_ring_set_t *, uint_t);
528275SEric Cheng 
538275SEric Cheng static int mac_srs_cpu_setup(cpu_setup_t, int, void *);
548275SEric Cheng static void mac_srs_worker_bind(mac_soft_ring_set_t *, processorid_t);
558275SEric Cheng static void mac_srs_poll_bind(mac_soft_ring_set_t *, processorid_t);
568275SEric Cheng static void mac_srs_threads_unbind(mac_soft_ring_set_t *);
578275SEric Cheng static void mac_srs_add_glist(mac_soft_ring_set_t *);
588275SEric Cheng static void mac_srs_remove_glist(mac_soft_ring_set_t *);
598275SEric Cheng static void mac_srs_fanout_list_free(mac_soft_ring_set_t *);
608275SEric Cheng static void mac_soft_ring_remove(mac_soft_ring_set_t *, mac_soft_ring_t *);
618275SEric Cheng 
6211878SVenu.Iyer@Sun.COM static int mac_compute_soft_ring_count(flow_entry_t *, int, int);
638275SEric Cheng static void mac_walk_srs_and_bind(int);
648275SEric Cheng static void mac_walk_srs_and_unbind(int);
658275SEric Cheng 
668275SEric Cheng extern boolean_t mac_latency_optimize;
678275SEric Cheng 
688275SEric Cheng static kmem_cache_t *mac_srs_cache;
698275SEric Cheng kmem_cache_t *mac_soft_ring_cache;
708275SEric Cheng 
718275SEric Cheng /*
728275SEric Cheng  * The duration in msec we wait before signalling the soft ring
738275SEric Cheng  * worker thread in case packets get queued.
748275SEric Cheng  */
758833SVenu.Iyer@Sun.COM uint32_t mac_soft_ring_worker_wait = 0;
768833SVenu.Iyer@Sun.COM 
778833SVenu.Iyer@Sun.COM /*
788833SVenu.Iyer@Sun.COM  * A global tunable for turning polling on/off. By default, dynamic
798833SVenu.Iyer@Sun.COM  * polling is always on and is always very beneficial. It should be
808833SVenu.Iyer@Sun.COM  * turned off with absolute care and for the rare workload (very
818833SVenu.Iyer@Sun.COM  * low latency sensitive traffic).
828833SVenu.Iyer@Sun.COM  */
838833SVenu.Iyer@Sun.COM int mac_poll_enable = B_TRUE;
848275SEric Cheng 
858275SEric Cheng /*
868275SEric Cheng  * Need to set mac_soft_ring_max_q_cnt based on bandwidth and perhaps latency.
878275SEric Cheng  * Large values could end up in consuming lot of system memory and cause
888275SEric Cheng  * system hang.
898275SEric Cheng  */
908833SVenu.Iyer@Sun.COM int mac_soft_ring_max_q_cnt = 1024;
918833SVenu.Iyer@Sun.COM int mac_soft_ring_min_q_cnt = 256;
928833SVenu.Iyer@Sun.COM int mac_soft_ring_poll_thres = 16;
938275SEric Cheng 
948833SVenu.Iyer@Sun.COM boolean_t mac_tx_serialize = B_FALSE;
958275SEric Cheng 
968275SEric Cheng /*
978275SEric Cheng  * mac_tx_srs_hiwat is the queue depth threshold at which callers of
988275SEric Cheng  * mac_tx() will be notified of flow control condition.
998275SEric Cheng  *
1008275SEric Cheng  * TCP does not honour flow control condition sent up by mac_tx().
1018275SEric Cheng  * Thus provision is made for TCP to allow more packets to be queued
1028275SEric Cheng  * in SRS upto a maximum of mac_tx_srs_max_q_cnt.
1038275SEric Cheng  *
1048275SEric Cheng  * Note that mac_tx_srs_hiwat is always be lesser than
1058275SEric Cheng  * mac_tx_srs_max_q_cnt.
1068275SEric Cheng  */
1078833SVenu.Iyer@Sun.COM uint32_t mac_tx_srs_max_q_cnt = 100000;
1088833SVenu.Iyer@Sun.COM uint32_t mac_tx_srs_hiwat = 1000;
1098275SEric Cheng 
1108275SEric Cheng /*
1118275SEric Cheng  * mac_rx_soft_ring_count, mac_soft_ring_10gig_count:
1128275SEric Cheng  *
1138275SEric Cheng  * Global tunables that determines the number of soft rings to be used for
1148275SEric Cheng  * fanning out incoming traffic on a link. These count will be used only
1158275SEric Cheng  * when no explicit set of CPUs was assigned to the data-links.
1168275SEric Cheng  *
1178275SEric Cheng  * mac_rx_soft_ring_count tunable will come into effect only if
1188275SEric Cheng  * mac_soft_ring_enable is set. mac_soft_ring_enable is turned on by
1198275SEric Cheng  * default only for sun4v platforms.
1208275SEric Cheng  *
1218275SEric Cheng  * mac_rx_soft_ring_10gig_count will come into effect if you are running on a
1228275SEric Cheng  * 10Gbps link and is not dependent upon mac_soft_ring_enable.
1238275SEric Cheng  *
1248275SEric Cheng  * The number of soft rings for fanout for a link or a flow is determined
1258275SEric Cheng  * by mac_compute_soft_ring_count() routine. This routine will take into
1268275SEric Cheng  * account mac_soft_ring_enable, mac_rx_soft_ring_count and
1278275SEric Cheng  * mac_rx_soft_ring_10gig_count to determine the soft ring count for a link.
1288275SEric Cheng  *
1298275SEric Cheng  * If a bandwidth is specified, the determination of the number of soft
1308275SEric Cheng  * rings is based on specified bandwidth, CPU speed and number of CPUs in
1318275SEric Cheng  * the system.
1328275SEric Cheng  */
1338833SVenu.Iyer@Sun.COM uint_t mac_rx_soft_ring_count = 8;
1348833SVenu.Iyer@Sun.COM uint_t mac_rx_soft_ring_10gig_count = 8;
1358275SEric Cheng 
1368275SEric Cheng /*
1378275SEric Cheng  * Every Tx and Rx mac_soft_ring_set_t (mac_srs) created gets added
1388275SEric Cheng  * to mac_srs_g_list and mac_srs_g_lock protects mac_srs_g_list. The
1398275SEric Cheng  * list is used to walk the list of all MAC threads when a CPU is
1408275SEric Cheng  * coming online or going offline.
1418275SEric Cheng  */
1428275SEric Cheng static mac_soft_ring_set_t *mac_srs_g_list = NULL;
1438275SEric Cheng static krwlock_t mac_srs_g_lock;
1448275SEric Cheng 
1458275SEric Cheng /*
1468275SEric Cheng  * Whether the SRS threads should be bound, or not.
1478275SEric Cheng  */
1488833SVenu.Iyer@Sun.COM boolean_t mac_srs_thread_bind = B_TRUE;
1498275SEric Cheng 
1508275SEric Cheng /*
15111878SVenu.Iyer@Sun.COM  * Whether Rx/Tx interrupts should be re-targeted. Disabled by default.
15211878SVenu.Iyer@Sun.COM  * dladm command would override this.
1538275SEric Cheng  */
15411878SVenu.Iyer@Sun.COM boolean_t mac_tx_intr_retarget = B_FALSE;
15511878SVenu.Iyer@Sun.COM boolean_t mac_rx_intr_retarget = B_FALSE;
1568275SEric Cheng 
1578275SEric Cheng /*
1588275SEric Cheng  * If cpu bindings are specified by user, then Tx SRS and its soft
1598275SEric Cheng  * rings should also be bound to the CPUs specified by user. The
1608275SEric Cheng  * CPUs for Tx bindings are at the end of the cpu list provided by
1618275SEric Cheng  * the user. If enough CPUs are not available (for Tx and Rx
1628275SEric Cheng  * SRSes), then the CPUs are shared by both Tx and Rx SRSes.
1638275SEric Cheng  */
1648275SEric Cheng #define	BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp) {			\
1658275SEric Cheng 	processorid_t cpuid;						\
16611878SVenu.Iyer@Sun.COM 	int i;								\
1678275SEric Cheng 	mac_soft_ring_t *softring;					\
16811878SVenu.Iyer@Sun.COM 	mac_cpus_t *srs_cpu;						\
1698275SEric Cheng 									\
17011878SVenu.Iyer@Sun.COM 	srs_cpu = &mac_tx_srs->srs_cpu;					\
17111878SVenu.Iyer@Sun.COM 	cpuid = srs_cpu->mc_tx_fanout_cpus[0];				\
17211878SVenu.Iyer@Sun.COM 	mac_srs_worker_bind(mac_tx_srs, cpuid);				\
17311878SVenu.Iyer@Sun.COM 	if (MAC_TX_SOFT_RINGS(mac_tx_srs)) {				\
17411878SVenu.Iyer@Sun.COM 		for (i = 0; i < mac_tx_srs->srs_tx_ring_count; i++) {	\
17511878SVenu.Iyer@Sun.COM 			cpuid = srs_cpu->mc_tx_fanout_cpus[i];		\
17611878SVenu.Iyer@Sun.COM 			softring = mac_tx_srs->srs_tx_soft_rings[i];	\
17711878SVenu.Iyer@Sun.COM 			if (cpuid != -1) {				\
17811878SVenu.Iyer@Sun.COM 				(void) mac_soft_ring_bind(softring,	\
17911878SVenu.Iyer@Sun.COM 				    cpuid);				\
18011878SVenu.Iyer@Sun.COM 			}						\
1818275SEric Cheng 		}							\
1828275SEric Cheng 	}								\
1838275SEric Cheng }
1848275SEric Cheng 
18511878SVenu.Iyer@Sun.COM /*
18611878SVenu.Iyer@Sun.COM  * Re-targeting is allowed only for exclusive group or for primary.
18711878SVenu.Iyer@Sun.COM  */
18811878SVenu.Iyer@Sun.COM #define	RETARGETABLE_CLIENT(group, mcip)				\
18911878SVenu.Iyer@Sun.COM 	((((group) != NULL) &&						\
19011878SVenu.Iyer@Sun.COM 	    ((group)->mrg_state == MAC_GROUP_STATE_RESERVED)) ||	\
19111878SVenu.Iyer@Sun.COM 	    mac_is_primary_client(mcip))
19211878SVenu.Iyer@Sun.COM 
19311878SVenu.Iyer@Sun.COM #define	MAC_RING_RETARGETABLE(ring)					\
19411878SVenu.Iyer@Sun.COM 	(((ring) != NULL) &&						\
19511878SVenu.Iyer@Sun.COM 	    ((ring)->mr_info.mri_intr.mi_ddi_handle != NULL) &&		\
19611878SVenu.Iyer@Sun.COM 	    !((ring)->mr_info.mri_intr.mi_ddi_shared))
19711878SVenu.Iyer@Sun.COM 
19811878SVenu.Iyer@Sun.COM 
1998275SEric Cheng /* INIT and FINI ROUTINES */
2008275SEric Cheng 
2018275SEric Cheng void
mac_soft_ring_init(void)2028275SEric Cheng mac_soft_ring_init(void)
2038275SEric Cheng {
2048275SEric Cheng 	mac_soft_ring_cache = kmem_cache_create("mac_soft_ring_cache",
2058275SEric Cheng 	    sizeof (mac_soft_ring_t), 64, NULL, NULL, NULL, NULL, NULL, 0);
2068275SEric Cheng 
2078275SEric Cheng 	mac_srs_cache = kmem_cache_create("mac_srs_cache",
2088275SEric Cheng 	    sizeof (mac_soft_ring_set_t),
2098275SEric Cheng 	    64, NULL, NULL, NULL, NULL, NULL, 0);
2108275SEric Cheng 
2118275SEric Cheng 	rw_init(&mac_srs_g_lock, NULL, RW_DEFAULT, NULL);
2128275SEric Cheng 	mutex_enter(&cpu_lock);
2138275SEric Cheng 	register_cpu_setup_func(mac_srs_cpu_setup, NULL);
2148275SEric Cheng 	mutex_exit(&cpu_lock);
2158275SEric Cheng }
2168275SEric Cheng 
2178275SEric Cheng void
mac_soft_ring_finish(void)2188275SEric Cheng mac_soft_ring_finish(void)
2198275SEric Cheng {
2208275SEric Cheng 	mutex_enter(&cpu_lock);
2218275SEric Cheng 	unregister_cpu_setup_func(mac_srs_cpu_setup, NULL);
2228275SEric Cheng 	mutex_exit(&cpu_lock);
2238275SEric Cheng 	rw_destroy(&mac_srs_g_lock);
2248275SEric Cheng 	kmem_cache_destroy(mac_soft_ring_cache);
2258275SEric Cheng 	kmem_cache_destroy(mac_srs_cache);
2268275SEric Cheng }
2278275SEric Cheng 
2288275SEric Cheng static void
mac_srs_soft_rings_free(mac_soft_ring_set_t * mac_srs)22911878SVenu.Iyer@Sun.COM mac_srs_soft_rings_free(mac_soft_ring_set_t *mac_srs)
2308275SEric Cheng {
2318275SEric Cheng 	mac_soft_ring_t	*softring, *next, *head;
2328275SEric Cheng 
2338275SEric Cheng 	/*
2348275SEric Cheng 	 * Synchronize with mac_walk_srs_bind/unbind which are callbacks from
2358275SEric Cheng 	 * DR. The callbacks from DR are called with cpu_lock held, and hence
2368275SEric Cheng 	 * can't wait to grab the mac perimeter. The soft ring list is hence
2378275SEric Cheng 	 * protected for read access by srs_lock. Changing the soft ring list
2388275SEric Cheng 	 * needs the mac perimeter and the srs_lock.
2398275SEric Cheng 	 */
2408275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
2418275SEric Cheng 
2428275SEric Cheng 	head = mac_srs->srs_soft_ring_head;
2438275SEric Cheng 	mac_srs->srs_soft_ring_head = NULL;
2448275SEric Cheng 	mac_srs->srs_soft_ring_tail = NULL;
2458275SEric Cheng 	mac_srs->srs_soft_ring_count = 0;
2468275SEric Cheng 
2478275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
2488275SEric Cheng 
2498275SEric Cheng 	for (softring = head; softring != NULL; softring = next) {
2508275SEric Cheng 		next = softring->s_ring_next;
25111878SVenu.Iyer@Sun.COM 		mac_soft_ring_free(softring);
2528275SEric Cheng 	}
2538275SEric Cheng }
2548275SEric Cheng 
2558275SEric Cheng static void
mac_srs_add_glist(mac_soft_ring_set_t * mac_srs)2568275SEric Cheng mac_srs_add_glist(mac_soft_ring_set_t *mac_srs)
2578275SEric Cheng {
2588275SEric Cheng 	ASSERT(mac_srs->srs_next == NULL && mac_srs->srs_prev == NULL);
2598275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
2608275SEric Cheng 
2618275SEric Cheng 	rw_enter(&mac_srs_g_lock, RW_WRITER);
2628275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
2638275SEric Cheng 
2648275SEric Cheng 	ASSERT((mac_srs->srs_state & SRS_IN_GLIST) == 0);
2658275SEric Cheng 
2668275SEric Cheng 	if (mac_srs_g_list == NULL) {
2678275SEric Cheng 		mac_srs_g_list = mac_srs;
2688275SEric Cheng 	} else {
2698275SEric Cheng 		mac_srs->srs_next = mac_srs_g_list;
2708275SEric Cheng 		mac_srs_g_list->srs_prev = mac_srs;
2718275SEric Cheng 		mac_srs->srs_prev = NULL;
2728275SEric Cheng 		mac_srs_g_list = mac_srs;
2738275SEric Cheng 	}
2748275SEric Cheng 	mac_srs->srs_state |= SRS_IN_GLIST;
2758275SEric Cheng 
2768275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
2778275SEric Cheng 	rw_exit(&mac_srs_g_lock);
2788275SEric Cheng }
2798275SEric Cheng 
2808275SEric Cheng static void
mac_srs_remove_glist(mac_soft_ring_set_t * mac_srs)2818275SEric Cheng mac_srs_remove_glist(mac_soft_ring_set_t *mac_srs)
2828275SEric Cheng {
2838275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
2848275SEric Cheng 
2858275SEric Cheng 	rw_enter(&mac_srs_g_lock, RW_WRITER);
2868275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
2878275SEric Cheng 
2888275SEric Cheng 	ASSERT((mac_srs->srs_state & SRS_IN_GLIST) != 0);
2898275SEric Cheng 
2908275SEric Cheng 	if (mac_srs == mac_srs_g_list) {
2918275SEric Cheng 		mac_srs_g_list = mac_srs->srs_next;
2928275SEric Cheng 		if (mac_srs_g_list != NULL)
2938275SEric Cheng 			mac_srs_g_list->srs_prev = NULL;
2948275SEric Cheng 	} else {
2958275SEric Cheng 		mac_srs->srs_prev->srs_next = mac_srs->srs_next;
2968275SEric Cheng 		if (mac_srs->srs_next != NULL)
2978275SEric Cheng 			mac_srs->srs_next->srs_prev = mac_srs->srs_prev;
2988275SEric Cheng 	}
2998275SEric Cheng 	mac_srs->srs_state &= ~SRS_IN_GLIST;
3008275SEric Cheng 
3018275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
3028275SEric Cheng 	rw_exit(&mac_srs_g_lock);
3038275SEric Cheng }
3048275SEric Cheng 
3058275SEric Cheng /* POLLING SETUP AND TEAR DOWN ROUTINES */
3068275SEric Cheng 
3078275SEric Cheng /*
3088275SEric Cheng  * mac_srs_client_poll_quiesce and mac_srs_client_poll_restart
3098275SEric Cheng  *
3108275SEric Cheng  * These routines are used to call back into the upper layer
3118275SEric Cheng  * (primarily TCP squeue) to stop polling the soft rings or
3128275SEric Cheng  * restart polling.
3138275SEric Cheng  */
3148275SEric Cheng void
mac_srs_client_poll_quiesce(mac_client_impl_t * mcip,mac_soft_ring_set_t * mac_srs)3158275SEric Cheng mac_srs_client_poll_quiesce(mac_client_impl_t *mcip,
3168275SEric Cheng     mac_soft_ring_set_t *mac_srs)
3178275SEric Cheng {
3188275SEric Cheng 	mac_soft_ring_t	*softring;
3198275SEric Cheng 
3208275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
3218275SEric Cheng 
3228275SEric Cheng 	if (!(mac_srs->srs_type & SRST_CLIENT_POLL_ENABLED)) {
3238275SEric Cheng 		ASSERT(!(mac_srs->srs_type & SRST_DLS_BYPASS));
3248275SEric Cheng 		return;
3258275SEric Cheng 	}
3268275SEric Cheng 
3278275SEric Cheng 	for (softring = mac_srs->srs_soft_ring_head;
3288275SEric Cheng 	    softring != NULL; softring = softring->s_ring_next) {
3298275SEric Cheng 		if ((softring->s_ring_type & ST_RING_TCP) &&
3308275SEric Cheng 		    (softring->s_ring_rx_arg2 != NULL)) {
3318275SEric Cheng 			mcip->mci_resource_quiesce(mcip->mci_resource_arg,
3328275SEric Cheng 			    softring->s_ring_rx_arg2);
3338275SEric Cheng 		}
3348275SEric Cheng 	}
3358275SEric Cheng }
3368275SEric Cheng 
3378275SEric Cheng void
mac_srs_client_poll_restart(mac_client_impl_t * mcip,mac_soft_ring_set_t * mac_srs)3388275SEric Cheng mac_srs_client_poll_restart(mac_client_impl_t *mcip,
3398275SEric Cheng     mac_soft_ring_set_t *mac_srs)
3408275SEric Cheng {
3418275SEric Cheng 	mac_soft_ring_t	*softring;
3428275SEric Cheng 
3438275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
3448275SEric Cheng 
3458275SEric Cheng 	if (!(mac_srs->srs_type & SRST_CLIENT_POLL_ENABLED)) {
3468275SEric Cheng 		ASSERT(!(mac_srs->srs_type & SRST_DLS_BYPASS));
3478275SEric Cheng 		return;
3488275SEric Cheng 	}
3498275SEric Cheng 
3508275SEric Cheng 	for (softring = mac_srs->srs_soft_ring_head;
3518275SEric Cheng 	    softring != NULL; softring = softring->s_ring_next) {
3528275SEric Cheng 		if ((softring->s_ring_type & ST_RING_TCP) &&
3538275SEric Cheng 		    (softring->s_ring_rx_arg2 != NULL)) {
3548275SEric Cheng 			mcip->mci_resource_restart(mcip->mci_resource_arg,
3558275SEric Cheng 			    softring->s_ring_rx_arg2);
3568275SEric Cheng 		}
3578275SEric Cheng 	}
3588275SEric Cheng }
3598275SEric Cheng 
3608275SEric Cheng /*
3618275SEric Cheng  * Register the given SRS and associated soft rings with the consumer and
3628275SEric Cheng  * enable the polling interface used by the consumer.(i.e IP) over this
3638275SEric Cheng  * SRS and associated soft rings.
3648275SEric Cheng  */
3658275SEric Cheng void
mac_srs_client_poll_enable(mac_client_impl_t * mcip,mac_soft_ring_set_t * mac_srs)3668275SEric Cheng mac_srs_client_poll_enable(mac_client_impl_t *mcip,
3678275SEric Cheng     mac_soft_ring_set_t *mac_srs)
3688275SEric Cheng {
3698275SEric Cheng 	mac_rx_fifo_t		mrf;
3708275SEric Cheng 	mac_soft_ring_t		*softring;
3718275SEric Cheng 
3728275SEric Cheng 	ASSERT(mac_srs->srs_mcip == mcip);
3738275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
3748275SEric Cheng 
3758275SEric Cheng 	if (!(mcip->mci_state_flags & MCIS_CLIENT_POLL_CAPABLE))
3768275SEric Cheng 		return;
3778275SEric Cheng 
3788275SEric Cheng 	bzero(&mrf, sizeof (mac_rx_fifo_t));
3798275SEric Cheng 	mrf.mrf_type = MAC_RX_FIFO;
3808275SEric Cheng 
3818275SEric Cheng 	/*
3828275SEric Cheng 	 * A SRS is capable of acting as a soft ring for cases
3838275SEric Cheng 	 * where no fanout is needed. This is the case for userland
3848275SEric Cheng 	 * flows.
3858275SEric Cheng 	 */
3868275SEric Cheng 	if (mac_srs->srs_type & SRST_NO_SOFT_RINGS)
3878275SEric Cheng 		return;
3888275SEric Cheng 
3898275SEric Cheng 	mrf.mrf_receive = (mac_receive_t)mac_soft_ring_poll;
3908275SEric Cheng 	mrf.mrf_intr_enable = (mac_intr_enable_t)mac_soft_ring_intr_enable;
3918275SEric Cheng 	mrf.mrf_intr_disable = (mac_intr_disable_t)mac_soft_ring_intr_disable;
3928275SEric Cheng 	mac_srs->srs_type |= SRST_CLIENT_POLL_ENABLED;
3938275SEric Cheng 
3948275SEric Cheng 	softring = mac_srs->srs_soft_ring_head;
3958275SEric Cheng 	while (softring != NULL) {
3968275SEric Cheng 		if (softring->s_ring_type & (ST_RING_TCP | ST_RING_UDP)) {
3978275SEric Cheng 			/*
3988275SEric Cheng 			 * TCP and UDP support DLS bypass. Squeue polling
3998275SEric Cheng 			 * support implies DLS bypass since the squeue poll
4008275SEric Cheng 			 * path does not have DLS processing.
4018275SEric Cheng 			 */
4028275SEric Cheng 			mac_soft_ring_dls_bypass(softring,
4038275SEric Cheng 			    mcip->mci_direct_rx_fn, mcip->mci_direct_rx_arg);
4048275SEric Cheng 		}
4058275SEric Cheng 		/*
4068275SEric Cheng 		 * Non-TCP protocols don't support squeues. Hence we don't
4078275SEric Cheng 		 * make any ring addition callbacks for non-TCP rings
4088275SEric Cheng 		 */
4098275SEric Cheng 		if (!(softring->s_ring_type & ST_RING_TCP)) {
4108275SEric Cheng 			softring->s_ring_rx_arg2 = NULL;
4118275SEric Cheng 			softring = softring->s_ring_next;
4128275SEric Cheng 			continue;
4138275SEric Cheng 		}
4148275SEric Cheng 		mrf.mrf_rx_arg = softring;
4158275SEric Cheng 		mrf.mrf_intr_handle = (mac_intr_handle_t)softring;
4168275SEric Cheng 		mrf.mrf_cpu_id = softring->s_ring_cpuid;
4178275SEric Cheng 		mrf.mrf_flow_priority = mac_srs->srs_pri;
4188275SEric Cheng 
4198275SEric Cheng 		softring->s_ring_rx_arg2 = mcip->mci_resource_add(
4208275SEric Cheng 		    mcip->mci_resource_arg, (mac_resource_t *)&mrf);
4218275SEric Cheng 
4228275SEric Cheng 		softring = softring->s_ring_next;
4238275SEric Cheng 	}
4248275SEric Cheng }
4258275SEric Cheng 
4268275SEric Cheng /*
4278275SEric Cheng  * Unregister the given SRS and associated soft rings with the consumer and
4288275SEric Cheng  * disable the polling interface used by the consumer.(i.e IP) over this
4298275SEric Cheng  * SRS and associated soft rings.
4308275SEric Cheng  */
4318275SEric Cheng void
mac_srs_client_poll_disable(mac_client_impl_t * mcip,mac_soft_ring_set_t * mac_srs)4328275SEric Cheng mac_srs_client_poll_disable(mac_client_impl_t *mcip,
4338275SEric Cheng     mac_soft_ring_set_t *mac_srs)
4348275SEric Cheng {
4358275SEric Cheng 	mac_soft_ring_t		*softring;
4368275SEric Cheng 
4378275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4388275SEric Cheng 
4398275SEric Cheng 	/*
4408275SEric Cheng 	 * A SRS is capable of acting as a soft ring for cases
4418275SEric Cheng 	 * where no protocol fanout is needed. This is the case
4428275SEric Cheng 	 * for userland flows. Nothing to do here.
4438275SEric Cheng 	 */
4448275SEric Cheng 	if (mac_srs->srs_type & SRST_NO_SOFT_RINGS)
4458275SEric Cheng 		return;
4468275SEric Cheng 
4478275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
4488275SEric Cheng 	if (!(mac_srs->srs_type & SRST_CLIENT_POLL_ENABLED)) {
4498275SEric Cheng 		ASSERT(!(mac_srs->srs_type & SRST_DLS_BYPASS));
4508275SEric Cheng 		mutex_exit(&mac_srs->srs_lock);
4518275SEric Cheng 		return;
4528275SEric Cheng 	}
4538275SEric Cheng 	mac_srs->srs_type &= ~(SRST_CLIENT_POLL_ENABLED | SRST_DLS_BYPASS);
4548275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
4558275SEric Cheng 
4568275SEric Cheng 	/*
4578275SEric Cheng 	 * DLS bypass is now disabled in the case of both TCP and UDP.
4588275SEric Cheng 	 * Reset the soft ring callbacks to the standard 'mac_rx_deliver'
4598275SEric Cheng 	 * callback. In addition, in the case of TCP, invoke IP's callback
4608275SEric Cheng 	 * for ring removal.
4618275SEric Cheng 	 */
4628275SEric Cheng 	for (softring = mac_srs->srs_soft_ring_head;
4638275SEric Cheng 	    softring != NULL; softring = softring->s_ring_next) {
4648275SEric Cheng 		if (!(softring->s_ring_type & (ST_RING_UDP | ST_RING_TCP)))
4658275SEric Cheng 			continue;
4668275SEric Cheng 
4678275SEric Cheng 		if ((softring->s_ring_type & ST_RING_TCP) &&
4688275SEric Cheng 		    softring->s_ring_rx_arg2 != NULL) {
4698275SEric Cheng 			mcip->mci_resource_remove(mcip->mci_resource_arg,
4708275SEric Cheng 			    softring->s_ring_rx_arg2);
4718275SEric Cheng 		}
4728275SEric Cheng 
4738275SEric Cheng 		mutex_enter(&softring->s_ring_lock);
4748275SEric Cheng 		while (softring->s_ring_state & S_RING_PROC) {
4758275SEric Cheng 			softring->s_ring_state |= S_RING_CLIENT_WAIT;
4768275SEric Cheng 			cv_wait(&softring->s_ring_client_cv,
4778275SEric Cheng 			    &softring->s_ring_lock);
4788275SEric Cheng 		}
4798275SEric Cheng 		softring->s_ring_state &= ~S_RING_CLIENT_WAIT;
4808275SEric Cheng 		softring->s_ring_rx_arg2 = NULL;
4818275SEric Cheng 		softring->s_ring_rx_func = mac_rx_deliver;
4828275SEric Cheng 		softring->s_ring_rx_arg1 = mcip;
4838275SEric Cheng 		mutex_exit(&softring->s_ring_lock);
4848275SEric Cheng 	}
4858275SEric Cheng }
4868275SEric Cheng 
4878275SEric Cheng /*
4888275SEric Cheng  * Enable or disable poll capability of the SRS on the underlying Rx ring.
4898275SEric Cheng  *
4908275SEric Cheng  * There is a need to enable or disable the poll capability of an SRS over an
4918275SEric Cheng  * Rx ring depending on the number of mac clients sharing the ring and also
4928275SEric Cheng  * whether user flows are configured on it. However the poll state is actively
4938275SEric Cheng  * manipulated by the SRS worker and poll threads and uncoordinated changes by
4948275SEric Cheng  * yet another thread to the underlying capability can surprise them leading
4958275SEric Cheng  * to assert failures. Instead we quiesce the SRS, make the changes and then
4968275SEric Cheng  * restart the SRS.
4978275SEric Cheng  */
4988275SEric Cheng static void
mac_srs_poll_state_change(mac_soft_ring_set_t * mac_srs,boolean_t turn_off_poll_capab,mac_rx_func_t rx_func)4998275SEric Cheng mac_srs_poll_state_change(mac_soft_ring_set_t *mac_srs,
5008275SEric Cheng     boolean_t turn_off_poll_capab, mac_rx_func_t rx_func)
5018275SEric Cheng {
5028275SEric Cheng 	boolean_t	need_restart = B_FALSE;
5038275SEric Cheng 	mac_srs_rx_t	*srs_rx = &mac_srs->srs_rx;
5048275SEric Cheng 	mac_ring_t	*ring;
5058275SEric Cheng 
5068275SEric Cheng 	if (!SRS_QUIESCED(mac_srs)) {
5078275SEric Cheng 		mac_rx_srs_quiesce(mac_srs, SRS_QUIESCE);
5088275SEric Cheng 		need_restart = B_TRUE;
5098275SEric Cheng 	}
5108275SEric Cheng 
5118275SEric Cheng 	ring = mac_srs->srs_ring;
5128275SEric Cheng 	if ((ring != NULL) &&
5138275SEric Cheng 	    (ring->mr_classify_type == MAC_HW_CLASSIFIER)) {
5148275SEric Cheng 		if (turn_off_poll_capab)
5158275SEric Cheng 			mac_srs->srs_state &= ~SRS_POLLING_CAPAB;
5168833SVenu.Iyer@Sun.COM 		else if (mac_poll_enable)
5178275SEric Cheng 			mac_srs->srs_state |= SRS_POLLING_CAPAB;
5188275SEric Cheng 	}
5198275SEric Cheng 	srs_rx->sr_lower_proc = rx_func;
5208275SEric Cheng 
5218275SEric Cheng 	if (need_restart)
5228275SEric Cheng 		mac_rx_srs_restart(mac_srs);
5238275SEric Cheng }
5248275SEric Cheng 
5258275SEric Cheng /* CPU RECONFIGURATION AND FANOUT COMPUTATION ROUTINES */
5268275SEric Cheng 
5278275SEric Cheng /*
5288275SEric Cheng  * Return the next CPU to be used to bind a MAC kernel thread.
52911878SVenu.Iyer@Sun.COM  * If a cpupart is specified, the cpu chosen must be from that
53011878SVenu.Iyer@Sun.COM  * cpu partition.
5318275SEric Cheng  */
5328275SEric Cheng static processorid_t
mac_next_bind_cpu(cpupart_t * cpupart)53311878SVenu.Iyer@Sun.COM mac_next_bind_cpu(cpupart_t *cpupart)
5348275SEric Cheng {
53511878SVenu.Iyer@Sun.COM 	static cpu_t		*cp = NULL;
53611878SVenu.Iyer@Sun.COM 	cpu_t			*cp_start;
5378275SEric Cheng 
5388275SEric Cheng 	ASSERT(MUTEX_HELD(&cpu_lock));
5398275SEric Cheng 
54011878SVenu.Iyer@Sun.COM 	if (cp == NULL)
54111878SVenu.Iyer@Sun.COM 		cp = cpu_list;
54211878SVenu.Iyer@Sun.COM 
54311878SVenu.Iyer@Sun.COM 	cp = cp->cpu_next_onln;
54411878SVenu.Iyer@Sun.COM 	cp_start = cp;
54511878SVenu.Iyer@Sun.COM 
54611878SVenu.Iyer@Sun.COM 	do {
54711878SVenu.Iyer@Sun.COM 		if ((cpupart == NULL) || (cp->cpu_part == cpupart))
54811878SVenu.Iyer@Sun.COM 			return (cp->cpu_id);
54911878SVenu.Iyer@Sun.COM 
55011878SVenu.Iyer@Sun.COM 	} while ((cp = cp->cpu_next_onln) != cp_start);
55111878SVenu.Iyer@Sun.COM 
55211878SVenu.Iyer@Sun.COM 	return (NULL);
5538275SEric Cheng }
5548275SEric Cheng 
5558275SEric Cheng /* ARGSUSED */
5568275SEric Cheng static int
mac_srs_cpu_setup(cpu_setup_t what,int id,void * arg)5578275SEric Cheng mac_srs_cpu_setup(cpu_setup_t what, int id, void *arg)
5588275SEric Cheng {
5598275SEric Cheng 	ASSERT(MUTEX_HELD(&cpu_lock));
5608275SEric Cheng 	switch (what) {
5618275SEric Cheng 	case CPU_CONFIG:
5628275SEric Cheng 	case CPU_ON:
5638275SEric Cheng 	case CPU_CPUPART_IN:
5648275SEric Cheng 		mac_walk_srs_and_bind(id);
5658275SEric Cheng 		break;
5668275SEric Cheng 
5678275SEric Cheng 	case CPU_UNCONFIG:
5688275SEric Cheng 	case CPU_OFF:
5698275SEric Cheng 	case CPU_CPUPART_OUT:
5708275SEric Cheng 		mac_walk_srs_and_unbind(id);
5718275SEric Cheng 		break;
5728275SEric Cheng 
5738275SEric Cheng 	default:
5748275SEric Cheng 		break;
5758275SEric Cheng 	}
5768275SEric Cheng 	return (0);
5778275SEric Cheng }
5788275SEric Cheng 
5798275SEric Cheng /*
5808275SEric Cheng  * mac_compute_soft_ring_count():
5818275SEric Cheng  *
5828275SEric Cheng  * This routine computes the number of soft rings needed to handle incoming
5838275SEric Cheng  * load given a flow_entry.
5848275SEric Cheng  *
5858275SEric Cheng  * The routine does the following:
5868275SEric Cheng  * 1) soft rings will be created if mac_soft_ring_enable is set.
5878275SEric Cheng  * 2) If the underlying link is a 10Gbps link, then soft rings will be
5888275SEric Cheng  * created even if mac_soft_ring_enable is not set. The number of soft
5898275SEric Cheng  * rings, so created,  will equal mac_rx_soft_ring_10gig_count.
5908275SEric Cheng  * 3) On a sun4v platform (i.e., mac_soft_ring_enable is set), 2 times the
5918275SEric Cheng  * mac_rx_soft_ring_10gig_count number of soft rings will be created for a
5928275SEric Cheng  * 10Gbps link.
5938275SEric Cheng  *
5948275SEric Cheng  * If a bandwidth limit is specified, the number that gets computed is
5958275SEric Cheng  * dependent upon CPU speed, the number of Rx rings configured, and
5968275SEric Cheng  * the bandwidth limit.
5978275SEric Cheng  * If more Rx rings are available, less number of soft rings is needed.
5988275SEric Cheng  *
5998275SEric Cheng  * mac_use_bw_heuristic is another "hidden" variable that can be used to
6008275SEric Cheng  * override the default use of soft ring count computation. Depending upon
6018275SEric Cheng  * the usefulness of it, mac_use_bw_heuristic can later be made into a
6028275SEric Cheng  * data-link property or removed altogether.
6038275SEric Cheng  *
6048275SEric Cheng  * TODO: Cleanup and tighten some of the assumptions.
6058275SEric Cheng  */
6068275SEric Cheng boolean_t mac_use_bw_heuristic = B_TRUE;
6078275SEric Cheng static int
mac_compute_soft_ring_count(flow_entry_t * flent,int rx_srs_cnt,int maxcpus)60811878SVenu.Iyer@Sun.COM mac_compute_soft_ring_count(flow_entry_t *flent, int rx_srs_cnt, int maxcpus)
6098275SEric Cheng {
6108275SEric Cheng 	uint64_t cpu_speed, bw = 0;
6118275SEric Cheng 	int srings = 0;
6128275SEric Cheng 	boolean_t bw_enabled = B_FALSE;
6138275SEric Cheng 
6148275SEric Cheng 	ASSERT(!(flent->fe_type & FLOW_USER));
6158275SEric Cheng 	if (flent->fe_resource_props.mrp_mask & MRP_MAXBW &&
6168275SEric Cheng 	    mac_use_bw_heuristic) {
6178275SEric Cheng 		/* bandwidth enabled */
6188275SEric Cheng 		bw_enabled = B_TRUE;
6198275SEric Cheng 		bw = flent->fe_resource_props.mrp_maxbw;
6208275SEric Cheng 	}
6218275SEric Cheng 	if (!bw_enabled) {
6228275SEric Cheng 		/* No bandwidth enabled */
6238275SEric Cheng 		if (mac_soft_ring_enable)
6248275SEric Cheng 			srings = mac_rx_soft_ring_count;
6258275SEric Cheng 
6268275SEric Cheng 		/* Is this a 10Gig link? */
6278275SEric Cheng 		flent->fe_nic_speed = mac_client_stat_get(flent->fe_mcip,
6288275SEric Cheng 		    MAC_STAT_IFSPEED);
6298275SEric Cheng 		/* convert to Mbps */
6308275SEric Cheng 		if (((flent->fe_nic_speed)/1000000) > 1000 &&
6318275SEric Cheng 		    mac_rx_soft_ring_10gig_count > 0) {
6328275SEric Cheng 			/* This is a 10Gig link */
6338275SEric Cheng 			srings = mac_rx_soft_ring_10gig_count;
6348275SEric Cheng 			/*
6358275SEric Cheng 			 * Use 2 times mac_rx_soft_ring_10gig_count for
6368275SEric Cheng 			 * sun4v systems.
6378275SEric Cheng 			 */
6388275SEric Cheng 			if (mac_soft_ring_enable)
6398275SEric Cheng 				srings = srings * 2;
6408275SEric Cheng 		}
6418275SEric Cheng 	} else {
6428275SEric Cheng 		/*
6438275SEric Cheng 		 * Soft ring computation using CPU speed and specified
6448275SEric Cheng 		 * bandwidth limit.
6458275SEric Cheng 		 */
6468275SEric Cheng 		/* Assumption: all CPUs have the same frequency */
6478275SEric Cheng 		cpu_speed = (uint64_t)CPU->cpu_type_info.pi_clock;
6488275SEric Cheng 
6498275SEric Cheng 		/* cpu_speed is in MHz; make bw in units of Mbps.  */
6508275SEric Cheng 		bw = bw/1000000;
6518275SEric Cheng 
6528275SEric Cheng 		if (bw >= 1000) {
6538275SEric Cheng 			/*
6548275SEric Cheng 			 * bw is greater than or equal to 1Gbps.
6558275SEric Cheng 			 * The number of soft rings required is a function
6568275SEric Cheng 			 * of bandwidth and CPU speed. To keep this simple,
6578275SEric Cheng 			 * let's use this rule: 1GHz CPU can handle 1Gbps.
6588275SEric Cheng 			 * If bw is less than 1 Gbps, then there is no need
6598275SEric Cheng 			 * for soft rings. Assumption is that CPU speeds
6608275SEric Cheng 			 * (on modern systems) are at least 1GHz.
6618275SEric Cheng 			 */
6628275SEric Cheng 			srings = bw/cpu_speed;
6638275SEric Cheng 			if (srings <= 1 && mac_soft_ring_enable) {
6648275SEric Cheng 				/*
6658275SEric Cheng 				 * Give at least 2 soft rings
6668275SEric Cheng 				 * for sun4v systems
6678275SEric Cheng 				 */
6688275SEric Cheng 				srings = 2;
6698275SEric Cheng 			}
6708275SEric Cheng 		}
6718275SEric Cheng 	}
6728275SEric Cheng 	/*
6738275SEric Cheng 	 * If the flent has multiple Rx SRSs, then each SRS need not
6748275SEric Cheng 	 * have that many soft rings on top of it. The number of
6758275SEric Cheng 	 * soft rings for each Rx SRS is found by dividing srings by
6768275SEric Cheng 	 * rx_srs_cnt.
6778275SEric Cheng 	 */
6788275SEric Cheng 	if (rx_srs_cnt > 1) {
6798275SEric Cheng 		int remainder;
6808275SEric Cheng 
6818275SEric Cheng 		remainder = srings%rx_srs_cnt;
6828275SEric Cheng 		srings = srings/rx_srs_cnt;
6838275SEric Cheng 		if (remainder != 0)
6848275SEric Cheng 			srings++;
6858275SEric Cheng 		/*
6868275SEric Cheng 		 * Fanning out to 1 soft ring is not very useful.
6878275SEric Cheng 		 * Set it as well to 0 and mac_srs_fanout_init()
6888275SEric Cheng 		 * will take care of creating a single soft ring
6898275SEric Cheng 		 * for proto fanout.
6908275SEric Cheng 		 */
6918275SEric Cheng 		if (srings == 1)
6928275SEric Cheng 			srings = 0;
6938275SEric Cheng 	}
6948275SEric Cheng 	/* Do some more massaging */
69511878SVenu.Iyer@Sun.COM 	srings = min(srings, maxcpus);
6968275SEric Cheng 	srings = min(srings, MAX_SR_FANOUT);
6978275SEric Cheng 	return (srings);
6988275SEric Cheng }
6998275SEric Cheng 
7008275SEric Cheng /*
70111878SVenu.Iyer@Sun.COM  * mac_tx_cpu_init:
70211878SVenu.Iyer@Sun.COM  * set up CPUs for Tx interrupt re-targeting and Tx worker
70311878SVenu.Iyer@Sun.COM  * thread binding
70411878SVenu.Iyer@Sun.COM  */
70511878SVenu.Iyer@Sun.COM static void
mac_tx_cpu_init(flow_entry_t * flent,mac_resource_props_t * mrp,cpupart_t * cpupart)70611878SVenu.Iyer@Sun.COM mac_tx_cpu_init(flow_entry_t *flent, mac_resource_props_t *mrp,
70711878SVenu.Iyer@Sun.COM     cpupart_t *cpupart)
70811878SVenu.Iyer@Sun.COM {
70911878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t *tx_srs = flent->fe_tx_srs;
71011878SVenu.Iyer@Sun.COM 	mac_srs_tx_t *srs_tx = &tx_srs->srs_tx;
71111878SVenu.Iyer@Sun.COM 	mac_cpus_t *srs_cpu = &tx_srs->srs_cpu;
71211878SVenu.Iyer@Sun.COM 	mac_soft_ring_t *sringp;
71311878SVenu.Iyer@Sun.COM 	mac_ring_t *ring;
71411878SVenu.Iyer@Sun.COM 	processorid_t worker_cpuid;
71511878SVenu.Iyer@Sun.COM 	boolean_t retargetable_client = B_FALSE;
71611878SVenu.Iyer@Sun.COM 	int i, j;
71711878SVenu.Iyer@Sun.COM 
71811878SVenu.Iyer@Sun.COM 	if (RETARGETABLE_CLIENT((mac_group_t *)flent->fe_tx_ring_group,
71911878SVenu.Iyer@Sun.COM 	    flent->fe_mcip)) {
72011878SVenu.Iyer@Sun.COM 		retargetable_client = B_TRUE;
72111878SVenu.Iyer@Sun.COM 	}
72211878SVenu.Iyer@Sun.COM 
72311878SVenu.Iyer@Sun.COM 	if (MAC_TX_SOFT_RINGS(tx_srs)) {
72411878SVenu.Iyer@Sun.COM 		if (mrp != NULL)
72511878SVenu.Iyer@Sun.COM 			j = mrp->mrp_ncpus - 1;
72611878SVenu.Iyer@Sun.COM 		for (i = 0; i < tx_srs->srs_tx_ring_count; i++) {
72711878SVenu.Iyer@Sun.COM 			if (mrp != NULL) {
72811878SVenu.Iyer@Sun.COM 				if (j < 0)
72911878SVenu.Iyer@Sun.COM 					j = mrp->mrp_ncpus - 1;
73011878SVenu.Iyer@Sun.COM 				worker_cpuid = mrp->mrp_cpu[j];
73111878SVenu.Iyer@Sun.COM 			} else {
73211878SVenu.Iyer@Sun.COM 				/*
73311878SVenu.Iyer@Sun.COM 				 * Bind interrupt to the next CPU available
73411878SVenu.Iyer@Sun.COM 				 * and leave the worker unbound.
73511878SVenu.Iyer@Sun.COM 				 */
73611878SVenu.Iyer@Sun.COM 				worker_cpuid = -1;
73711878SVenu.Iyer@Sun.COM 			}
73811878SVenu.Iyer@Sun.COM 			sringp = tx_srs->srs_tx_soft_rings[i];
73911878SVenu.Iyer@Sun.COM 			ring = (mac_ring_t *)sringp->s_ring_tx_arg2;
74011878SVenu.Iyer@Sun.COM 			srs_cpu->mc_tx_fanout_cpus[i] = worker_cpuid;
74111878SVenu.Iyer@Sun.COM 			if (MAC_RING_RETARGETABLE(ring) &&
74211878SVenu.Iyer@Sun.COM 			    retargetable_client) {
74311878SVenu.Iyer@Sun.COM 				mutex_enter(&cpu_lock);
74411878SVenu.Iyer@Sun.COM 				srs_cpu->mc_tx_intr_cpu[i] =
74511878SVenu.Iyer@Sun.COM 				    (mrp != NULL) ? mrp->mrp_cpu[j] :
74611878SVenu.Iyer@Sun.COM 				    (mac_tx_intr_retarget ?
74711878SVenu.Iyer@Sun.COM 				    mac_next_bind_cpu(cpupart) : -1);
74811878SVenu.Iyer@Sun.COM 				mutex_exit(&cpu_lock);
74911878SVenu.Iyer@Sun.COM 			} else {
75011878SVenu.Iyer@Sun.COM 				srs_cpu->mc_tx_intr_cpu[i] = -1;
75111878SVenu.Iyer@Sun.COM 			}
75211878SVenu.Iyer@Sun.COM 			if (mrp != NULL)
75311878SVenu.Iyer@Sun.COM 				j--;
75411878SVenu.Iyer@Sun.COM 		}
75511878SVenu.Iyer@Sun.COM 	} else {
75611878SVenu.Iyer@Sun.COM 		/* Tx mac_ring_handle_t is stored in st_arg2 */
75711878SVenu.Iyer@Sun.COM 		srs_cpu->mc_tx_fanout_cpus[0] =
75811878SVenu.Iyer@Sun.COM 		    (mrp != NULL) ? mrp->mrp_cpu[mrp->mrp_ncpus - 1] : -1;
75911878SVenu.Iyer@Sun.COM 		ring = (mac_ring_t *)srs_tx->st_arg2;
76011878SVenu.Iyer@Sun.COM 		if (MAC_RING_RETARGETABLE(ring) && retargetable_client) {
76111878SVenu.Iyer@Sun.COM 			mutex_enter(&cpu_lock);
76211878SVenu.Iyer@Sun.COM 			srs_cpu->mc_tx_intr_cpu[0] = (mrp != NULL) ?
76311878SVenu.Iyer@Sun.COM 			    mrp->mrp_cpu[mrp->mrp_ncpus - 1] :
76411878SVenu.Iyer@Sun.COM 			    (mac_tx_intr_retarget ?
76511878SVenu.Iyer@Sun.COM 			    mac_next_bind_cpu(cpupart) : -1);
76611878SVenu.Iyer@Sun.COM 			mutex_exit(&cpu_lock);
76711878SVenu.Iyer@Sun.COM 		} else {
76811878SVenu.Iyer@Sun.COM 			srs_cpu->mc_tx_intr_cpu[0] = -1;
76911878SVenu.Iyer@Sun.COM 		}
77011878SVenu.Iyer@Sun.COM 	}
77111878SVenu.Iyer@Sun.COM }
77211878SVenu.Iyer@Sun.COM 
77311878SVenu.Iyer@Sun.COM /*
7748275SEric Cheng  * Assignment of user specified CPUs to a link.
7758275SEric Cheng  *
7768275SEric Cheng  * Minimum CPUs required to get an optimal assignmet:
7778275SEric Cheng  * For each Rx SRS, atleast two CPUs are needed if mac_latency_optimize
7788275SEric Cheng  * flag is set -- one for polling, one for fanout soft ring.
7798275SEric Cheng  * If mac_latency_optimize is not set, then 3 CPUs are needed -- one
7808275SEric Cheng  * for polling, one for SRS worker thread and one for fanout soft ring.
7818275SEric Cheng  *
7828275SEric Cheng  * The CPUs needed for Tx side is equal to the number of Tx rings
7838275SEric Cheng  * the link is using.
7848275SEric Cheng  *
7858275SEric Cheng  * mac_flow_user_cpu_init() categorizes the CPU assignment depending
7868275SEric Cheng  * upon the number of CPUs in 3 different buckets.
7878275SEric Cheng  *
7888275SEric Cheng  * In the first bucket, the most optimal case is handled. The user has
7898275SEric Cheng  * passed enough number of CPUs and every thread gets its own CPU.
7908275SEric Cheng  *
7918275SEric Cheng  * The second and third are the sub-optimal cases. Enough CPUs are not
7928275SEric Cheng  * available.
7938275SEric Cheng  *
7948275SEric Cheng  * The second bucket handles the case where atleast one distinct CPU is
7958275SEric Cheng  * is available for each of the Rx rings (Rx SRSes) and Tx rings (Tx
7968275SEric Cheng  * SRS or soft rings).
7978275SEric Cheng  *
7988275SEric Cheng  * In the third case (worst case scenario), specified CPU count is less
7998275SEric Cheng  * than the Rx rings configured for the link. In this case, we round
8008275SEric Cheng  * robin the CPUs among the Rx SRSes and Tx SRS/soft rings.
8018275SEric Cheng  */
8028275SEric Cheng static void
mac_flow_user_cpu_init(flow_entry_t * flent,mac_resource_props_t * mrp)8038275SEric Cheng mac_flow_user_cpu_init(flow_entry_t *flent, mac_resource_props_t *mrp)
8048275SEric Cheng {
8058275SEric Cheng 	mac_soft_ring_set_t *rx_srs, *tx_srs;
8068275SEric Cheng 	int i, srs_cnt;
8078275SEric Cheng 	mac_cpus_t *srs_cpu;
8088275SEric Cheng 	int no_of_cpus, cpu_cnt;
8098275SEric Cheng 	int rx_srs_cnt, reqd_rx_cpu_cnt;
8108275SEric Cheng 	int fanout_cpu_cnt, reqd_tx_cpu_cnt;
8118275SEric Cheng 	int reqd_poll_worker_cnt, fanout_cnt_per_srs;
81211878SVenu.Iyer@Sun.COM 	mac_resource_props_t *emrp = &flent->fe_effective_props;
8138275SEric Cheng 
8148275SEric Cheng 	ASSERT(mrp->mrp_fanout_mode == MCM_CPUS);
8158275SEric Cheng 	/*
8168275SEric Cheng 	 * The check for nbc_ncpus to be within limits for
8178275SEric Cheng 	 * the user specified case was done earlier and if
8188275SEric Cheng 	 * not within limits, an error would have been
8198275SEric Cheng 	 * returned to the user.
8208275SEric Cheng 	 */
82112062SRajagopal.Kunhappan@Sun.COM 	ASSERT(mrp->mrp_ncpus > 0);
8228275SEric Cheng 
8238275SEric Cheng 	no_of_cpus = mrp->mrp_ncpus;
8248275SEric Cheng 
82511878SVenu.Iyer@Sun.COM 	if (mrp->mrp_rx_intr_cpu != -1) {
8268275SEric Cheng 		/*
8278275SEric Cheng 		 * interrupt has been re-targetted. Poll
8288275SEric Cheng 		 * thread needs to be bound to interrupt
82911878SVenu.Iyer@Sun.COM 		 * CPU.
8308275SEric Cheng 		 *
8318275SEric Cheng 		 * Find where in the list is the intr
8328275SEric Cheng 		 * CPU and swap it with the first one.
8338275SEric Cheng 		 * We will be using the first CPU in the
8348275SEric Cheng 		 * list for poll.
8358275SEric Cheng 		 */
8368275SEric Cheng 		for (i = 0; i < no_of_cpus; i++) {
83711878SVenu.Iyer@Sun.COM 			if (mrp->mrp_cpu[i] == mrp->mrp_rx_intr_cpu)
8388275SEric Cheng 				break;
8398275SEric Cheng 		}
8408275SEric Cheng 		mrp->mrp_cpu[i] = mrp->mrp_cpu[0];
84111878SVenu.Iyer@Sun.COM 		mrp->mrp_cpu[0] = mrp->mrp_rx_intr_cpu;
8428275SEric Cheng 	}
8438275SEric Cheng 
8448275SEric Cheng 	/*
8458275SEric Cheng 	 * Requirements:
8468275SEric Cheng 	 * The number of CPUs that each Rx ring needs is dependent
8478275SEric Cheng 	 * upon mac_latency_optimize flag.
8488275SEric Cheng 	 * 1) If set, atleast 2 CPUs are needed -- one for
8498275SEric Cheng 	 * polling, one for fanout soft ring.
8508275SEric Cheng 	 * 2) If not set, then atleast 3 CPUs are needed -- one
8518275SEric Cheng 	 * for polling, one for srs worker thread, and one for
8528275SEric Cheng 	 * fanout soft ring.
8538275SEric Cheng 	 */
8548275SEric Cheng 	rx_srs_cnt = (flent->fe_rx_srs_cnt > 1) ?
8558275SEric Cheng 	    (flent->fe_rx_srs_cnt - 1) : flent->fe_rx_srs_cnt;
8568275SEric Cheng 	reqd_rx_cpu_cnt = mac_latency_optimize ?
8578275SEric Cheng 	    (rx_srs_cnt * 2) : (rx_srs_cnt * 3);
8588275SEric Cheng 
8598275SEric Cheng 	/* How many CPUs are needed for Tx side? */
8608275SEric Cheng 	tx_srs = flent->fe_tx_srs;
86111878SVenu.Iyer@Sun.COM 	reqd_tx_cpu_cnt = MAC_TX_SOFT_RINGS(tx_srs) ?
86211878SVenu.Iyer@Sun.COM 	    tx_srs->srs_tx_ring_count : 1;
8638275SEric Cheng 
8648275SEric Cheng 	/* CPUs needed for Rx SRSes poll and worker threads */
8658275SEric Cheng 	reqd_poll_worker_cnt = mac_latency_optimize ?
8668275SEric Cheng 	    rx_srs_cnt : rx_srs_cnt * 2;
8678275SEric Cheng 
8688275SEric Cheng 	/* Has the user provided enough CPUs? */
8698275SEric Cheng 	if (no_of_cpus >= (reqd_rx_cpu_cnt + reqd_tx_cpu_cnt)) {
8708275SEric Cheng 		/*
8718275SEric Cheng 		 * Best case scenario. There is enough CPUs. All
8728275SEric Cheng 		 * Rx rings will get their own set of CPUs plus
8738275SEric Cheng 		 * Tx soft rings will get their own.
8748275SEric Cheng 		 */
8758275SEric Cheng 		/*
8768275SEric Cheng 		 * fanout_cpu_cnt is the number of CPUs available
8778275SEric Cheng 		 * for Rx side fanout soft rings.
8788275SEric Cheng 		 */
8798275SEric Cheng 		fanout_cpu_cnt = no_of_cpus -
8808275SEric Cheng 		    reqd_poll_worker_cnt - reqd_tx_cpu_cnt;
8818275SEric Cheng 
8828275SEric Cheng 		/*
8838275SEric Cheng 		 * Divide fanout_cpu_cnt by rx_srs_cnt to find
8848275SEric Cheng 		 * out how many fanout soft rings each Rx SRS
8858275SEric Cheng 		 * can have.
8868275SEric Cheng 		 */
8878275SEric Cheng 		fanout_cnt_per_srs = fanout_cpu_cnt/rx_srs_cnt;
8888275SEric Cheng 
88912062SRajagopal.Kunhappan@Sun.COM 		/* fanout_cnt_per_srs should not be >  MAX_SR_FANOUT */
89012062SRajagopal.Kunhappan@Sun.COM 		fanout_cnt_per_srs = min(fanout_cnt_per_srs, MAX_SR_FANOUT);
89112062SRajagopal.Kunhappan@Sun.COM 
8928275SEric Cheng 		/* Do the assignment for the default Rx ring */
8938275SEric Cheng 		cpu_cnt = 0;
8948275SEric Cheng 		rx_srs = flent->fe_rx_srs[0];
8958275SEric Cheng 		ASSERT(rx_srs->srs_ring == NULL);
8968275SEric Cheng 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
8978275SEric Cheng 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
8988275SEric Cheng 		srs_cpu = &rx_srs->srs_cpu;
8998275SEric Cheng 		srs_cpu->mc_ncpus = no_of_cpus;
9008275SEric Cheng 		bcopy(mrp->mrp_cpu,
9018275SEric Cheng 		    srs_cpu->mc_cpus, sizeof (srs_cpu->mc_cpus));
90211878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_fanout_cnt = fanout_cnt_per_srs;
90311878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt++];
90411878SVenu.Iyer@Sun.COM 		/* Retarget the interrupt to the same CPU as the poll */
90511878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
90611878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_workerid = (mac_latency_optimize ?
90711878SVenu.Iyer@Sun.COM 		    srs_cpu->mc_rx_pollid : mrp->mrp_cpu[cpu_cnt++]);
9088275SEric Cheng 		for (i = 0; i < fanout_cnt_per_srs; i++)
90911878SVenu.Iyer@Sun.COM 			srs_cpu->mc_rx_fanout_cpus[i] = mrp->mrp_cpu[cpu_cnt++];
9108275SEric Cheng 
9118275SEric Cheng 		/* Do the assignment for h/w Rx SRSes */
9128275SEric Cheng 		if (flent->fe_rx_srs_cnt > 1) {
9138275SEric Cheng 			cpu_cnt = 0;
9148275SEric Cheng 			for (srs_cnt = 1;
9158275SEric Cheng 			    srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
9168275SEric Cheng 				rx_srs = flent->fe_rx_srs[srs_cnt];
9178275SEric Cheng 				ASSERT(rx_srs->srs_ring != NULL);
9188275SEric Cheng 				if (rx_srs->srs_fanout_state ==
9198275SEric Cheng 				    SRS_FANOUT_INIT) {
9208275SEric Cheng 					rx_srs->srs_fanout_state =
9218275SEric Cheng 					    SRS_FANOUT_REINIT;
9228275SEric Cheng 				}
9238275SEric Cheng 				srs_cpu = &rx_srs->srs_cpu;
9248275SEric Cheng 				srs_cpu->mc_ncpus = no_of_cpus;
9258275SEric Cheng 				bcopy(mrp->mrp_cpu, srs_cpu->mc_cpus,
9268275SEric Cheng 				    sizeof (srs_cpu->mc_cpus));
92711878SVenu.Iyer@Sun.COM 				srs_cpu->mc_rx_fanout_cnt = fanout_cnt_per_srs;
9288275SEric Cheng 				/* The first CPU in the list is the intr CPU */
92911878SVenu.Iyer@Sun.COM 				srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt++];
93011878SVenu.Iyer@Sun.COM 				srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
93111878SVenu.Iyer@Sun.COM 				srs_cpu->mc_rx_workerid =
93211878SVenu.Iyer@Sun.COM 				    (mac_latency_optimize ?
93311878SVenu.Iyer@Sun.COM 				    srs_cpu->mc_rx_pollid :
93411878SVenu.Iyer@Sun.COM 				    mrp->mrp_cpu[cpu_cnt++]);
9358275SEric Cheng 				for (i = 0; i < fanout_cnt_per_srs; i++) {
93611878SVenu.Iyer@Sun.COM 					srs_cpu->mc_rx_fanout_cpus[i] =
9378275SEric Cheng 					    mrp->mrp_cpu[cpu_cnt++];
9388275SEric Cheng 				}
9398275SEric Cheng 				ASSERT(cpu_cnt <= no_of_cpus);
9408275SEric Cheng 			}
9418275SEric Cheng 		}
94211878SVenu.Iyer@Sun.COM 		goto tx_cpu_init;
9438275SEric Cheng 	}
9448275SEric Cheng 
9458275SEric Cheng 	/*
9468275SEric Cheng 	 * Sub-optimal case.
9478275SEric Cheng 	 * We have the following information:
9488275SEric Cheng 	 * no_of_cpus - no. of cpus that user passed.
9498275SEric Cheng 	 * rx_srs_cnt - no. of rx rings.
9508275SEric Cheng 	 * reqd_rx_cpu_cnt = mac_latency_optimize?rx_srs_cnt*2:rx_srs_cnt*3
9518275SEric Cheng 	 * reqd_tx_cpu_cnt - no. of cpus reqd. for Tx side.
9528275SEric Cheng 	 * reqd_poll_worker_cnt = mac_latency_optimize?rx_srs_cnt:rx_srs_cnt*2
9538275SEric Cheng 	 */
9548275SEric Cheng 	/*
9558275SEric Cheng 	 * If we bind the Rx fanout soft rings to the same CPUs
9568275SEric Cheng 	 * as poll/worker, would that be enough?
9578275SEric Cheng 	 */
9588275SEric Cheng 	if (no_of_cpus >= (rx_srs_cnt + reqd_tx_cpu_cnt)) {
9598275SEric Cheng 		boolean_t worker_assign = B_FALSE;
9608275SEric Cheng 
9618275SEric Cheng 		/*
9628275SEric Cheng 		 * If mac_latency_optimize is not set, are there
9638275SEric Cheng 		 * enough CPUs to assign a CPU for worker also?
9648275SEric Cheng 		 */
9658275SEric Cheng 		if (no_of_cpus >= (reqd_poll_worker_cnt + reqd_tx_cpu_cnt))
9668275SEric Cheng 			worker_assign = B_TRUE;
9678275SEric Cheng 		/*
9688275SEric Cheng 		 * Zero'th Rx SRS is the default Rx ring. It is not
9698275SEric Cheng 		 * associated with h/w Rx ring.
9708275SEric Cheng 		 */
9718275SEric Cheng 		rx_srs = flent->fe_rx_srs[0];
9728275SEric Cheng 		ASSERT(rx_srs->srs_ring == NULL);
9738275SEric Cheng 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
9748275SEric Cheng 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
9758275SEric Cheng 		cpu_cnt = 0;
9768275SEric Cheng 		srs_cpu = &rx_srs->srs_cpu;
9778275SEric Cheng 		srs_cpu->mc_ncpus = no_of_cpus;
9788275SEric Cheng 		bcopy(mrp->mrp_cpu,
9798275SEric Cheng 		    srs_cpu->mc_cpus, sizeof (srs_cpu->mc_cpus));
98011878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_fanout_cnt = 1;
98111878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt++];
98211878SVenu.Iyer@Sun.COM 		/* Retarget the interrupt to the same CPU as the poll */
98311878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
98411878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_workerid =
98511878SVenu.Iyer@Sun.COM 		    ((!mac_latency_optimize && worker_assign) ?
98611878SVenu.Iyer@Sun.COM 		    mrp->mrp_cpu[cpu_cnt++] : srs_cpu->mc_rx_pollid);
98711878SVenu.Iyer@Sun.COM 
98811878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_fanout_cpus[0] = mrp->mrp_cpu[cpu_cnt];
9898275SEric Cheng 
9908275SEric Cheng 		/* Do CPU bindings for SRSes having h/w Rx rings */
9918275SEric Cheng 		if (flent->fe_rx_srs_cnt > 1) {
9928275SEric Cheng 			cpu_cnt = 0;
9938275SEric Cheng 			for (srs_cnt = 1;
9948275SEric Cheng 			    srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
9958275SEric Cheng 				rx_srs = flent->fe_rx_srs[srs_cnt];
9968275SEric Cheng 				ASSERT(rx_srs->srs_ring != NULL);
9978275SEric Cheng 				if (rx_srs->srs_fanout_state ==
9988275SEric Cheng 				    SRS_FANOUT_INIT) {
9998275SEric Cheng 					rx_srs->srs_fanout_state =
10008275SEric Cheng 					    SRS_FANOUT_REINIT;
10018275SEric Cheng 				}
10028275SEric Cheng 				srs_cpu = &rx_srs->srs_cpu;
10038275SEric Cheng 				srs_cpu->mc_ncpus = no_of_cpus;
10048275SEric Cheng 				bcopy(mrp->mrp_cpu, srs_cpu->mc_cpus,
10058275SEric Cheng 				    sizeof (srs_cpu->mc_cpus));
100611878SVenu.Iyer@Sun.COM 				srs_cpu->mc_rx_pollid =
10078275SEric Cheng 				    mrp->mrp_cpu[cpu_cnt];
100811878SVenu.Iyer@Sun.COM 				srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
100911878SVenu.Iyer@Sun.COM 				srs_cpu->mc_rx_workerid =
101011878SVenu.Iyer@Sun.COM 				    ((!mac_latency_optimize && worker_assign) ?
101111878SVenu.Iyer@Sun.COM 				    mrp->mrp_cpu[++cpu_cnt] :
101211878SVenu.Iyer@Sun.COM 				    srs_cpu->mc_rx_pollid);
101311878SVenu.Iyer@Sun.COM 				srs_cpu->mc_rx_fanout_cnt = 1;
101411878SVenu.Iyer@Sun.COM 				srs_cpu->mc_rx_fanout_cpus[0] =
10158275SEric Cheng 				    mrp->mrp_cpu[cpu_cnt];
10168275SEric Cheng 				cpu_cnt++;
10178275SEric Cheng 				ASSERT(cpu_cnt <= no_of_cpus);
10188275SEric Cheng 			}
10198275SEric Cheng 		}
102011878SVenu.Iyer@Sun.COM 		goto tx_cpu_init;
10218275SEric Cheng 	}
10228275SEric Cheng 
10238275SEric Cheng 	/*
10248275SEric Cheng 	 * Real sub-optimal case. Not enough CPUs for poll and
10258275SEric Cheng 	 * Tx soft rings. Do a round robin assignment where
10268275SEric Cheng 	 * each Rx SRS will get the same CPU for poll, worker
10278275SEric Cheng 	 * and fanout soft ring.
10288275SEric Cheng 	 */
10298275SEric Cheng 	cpu_cnt = 0;
10308275SEric Cheng 	for (srs_cnt = 0; srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
10318275SEric Cheng 		rx_srs = flent->fe_rx_srs[srs_cnt];
10328275SEric Cheng 		srs_cpu = &rx_srs->srs_cpu;
10338275SEric Cheng 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
10348275SEric Cheng 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
10358275SEric Cheng 		srs_cpu->mc_ncpus = no_of_cpus;
10368275SEric Cheng 		bcopy(mrp->mrp_cpu,
10378275SEric Cheng 		    srs_cpu->mc_cpus, sizeof (srs_cpu->mc_cpus));
103811878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_fanout_cnt = 1;
103911878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt];
104011878SVenu.Iyer@Sun.COM 		/* Retarget the interrupt to the same CPU as the poll */
104111878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
104211878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_workerid = mrp->mrp_cpu[cpu_cnt];
104311878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_fanout_cpus[0] = mrp->mrp_cpu[cpu_cnt];
10448275SEric Cheng 		if (++cpu_cnt >= no_of_cpus)
10458275SEric Cheng 			cpu_cnt = 0;
10468275SEric Cheng 	}
104711878SVenu.Iyer@Sun.COM 
104811878SVenu.Iyer@Sun.COM tx_cpu_init:
104911878SVenu.Iyer@Sun.COM 	mac_tx_cpu_init(flent, mrp, NULL);
105011878SVenu.Iyer@Sun.COM 
105111878SVenu.Iyer@Sun.COM 	/*
105211878SVenu.Iyer@Sun.COM 	 * Copy the user specified CPUs to the effective CPUs
105311878SVenu.Iyer@Sun.COM 	 */
105411878SVenu.Iyer@Sun.COM 	for (i = 0; i < mrp->mrp_ncpus; i++) {
105511878SVenu.Iyer@Sun.COM 		emrp->mrp_cpu[i] = mrp->mrp_cpu[i];
105611878SVenu.Iyer@Sun.COM 	}
105711878SVenu.Iyer@Sun.COM 	emrp->mrp_ncpus = mrp->mrp_ncpus;
105811878SVenu.Iyer@Sun.COM 	emrp->mrp_mask = mrp->mrp_mask;
105911878SVenu.Iyer@Sun.COM 	bzero(emrp->mrp_pool, MAXPATHLEN);
10608275SEric Cheng }
10618275SEric Cheng 
10628275SEric Cheng /*
10638275SEric Cheng  * mac_flow_cpu_init():
10648275SEric Cheng  *
10658275SEric Cheng  * Each SRS has a mac_cpu_t structure, srs_cpu. This routine fills in
10668275SEric Cheng  * the CPU binding information in srs_cpu for all Rx SRSes associated
10678275SEric Cheng  * with a flent.
10688275SEric Cheng  */
10698275SEric Cheng static void
mac_flow_cpu_init(flow_entry_t * flent,cpupart_t * cpupart)107011878SVenu.Iyer@Sun.COM mac_flow_cpu_init(flow_entry_t *flent, cpupart_t *cpupart)
10718275SEric Cheng {
10728275SEric Cheng 	mac_soft_ring_set_t *rx_srs;
10738275SEric Cheng 	processorid_t cpuid;
107411878SVenu.Iyer@Sun.COM 	int i, j, k, srs_cnt, nscpus, maxcpus, soft_ring_cnt = 0;
10758275SEric Cheng 	mac_cpus_t *srs_cpu;
107611878SVenu.Iyer@Sun.COM 	mac_resource_props_t *emrp = &flent->fe_effective_props;
107711878SVenu.Iyer@Sun.COM 	uint32_t cpus[MRP_NCPUS];
107811878SVenu.Iyer@Sun.COM 
107911878SVenu.Iyer@Sun.COM 	/*
108011878SVenu.Iyer@Sun.COM 	 * The maximum number of CPUs available can either be
108111878SVenu.Iyer@Sun.COM 	 * the number of CPUs in the pool or the number of CPUs
108211878SVenu.Iyer@Sun.COM 	 * in the system.
108311878SVenu.Iyer@Sun.COM 	 */
108411878SVenu.Iyer@Sun.COM 	maxcpus = (cpupart != NULL) ? cpupart->cp_ncpus : ncpus;
108511878SVenu.Iyer@Sun.COM 
108611878SVenu.Iyer@Sun.COM 	/*
108711878SVenu.Iyer@Sun.COM 	 * Compute the number of soft rings needed on top for each Rx
108811878SVenu.Iyer@Sun.COM 	 * SRS. "rx_srs_cnt-1" indicates the number of Rx SRS
108911878SVenu.Iyer@Sun.COM 	 * associated with h/w Rx rings. Soft ring count needed for
109011878SVenu.Iyer@Sun.COM 	 * each h/w Rx SRS is computed and the same is applied to
109111878SVenu.Iyer@Sun.COM 	 * software classified Rx SRS. The first Rx SRS in fe_rx_srs[]
109211878SVenu.Iyer@Sun.COM 	 * is the software classified Rx SRS.
109311878SVenu.Iyer@Sun.COM 	 */
109411878SVenu.Iyer@Sun.COM 	soft_ring_cnt = mac_compute_soft_ring_count(flent,
109511878SVenu.Iyer@Sun.COM 	    flent->fe_rx_srs_cnt - 1, maxcpus);
109611878SVenu.Iyer@Sun.COM 	if (soft_ring_cnt == 0) {
10978275SEric Cheng 		/*
109811878SVenu.Iyer@Sun.COM 		 * Even when soft_ring_cnt is 0, we still need
109911878SVenu.Iyer@Sun.COM 		 * to create a soft ring for TCP, UDP and
110011878SVenu.Iyer@Sun.COM 		 * OTHER. So set it to 1.
11018275SEric Cheng 		 */
110211878SVenu.Iyer@Sun.COM 		soft_ring_cnt = 1;
110311878SVenu.Iyer@Sun.COM 	}
110411878SVenu.Iyer@Sun.COM 	for (srs_cnt = 0; srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
110511878SVenu.Iyer@Sun.COM 		rx_srs = flent->fe_rx_srs[srs_cnt];
110611878SVenu.Iyer@Sun.COM 		srs_cpu = &rx_srs->srs_cpu;
110711878SVenu.Iyer@Sun.COM 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
110811878SVenu.Iyer@Sun.COM 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
110911878SVenu.Iyer@Sun.COM 		srs_cpu->mc_ncpus = soft_ring_cnt;
111011878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_fanout_cnt = soft_ring_cnt;
111111878SVenu.Iyer@Sun.COM 		mutex_enter(&cpu_lock);
111211878SVenu.Iyer@Sun.COM 		for (j = 0; j < soft_ring_cnt; j++) {
111311878SVenu.Iyer@Sun.COM 			cpuid = mac_next_bind_cpu(cpupart);
111411878SVenu.Iyer@Sun.COM 			srs_cpu->mc_cpus[j] = cpuid;
111511878SVenu.Iyer@Sun.COM 			srs_cpu->mc_rx_fanout_cpus[j] = cpuid;
11168275SEric Cheng 		}
111711878SVenu.Iyer@Sun.COM 		cpuid = mac_next_bind_cpu(cpupart);
111811878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_pollid = cpuid;
111911878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_intr_cpu = (mac_rx_intr_retarget ?
112011878SVenu.Iyer@Sun.COM 		    srs_cpu->mc_rx_pollid : -1);
112111878SVenu.Iyer@Sun.COM 		/* increment ncpus to account for polling cpu */
112211878SVenu.Iyer@Sun.COM 		srs_cpu->mc_ncpus++;
112311878SVenu.Iyer@Sun.COM 		srs_cpu->mc_cpus[j++] = cpuid;
112411878SVenu.Iyer@Sun.COM 		if (!mac_latency_optimize) {
112511878SVenu.Iyer@Sun.COM 			cpuid = mac_next_bind_cpu(cpupart);
11268275SEric Cheng 			srs_cpu->mc_ncpus++;
11278275SEric Cheng 			srs_cpu->mc_cpus[j++] = cpuid;
112811878SVenu.Iyer@Sun.COM 		}
112911878SVenu.Iyer@Sun.COM 		srs_cpu->mc_rx_workerid = cpuid;
113011878SVenu.Iyer@Sun.COM 		mutex_exit(&cpu_lock);
113111878SVenu.Iyer@Sun.COM 	}
113211878SVenu.Iyer@Sun.COM 
113311878SVenu.Iyer@Sun.COM 	nscpus = 0;
113411878SVenu.Iyer@Sun.COM 	for (srs_cnt = 0; srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
113511878SVenu.Iyer@Sun.COM 		rx_srs = flent->fe_rx_srs[srs_cnt];
113611878SVenu.Iyer@Sun.COM 		srs_cpu = &rx_srs->srs_cpu;
113711878SVenu.Iyer@Sun.COM 		for (j = 0; j < srs_cpu->mc_ncpus; j++) {
113811878SVenu.Iyer@Sun.COM 			cpus[nscpus++] = srs_cpu->mc_cpus[j];
11398275SEric Cheng 		}
11408275SEric Cheng 	}
114111878SVenu.Iyer@Sun.COM 
114211878SVenu.Iyer@Sun.COM 
114311878SVenu.Iyer@Sun.COM 	/*
114411878SVenu.Iyer@Sun.COM 	 * Copy cpu list to fe_effective_props
114511878SVenu.Iyer@Sun.COM 	 * without duplicates.
114611878SVenu.Iyer@Sun.COM 	 */
114711878SVenu.Iyer@Sun.COM 	k = 0;
114811878SVenu.Iyer@Sun.COM 	for (i = 0; i < nscpus; i++) {
114911878SVenu.Iyer@Sun.COM 		for (j = 0; j < k; j++) {
115011878SVenu.Iyer@Sun.COM 			if (emrp->mrp_cpu[j] == cpus[i])
115111878SVenu.Iyer@Sun.COM 				break;
115211878SVenu.Iyer@Sun.COM 		}
115311878SVenu.Iyer@Sun.COM 		if (j == k)
115411878SVenu.Iyer@Sun.COM 			emrp->mrp_cpu[k++] = cpus[i];
115511878SVenu.Iyer@Sun.COM 	}
115611878SVenu.Iyer@Sun.COM 	emrp->mrp_ncpus = k;
115711878SVenu.Iyer@Sun.COM 
115811878SVenu.Iyer@Sun.COM 	mac_tx_cpu_init(flent, NULL, cpupart);
11598275SEric Cheng }
11608275SEric Cheng 
11618275SEric Cheng /*
11628275SEric Cheng  * DATAPATH SETUP ROUTINES
11638275SEric Cheng  * (setup SRS and set/update FANOUT, B/W and PRIORITY)
11648275SEric Cheng  */
11658275SEric Cheng 
116611878SVenu.Iyer@Sun.COM /*
116711878SVenu.Iyer@Sun.COM  * mac_srs_fanout_list_alloc:
116811878SVenu.Iyer@Sun.COM  *
116911878SVenu.Iyer@Sun.COM  * The underlying device can expose upto MAX_RINGS_PER_GROUP worth of
117011878SVenu.Iyer@Sun.COM  * rings to a client. In such a case, MAX_RINGS_PER_GROUP worth of
117111878SVenu.Iyer@Sun.COM  * array space is needed to store Tx soft rings. Thus we allocate so
117211878SVenu.Iyer@Sun.COM  * much array space for srs_tx_soft_rings.
117311878SVenu.Iyer@Sun.COM  *
117411878SVenu.Iyer@Sun.COM  * And when it is an aggr, again we allocate MAX_RINGS_PER_GROUP worth
117511878SVenu.Iyer@Sun.COM  * of space to st_soft_rings. This array is used for quick access to
117611878SVenu.Iyer@Sun.COM  * soft ring associated with a pseudo Tx ring based on the pseudo
117711878SVenu.Iyer@Sun.COM  * ring's index (mr_index).
117811878SVenu.Iyer@Sun.COM  */
11798275SEric Cheng static void
mac_srs_fanout_list_alloc(mac_soft_ring_set_t * mac_srs)11808275SEric Cheng mac_srs_fanout_list_alloc(mac_soft_ring_set_t *mac_srs)
11818275SEric Cheng {
118211878SVenu.Iyer@Sun.COM 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
118311878SVenu.Iyer@Sun.COM 
118411878SVenu.Iyer@Sun.COM 	if (mac_srs->srs_type & SRST_TX) {
118511878SVenu.Iyer@Sun.COM 		mac_srs->srs_tx_soft_rings = (mac_soft_ring_t **)
118611878SVenu.Iyer@Sun.COM 		    kmem_zalloc(sizeof (mac_soft_ring_t *) *
118711878SVenu.Iyer@Sun.COM 		    MAX_RINGS_PER_GROUP, KM_SLEEP);
118811878SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_IS_AGGR) {
118911878SVenu.Iyer@Sun.COM 			mac_srs_tx_t *tx = &mac_srs->srs_tx;
119011878SVenu.Iyer@Sun.COM 
119111878SVenu.Iyer@Sun.COM 			tx->st_soft_rings = (mac_soft_ring_t **)
119211878SVenu.Iyer@Sun.COM 			    kmem_zalloc(sizeof (mac_soft_ring_t *) *
119311878SVenu.Iyer@Sun.COM 			    MAX_RINGS_PER_GROUP, KM_SLEEP);
119411878SVenu.Iyer@Sun.COM 		}
119511878SVenu.Iyer@Sun.COM 	} else {
119611878SVenu.Iyer@Sun.COM 		mac_srs->srs_tcp_soft_rings = (mac_soft_ring_t **)
119711878SVenu.Iyer@Sun.COM 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
119811878SVenu.Iyer@Sun.COM 		    KM_SLEEP);
119911878SVenu.Iyer@Sun.COM 		mac_srs->srs_udp_soft_rings = (mac_soft_ring_t **)
120011878SVenu.Iyer@Sun.COM 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
120111878SVenu.Iyer@Sun.COM 		    KM_SLEEP);
120211878SVenu.Iyer@Sun.COM 		mac_srs->srs_oth_soft_rings = (mac_soft_ring_t **)
120311878SVenu.Iyer@Sun.COM 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
120411878SVenu.Iyer@Sun.COM 		    KM_SLEEP);
120511878SVenu.Iyer@Sun.COM 	}
12068275SEric Cheng }
12078275SEric Cheng 
12088275SEric Cheng static void
mac_srs_worker_bind(mac_soft_ring_set_t * mac_srs,processorid_t cpuid)12098275SEric Cheng mac_srs_worker_bind(mac_soft_ring_set_t *mac_srs, processorid_t cpuid)
12108275SEric Cheng {
12118275SEric Cheng 	cpu_t *cp;
12128275SEric Cheng 	boolean_t clear = B_FALSE;
12138275SEric Cheng 
12148275SEric Cheng 	ASSERT(MUTEX_HELD(&cpu_lock));
12158275SEric Cheng 
12168275SEric Cheng 	if (!mac_srs_thread_bind)
12178275SEric Cheng 		return;
12188275SEric Cheng 
12198275SEric Cheng 	cp = cpu_get(cpuid);
12208275SEric Cheng 	if (cp == NULL || !cpu_is_online(cp))
12218275SEric Cheng 		return;
12228275SEric Cheng 
12238275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
12248275SEric Cheng 	mac_srs->srs_state |= SRS_WORKER_BOUND;
12258275SEric Cheng 	if (mac_srs->srs_worker_cpuid != -1)
12268275SEric Cheng 		clear = B_TRUE;
12278275SEric Cheng 	mac_srs->srs_worker_cpuid = cpuid;
12288275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
12298275SEric Cheng 
12308275SEric Cheng 	if (clear)
12318275SEric Cheng 		thread_affinity_clear(mac_srs->srs_worker);
12328275SEric Cheng 
12338275SEric Cheng 	thread_affinity_set(mac_srs->srs_worker, cpuid);
12348275SEric Cheng 	DTRACE_PROBE1(worker__CPU, processorid_t, cpuid);
12358275SEric Cheng }
12368275SEric Cheng 
12378275SEric Cheng static void
mac_srs_poll_bind(mac_soft_ring_set_t * mac_srs,processorid_t cpuid)12388275SEric Cheng mac_srs_poll_bind(mac_soft_ring_set_t *mac_srs, processorid_t cpuid)
12398275SEric Cheng {
12408275SEric Cheng 	cpu_t *cp;
12418275SEric Cheng 	boolean_t clear = B_FALSE;
12428275SEric Cheng 
12438275SEric Cheng 	ASSERT(MUTEX_HELD(&cpu_lock));
12448275SEric Cheng 
12458275SEric Cheng 	if (!mac_srs_thread_bind || mac_srs->srs_poll_thr == NULL)
12468275SEric Cheng 		return;
12478275SEric Cheng 
12488275SEric Cheng 	cp = cpu_get(cpuid);
12498275SEric Cheng 	if (cp == NULL || !cpu_is_online(cp))
12508275SEric Cheng 		return;
12518275SEric Cheng 
12528275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
12538275SEric Cheng 	mac_srs->srs_state |= SRS_POLL_BOUND;
12548275SEric Cheng 	if (mac_srs->srs_poll_cpuid != -1)
12558275SEric Cheng 		clear = B_TRUE;
12568275SEric Cheng 	mac_srs->srs_poll_cpuid = cpuid;
12578275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
12588275SEric Cheng 
12598275SEric Cheng 	if (clear)
12608275SEric Cheng 		thread_affinity_clear(mac_srs->srs_poll_thr);
12618275SEric Cheng 
12628275SEric Cheng 	thread_affinity_set(mac_srs->srs_poll_thr, cpuid);
12638275SEric Cheng 	DTRACE_PROBE1(poll__CPU, processorid_t, cpuid);
12648275SEric Cheng }
12658275SEric Cheng 
12668275SEric Cheng /*
126711878SVenu.Iyer@Sun.COM  * Re-target interrupt to the passed CPU. If re-target is successful,
126811878SVenu.Iyer@Sun.COM  * set mc_rx_intr_cpu to the re-targeted CPU. Otherwise set it to -1.
126911878SVenu.Iyer@Sun.COM  */
127011878SVenu.Iyer@Sun.COM void
mac_rx_srs_retarget_intr(mac_soft_ring_set_t * mac_srs,processorid_t cpuid)127111878SVenu.Iyer@Sun.COM mac_rx_srs_retarget_intr(mac_soft_ring_set_t *mac_srs, processorid_t cpuid)
127211878SVenu.Iyer@Sun.COM {
127311878SVenu.Iyer@Sun.COM 	cpu_t *cp;
127411878SVenu.Iyer@Sun.COM 	mac_ring_t *ring = mac_srs->srs_ring;
127511878SVenu.Iyer@Sun.COM 	mac_intr_t *mintr = &ring->mr_info.mri_intr;
127611878SVenu.Iyer@Sun.COM 	flow_entry_t *flent = mac_srs->srs_flent;
127711878SVenu.Iyer@Sun.COM 	boolean_t primary = mac_is_primary_client(mac_srs->srs_mcip);
127811878SVenu.Iyer@Sun.COM 
127911878SVenu.Iyer@Sun.COM 	ASSERT(MUTEX_HELD(&cpu_lock));
128011878SVenu.Iyer@Sun.COM 
128111878SVenu.Iyer@Sun.COM 	/*
128211878SVenu.Iyer@Sun.COM 	 * Don't re-target the interrupt for these cases:
128311878SVenu.Iyer@Sun.COM 	 * 1) ring is NULL
128411878SVenu.Iyer@Sun.COM 	 * 2) the interrupt is shared (mi_ddi_shared)
128511878SVenu.Iyer@Sun.COM 	 * 3) ddi_handle is NULL and !primary
128611878SVenu.Iyer@Sun.COM 	 * 4) primary, ddi_handle is NULL but fe_rx_srs_cnt > 2
128711878SVenu.Iyer@Sun.COM 	 * Case 3 & 4 are because of mac_client_intr_cpu() routine.
128811878SVenu.Iyer@Sun.COM 	 * This routine will re-target fixed interrupt for primary
128911878SVenu.Iyer@Sun.COM 	 * mac client if the client has only one ring. In that
129011878SVenu.Iyer@Sun.COM 	 * case, mc_rx_intr_cpu will already have the correct value.
129111878SVenu.Iyer@Sun.COM 	 */
129211878SVenu.Iyer@Sun.COM 	if (ring == NULL || mintr->mi_ddi_shared || cpuid == -1 ||
129311878SVenu.Iyer@Sun.COM 	    (mintr->mi_ddi_handle == NULL && !primary) || (primary &&
129411878SVenu.Iyer@Sun.COM 	    mintr->mi_ddi_handle == NULL && flent->fe_rx_srs_cnt > 2)) {
129511878SVenu.Iyer@Sun.COM 		mac_srs->srs_cpu.mc_rx_intr_cpu = -1;
129611878SVenu.Iyer@Sun.COM 		return;
129711878SVenu.Iyer@Sun.COM 	}
129811878SVenu.Iyer@Sun.COM 
129911878SVenu.Iyer@Sun.COM 	if (mintr->mi_ddi_handle == NULL)
130011878SVenu.Iyer@Sun.COM 		return;
130111878SVenu.Iyer@Sun.COM 
130211878SVenu.Iyer@Sun.COM 	cp = cpu_get(cpuid);
130311878SVenu.Iyer@Sun.COM 	if (cp == NULL || !cpu_is_online(cp))
130411878SVenu.Iyer@Sun.COM 		return;
130511878SVenu.Iyer@Sun.COM 
1306*12564SGongtian.Zhao@Sun.COM 	/* Drop the cpu_lock as set_intr_affinity() holds it */
130711878SVenu.Iyer@Sun.COM 	mutex_exit(&cpu_lock);
1308*12564SGongtian.Zhao@Sun.COM 	if (set_intr_affinity(mintr->mi_ddi_handle, cpuid) == DDI_SUCCESS)
130911878SVenu.Iyer@Sun.COM 		mac_srs->srs_cpu.mc_rx_intr_cpu = cpuid;
131011878SVenu.Iyer@Sun.COM 	else
131111878SVenu.Iyer@Sun.COM 		mac_srs->srs_cpu.mc_rx_intr_cpu = -1;
131211878SVenu.Iyer@Sun.COM 	mutex_enter(&cpu_lock);
131311878SVenu.Iyer@Sun.COM }
131411878SVenu.Iyer@Sun.COM 
131511878SVenu.Iyer@Sun.COM /*
131611878SVenu.Iyer@Sun.COM  * Re-target Tx interrupts
131711878SVenu.Iyer@Sun.COM  */
131811878SVenu.Iyer@Sun.COM void
mac_tx_srs_retarget_intr(mac_soft_ring_set_t * mac_srs)131911878SVenu.Iyer@Sun.COM mac_tx_srs_retarget_intr(mac_soft_ring_set_t *mac_srs)
132011878SVenu.Iyer@Sun.COM {
132111878SVenu.Iyer@Sun.COM 	cpu_t *cp;
132211878SVenu.Iyer@Sun.COM 	mac_ring_t *ring;
132311878SVenu.Iyer@Sun.COM 	mac_intr_t *mintr;
132411878SVenu.Iyer@Sun.COM 	mac_soft_ring_t *sringp;
132511878SVenu.Iyer@Sun.COM 	mac_srs_tx_t *srs_tx;
132611878SVenu.Iyer@Sun.COM 	mac_cpus_t *srs_cpu;
132711878SVenu.Iyer@Sun.COM 	processorid_t cpuid;
132811878SVenu.Iyer@Sun.COM 	int i;
132911878SVenu.Iyer@Sun.COM 
133011878SVenu.Iyer@Sun.COM 	ASSERT(MUTEX_HELD(&cpu_lock));
133111878SVenu.Iyer@Sun.COM 
133211878SVenu.Iyer@Sun.COM 	srs_cpu = &mac_srs->srs_cpu;
133311878SVenu.Iyer@Sun.COM 	if (MAC_TX_SOFT_RINGS(mac_srs)) {
133411878SVenu.Iyer@Sun.COM 		for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
133511878SVenu.Iyer@Sun.COM 			sringp = mac_srs->srs_tx_soft_rings[i];
133611878SVenu.Iyer@Sun.COM 			ring = (mac_ring_t *)sringp->s_ring_tx_arg2;
133711878SVenu.Iyer@Sun.COM 			cpuid = srs_cpu->mc_tx_intr_cpu[i];
133811878SVenu.Iyer@Sun.COM 			cp = cpu_get(cpuid);
133911878SVenu.Iyer@Sun.COM 			if (cp == NULL || !cpu_is_online(cp) ||
134011878SVenu.Iyer@Sun.COM 			    !MAC_RING_RETARGETABLE(ring)) {
134111878SVenu.Iyer@Sun.COM 				srs_cpu->mc_tx_retargeted_cpu[i] = -1;
134211878SVenu.Iyer@Sun.COM 				continue;
134311878SVenu.Iyer@Sun.COM 			}
134411878SVenu.Iyer@Sun.COM 			mintr = &ring->mr_info.mri_intr;
134511878SVenu.Iyer@Sun.COM 			/*
1346*12564SGongtian.Zhao@Sun.COM 			 * Drop the cpu_lock as set_intr_affinity()
134711878SVenu.Iyer@Sun.COM 			 * holds it
134811878SVenu.Iyer@Sun.COM 			 */
134911878SVenu.Iyer@Sun.COM 			mutex_exit(&cpu_lock);
1350*12564SGongtian.Zhao@Sun.COM 			if (set_intr_affinity(mintr->mi_ddi_handle,
135111878SVenu.Iyer@Sun.COM 			    cpuid) == DDI_SUCCESS) {
135211878SVenu.Iyer@Sun.COM 				srs_cpu->mc_tx_retargeted_cpu[i] = cpuid;
135311878SVenu.Iyer@Sun.COM 			} else {
135411878SVenu.Iyer@Sun.COM 				srs_cpu->mc_tx_retargeted_cpu[i] = -1;
135511878SVenu.Iyer@Sun.COM 			}
135611878SVenu.Iyer@Sun.COM 			mutex_enter(&cpu_lock);
135711878SVenu.Iyer@Sun.COM 		}
135811878SVenu.Iyer@Sun.COM 	} else {
135911878SVenu.Iyer@Sun.COM 		cpuid = srs_cpu->mc_tx_intr_cpu[0];
136011878SVenu.Iyer@Sun.COM 		cp = cpu_get(cpuid);
136111878SVenu.Iyer@Sun.COM 		if (cp == NULL || !cpu_is_online(cp)) {
136211878SVenu.Iyer@Sun.COM 			srs_cpu->mc_tx_retargeted_cpu[0] = -1;
136311878SVenu.Iyer@Sun.COM 			return;
136411878SVenu.Iyer@Sun.COM 		}
136511878SVenu.Iyer@Sun.COM 		srs_tx = &mac_srs->srs_tx;
136611878SVenu.Iyer@Sun.COM 		ring = (mac_ring_t *)srs_tx->st_arg2;
136711878SVenu.Iyer@Sun.COM 		if (MAC_RING_RETARGETABLE(ring)) {
136811878SVenu.Iyer@Sun.COM 			mintr = &ring->mr_info.mri_intr;
136911878SVenu.Iyer@Sun.COM 			mutex_exit(&cpu_lock);
1370*12564SGongtian.Zhao@Sun.COM 			if ((set_intr_affinity(mintr->mi_ddi_handle,
137111878SVenu.Iyer@Sun.COM 			    cpuid) == DDI_SUCCESS)) {
137211878SVenu.Iyer@Sun.COM 				srs_cpu->mc_tx_retargeted_cpu[0] = cpuid;
137311878SVenu.Iyer@Sun.COM 			} else {
137411878SVenu.Iyer@Sun.COM 				srs_cpu->mc_tx_retargeted_cpu[0] = -1;
137511878SVenu.Iyer@Sun.COM 			}
137611878SVenu.Iyer@Sun.COM 			mutex_enter(&cpu_lock);
137711878SVenu.Iyer@Sun.COM 		}
137811878SVenu.Iyer@Sun.COM 	}
137911878SVenu.Iyer@Sun.COM }
138011878SVenu.Iyer@Sun.COM 
138111878SVenu.Iyer@Sun.COM /*
13828275SEric Cheng  * When a CPU comes back online, bind the MAC kernel threads which
13838275SEric Cheng  * were previously bound to that CPU, and had to be unbound because
13848275SEric Cheng  * the CPU was going away.
13858275SEric Cheng  *
13868275SEric Cheng  * These functions are called with cpu_lock held and hence we can't
13878275SEric Cheng  * cv_wait to grab the mac perimeter. Since these functions walk the soft
13888275SEric Cheng  * ring list of an SRS without being in the perimeter, the list itself
13898275SEric Cheng  * is protected by the SRS lock.
13908275SEric Cheng  */
13918275SEric Cheng static void
mac_walk_srs_and_bind(int cpuid)13928275SEric Cheng mac_walk_srs_and_bind(int cpuid)
13938275SEric Cheng {
13948275SEric Cheng 	mac_soft_ring_set_t *mac_srs;
13958275SEric Cheng 	mac_soft_ring_t *soft_ring;
13968275SEric Cheng 
13978275SEric Cheng 	rw_enter(&mac_srs_g_lock, RW_READER);
13988275SEric Cheng 
13998275SEric Cheng 	if ((mac_srs = mac_srs_g_list) == NULL)
14008275SEric Cheng 		goto done;
14018275SEric Cheng 
14028275SEric Cheng 	for (; mac_srs != NULL; mac_srs = mac_srs->srs_next) {
14038275SEric Cheng 		if (mac_srs->srs_worker_cpuid == -1 &&
14048275SEric Cheng 		    mac_srs->srs_worker_cpuid_save == cpuid) {
14058275SEric Cheng 			mac_srs->srs_worker_cpuid_save = -1;
14068275SEric Cheng 			mac_srs_worker_bind(mac_srs, cpuid);
14078275SEric Cheng 		}
14088275SEric Cheng 
14098275SEric Cheng 		if (!(mac_srs->srs_type & SRST_TX)) {
14108275SEric Cheng 			if (mac_srs->srs_poll_cpuid == -1 &&
14118275SEric Cheng 			    mac_srs->srs_poll_cpuid_save == cpuid) {
14128275SEric Cheng 				mac_srs->srs_poll_cpuid_save = -1;
14138275SEric Cheng 				mac_srs_poll_bind(mac_srs, cpuid);
14148275SEric Cheng 			}
14158275SEric Cheng 		}
14168275SEric Cheng 
14178275SEric Cheng 		/* Next tackle the soft rings associated with the srs */
14188275SEric Cheng 		mutex_enter(&mac_srs->srs_lock);
14198275SEric Cheng 		for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
14208275SEric Cheng 		    soft_ring = soft_ring->s_ring_next) {
14218275SEric Cheng 			if (soft_ring->s_ring_cpuid == -1 &&
14228275SEric Cheng 			    soft_ring->s_ring_cpuid_save == cpuid) {
14238275SEric Cheng 				soft_ring->s_ring_cpuid_save = -1;
14248275SEric Cheng 				(void) mac_soft_ring_bind(soft_ring, cpuid);
14258275SEric Cheng 			}
14268275SEric Cheng 		}
14278275SEric Cheng 		mutex_exit(&mac_srs->srs_lock);
14288275SEric Cheng 	}
14298275SEric Cheng done:
14308275SEric Cheng 	rw_exit(&mac_srs_g_lock);
14318275SEric Cheng }
14328275SEric Cheng 
14338275SEric Cheng /*
14348275SEric Cheng  * Change the priority of the SRS's poll and worker thread. Additionally,
14358275SEric Cheng  * update the priority of the worker threads for the SRS's soft rings.
14368275SEric Cheng  * Need to modify any associated squeue threads.
14378275SEric Cheng  */
14388275SEric Cheng void
mac_update_srs_priority(mac_soft_ring_set_t * mac_srs,pri_t prival)14398275SEric Cheng mac_update_srs_priority(mac_soft_ring_set_t *mac_srs, pri_t prival)
14408275SEric Cheng {
14418275SEric Cheng 	mac_soft_ring_t		*ringp;
14428275SEric Cheng 
14438275SEric Cheng 	mac_srs->srs_pri = prival;
14448275SEric Cheng 	thread_lock(mac_srs->srs_worker);
14458275SEric Cheng 	(void) thread_change_pri(mac_srs->srs_worker, mac_srs->srs_pri, 0);
14468275SEric Cheng 	thread_unlock(mac_srs->srs_worker);
14478275SEric Cheng 	if (mac_srs->srs_poll_thr != NULL) {
14488275SEric Cheng 		thread_lock(mac_srs->srs_poll_thr);
14498275SEric Cheng 		(void) thread_change_pri(mac_srs->srs_poll_thr,
14508275SEric Cheng 		    mac_srs->srs_pri, 0);
14518275SEric Cheng 		thread_unlock(mac_srs->srs_poll_thr);
14528275SEric Cheng 	}
14538275SEric Cheng 	if ((ringp = mac_srs->srs_soft_ring_head) == NULL)
14548275SEric Cheng 		return;
14558275SEric Cheng 	while (ringp != mac_srs->srs_soft_ring_tail) {
14568275SEric Cheng 		thread_lock(ringp->s_ring_worker);
14578275SEric Cheng 		(void) thread_change_pri(ringp->s_ring_worker,
14588275SEric Cheng 		    mac_srs->srs_pri, 0);
14598275SEric Cheng 		thread_unlock(ringp->s_ring_worker);
14608275SEric Cheng 		ringp = ringp->s_ring_next;
14618275SEric Cheng 	}
14628275SEric Cheng 	ASSERT(ringp == mac_srs->srs_soft_ring_tail);
14638275SEric Cheng 	thread_lock(ringp->s_ring_worker);
14648275SEric Cheng 	(void) thread_change_pri(ringp->s_ring_worker, mac_srs->srs_pri, 0);
14658275SEric Cheng 	thread_unlock(ringp->s_ring_worker);
14668275SEric Cheng }
14678275SEric Cheng 
14688275SEric Cheng /*
14698275SEric Cheng  * Change the receive bandwidth limit.
14708275SEric Cheng  */
14718275SEric Cheng static void
mac_rx_srs_update_bwlimit(mac_soft_ring_set_t * srs,mac_resource_props_t * mrp)14728275SEric Cheng mac_rx_srs_update_bwlimit(mac_soft_ring_set_t *srs, mac_resource_props_t *mrp)
14738275SEric Cheng {
14748275SEric Cheng 	mac_soft_ring_t		*softring;
14758275SEric Cheng 
14768275SEric Cheng 	mutex_enter(&srs->srs_lock);
14778275SEric Cheng 	mutex_enter(&srs->srs_bw->mac_bw_lock);
14788275SEric Cheng 
14798275SEric Cheng 	if (mrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
14808275SEric Cheng 		/* Reset bandwidth limit */
14818275SEric Cheng 		if (srs->srs_type & SRST_BW_CONTROL) {
14828275SEric Cheng 			softring = srs->srs_soft_ring_head;
14838275SEric Cheng 			while (softring != NULL) {
14848275SEric Cheng 				softring->s_ring_type &= ~ST_RING_BW_CTL;
14858275SEric Cheng 				softring = softring->s_ring_next;
14868275SEric Cheng 			}
14878275SEric Cheng 			srs->srs_type &= ~SRST_BW_CONTROL;
14888275SEric Cheng 			srs->srs_drain_func = mac_rx_srs_drain;
14898275SEric Cheng 		}
14908275SEric Cheng 	} else {
14918275SEric Cheng 		/* Set/Modify bandwidth limit */
14928275SEric Cheng 		srs->srs_bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
14938275SEric Cheng 		/*
14948275SEric Cheng 		 * Give twice the queuing capability before
14958275SEric Cheng 		 * dropping packets. The unit is bytes/tick.
14968275SEric Cheng 		 */
14978275SEric Cheng 		srs->srs_bw->mac_bw_drop_threshold =
14988275SEric Cheng 		    srs->srs_bw->mac_bw_limit << 1;
14998275SEric Cheng 		if (!(srs->srs_type & SRST_BW_CONTROL)) {
15008275SEric Cheng 			softring = srs->srs_soft_ring_head;
15018275SEric Cheng 			while (softring != NULL) {
15028275SEric Cheng 				softring->s_ring_type |= ST_RING_BW_CTL;
15038275SEric Cheng 				softring = softring->s_ring_next;
15048275SEric Cheng 			}
15058275SEric Cheng 			srs->srs_type |= SRST_BW_CONTROL;
15068275SEric Cheng 			srs->srs_drain_func = mac_rx_srs_drain_bw;
15078275SEric Cheng 		}
15088275SEric Cheng 	}
15098275SEric Cheng done:
15108275SEric Cheng 	mutex_exit(&srs->srs_bw->mac_bw_lock);
15118275SEric Cheng 	mutex_exit(&srs->srs_lock);
15128275SEric Cheng }
15138275SEric Cheng 
15148275SEric Cheng /* Change the transmit bandwidth limit */
15158275SEric Cheng static void
mac_tx_srs_update_bwlimit(mac_soft_ring_set_t * srs,mac_resource_props_t * mrp)15168275SEric Cheng mac_tx_srs_update_bwlimit(mac_soft_ring_set_t *srs, mac_resource_props_t *mrp)
15178275SEric Cheng {
151811878SVenu.Iyer@Sun.COM 	uint32_t		tx_mode, ring_info = 0;
15198961SEric Cheng 	mac_srs_tx_t		*srs_tx = &srs->srs_tx;
15208961SEric Cheng 	mac_client_impl_t	*mcip = srs->srs_mcip;
15218961SEric Cheng 
15228961SEric Cheng 	/*
15238961SEric Cheng 	 * We need to quiesce/restart the client here because mac_tx() and
15248961SEric Cheng 	 * srs->srs_tx->st_func do not hold srs->srs_lock while accessing
15258961SEric Cheng 	 * st_mode and related fields, which are modified by the code below.
15268961SEric Cheng 	 */
152711878SVenu.Iyer@Sun.COM 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
15288275SEric Cheng 
15298275SEric Cheng 	mutex_enter(&srs->srs_lock);
15308275SEric Cheng 	mutex_enter(&srs->srs_bw->mac_bw_lock);
15318275SEric Cheng 
15328275SEric Cheng 	tx_mode = srs_tx->st_mode;
15338275SEric Cheng 	if (mrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
15348275SEric Cheng 		/* Reset bandwidth limit */
15358275SEric Cheng 		if (tx_mode == SRS_TX_BW) {
153611878SVenu.Iyer@Sun.COM 			if (srs_tx->st_arg2 != NULL)
153711878SVenu.Iyer@Sun.COM 				ring_info = mac_hwring_getinfo(srs_tx->st_arg2);
15388275SEric Cheng 			if (mac_tx_serialize ||
153911878SVenu.Iyer@Sun.COM 			    (ring_info & MAC_RING_TX_SERIALIZE)) {
15408275SEric Cheng 				srs_tx->st_mode = SRS_TX_SERIALIZE;
15418275SEric Cheng 			} else {
15428275SEric Cheng 				srs_tx->st_mode = SRS_TX_DEFAULT;
15438275SEric Cheng 			}
15448275SEric Cheng 		} else if (tx_mode == SRS_TX_BW_FANOUT) {
15458275SEric Cheng 			srs_tx->st_mode = SRS_TX_FANOUT;
154611878SVenu.Iyer@Sun.COM 		} else if (tx_mode == SRS_TX_BW_AGGR) {
154711878SVenu.Iyer@Sun.COM 			srs_tx->st_mode = SRS_TX_AGGR;
15488275SEric Cheng 		}
15498275SEric Cheng 		srs->srs_type &= ~SRST_BW_CONTROL;
15508275SEric Cheng 	} else {
15518275SEric Cheng 		/* Set/Modify bandwidth limit */
15528275SEric Cheng 		srs->srs_bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
15538275SEric Cheng 		/*
15548275SEric Cheng 		 * Give twice the queuing capability before
15558275SEric Cheng 		 * dropping packets. The unit is bytes/tick.
15568275SEric Cheng 		 */
15578275SEric Cheng 		srs->srs_bw->mac_bw_drop_threshold =
15588275SEric Cheng 		    srs->srs_bw->mac_bw_limit << 1;
15598275SEric Cheng 		srs->srs_type |= SRST_BW_CONTROL;
156011878SVenu.Iyer@Sun.COM 		if (tx_mode != SRS_TX_BW && tx_mode != SRS_TX_BW_FANOUT &&
156111878SVenu.Iyer@Sun.COM 		    tx_mode != SRS_TX_BW_AGGR) {
15628275SEric Cheng 			if (tx_mode == SRS_TX_SERIALIZE ||
15638275SEric Cheng 			    tx_mode == SRS_TX_DEFAULT) {
15648275SEric Cheng 				srs_tx->st_mode = SRS_TX_BW;
15658275SEric Cheng 			} else if (tx_mode == SRS_TX_FANOUT) {
15668275SEric Cheng 				srs_tx->st_mode = SRS_TX_BW_FANOUT;
156711878SVenu.Iyer@Sun.COM 			} else if (tx_mode == SRS_TX_AGGR) {
156811878SVenu.Iyer@Sun.COM 				srs_tx->st_mode = SRS_TX_BW_AGGR;
15698275SEric Cheng 			} else {
15708275SEric Cheng 				ASSERT(0);
15718275SEric Cheng 			}
15728275SEric Cheng 		}
15738275SEric Cheng 	}
15748275SEric Cheng done:
15758275SEric Cheng 	srs_tx->st_func = mac_tx_get_func(srs_tx->st_mode);
15768275SEric Cheng 	mutex_exit(&srs->srs_bw->mac_bw_lock);
15778275SEric Cheng 	mutex_exit(&srs->srs_lock);
15788961SEric Cheng 
157911878SVenu.Iyer@Sun.COM 	mac_tx_client_restart((mac_client_handle_t)mcip);
15808275SEric Cheng }
15818275SEric Cheng 
15828275SEric Cheng /*
15838275SEric Cheng  * The uber function that deals with any update to bandwidth limits.
15848275SEric Cheng  */
15858275SEric Cheng void
mac_srs_update_bwlimit(flow_entry_t * flent,mac_resource_props_t * mrp)15868275SEric Cheng mac_srs_update_bwlimit(flow_entry_t *flent, mac_resource_props_t *mrp)
15878275SEric Cheng {
15888275SEric Cheng 	int			count;
15898275SEric Cheng 
15908275SEric Cheng 	for (count = 0; count < flent->fe_rx_srs_cnt; count++)
15918275SEric Cheng 		mac_rx_srs_update_bwlimit(flent->fe_rx_srs[count], mrp);
15928275SEric Cheng 	mac_tx_srs_update_bwlimit(flent->fe_tx_srs, mrp);
15938275SEric Cheng }
15948275SEric Cheng 
15958275SEric Cheng void
mac_srs_change_upcall(void * arg,mac_direct_rx_t rx_func,void * rx_arg1)15968275SEric Cheng mac_srs_change_upcall(void *arg, mac_direct_rx_t rx_func, void *rx_arg1)
15978275SEric Cheng {
15988275SEric Cheng 	mac_soft_ring_set_t	*mac_srs = arg;
15998275SEric Cheng 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
16008275SEric Cheng 	mac_soft_ring_t		*softring;
16018275SEric Cheng 
16028275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
16038275SEric Cheng 	ASSERT((mac_srs->srs_type & SRST_TX) == 0);
16048275SEric Cheng 	srs_rx->sr_func = rx_func;
16058275SEric Cheng 	srs_rx->sr_arg1 = rx_arg1;
16068275SEric Cheng 
16078275SEric Cheng 	softring = mac_srs->srs_soft_ring_head;
16088275SEric Cheng 	while (softring != NULL) {
16098275SEric Cheng 		mutex_enter(&softring->s_ring_lock);
16108275SEric Cheng 		softring->s_ring_rx_func = rx_func;
16118275SEric Cheng 		softring->s_ring_rx_arg1 = rx_arg1;
16128275SEric Cheng 		mutex_exit(&softring->s_ring_lock);
16138275SEric Cheng 		softring = softring->s_ring_next;
16148275SEric Cheng 	}
16158275SEric Cheng 
16168275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
16178275SEric Cheng }
16188275SEric Cheng 
16198275SEric Cheng /*
16208275SEric Cheng  * When the first sub-flow is added to a link, we disable polling on the
16218275SEric Cheng  * link and also modify the entry point to mac_rx_srs_subflow_process.
16228275SEric Cheng  * (polling is disabled because with the subflow added, accounting
16238275SEric Cheng  * for polling needs additional logic, it is assumed that when a subflow is
16248275SEric Cheng  * added, we can take some hit as a result of disabling polling rather than
16258275SEric Cheng  * adding more complexity - if this becomes a perf. issue we need to
16268275SEric Cheng  * re-rvaluate this logic).  When the last subflow is removed, we turn back
16278275SEric Cheng  * polling and also reset the entry point to mac_rx_srs_process.
16288275SEric Cheng  *
16298275SEric Cheng  * In the future if there are multiple SRS, we can simply
16308275SEric Cheng  * take one and give it to the flow rather than disabling polling and
16318275SEric Cheng  * resetting the entry point.
16328275SEric Cheng  */
16338275SEric Cheng void
mac_client_update_classifier(mac_client_impl_t * mcip,boolean_t enable)16348275SEric Cheng mac_client_update_classifier(mac_client_impl_t *mcip, boolean_t enable)
16358275SEric Cheng {
16368275SEric Cheng 	flow_entry_t		*flent = mcip->mci_flent;
16378275SEric Cheng 	int			i;
16388275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
16398275SEric Cheng 	mac_rx_func_t		rx_func;
16408275SEric Cheng 	uint_t			rx_srs_cnt;
16418275SEric Cheng 	boolean_t		enable_classifier;
16428275SEric Cheng 
16438275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
16448275SEric Cheng 
16458275SEric Cheng 	enable_classifier = !FLOW_TAB_EMPTY(mcip->mci_subflow_tab) && enable;
16468275SEric Cheng 
16478275SEric Cheng 	rx_func = enable_classifier ? mac_rx_srs_subflow_process :
16488275SEric Cheng 	    mac_rx_srs_process;
16498275SEric Cheng 
165010491SRishi.Srivatsavai@Sun.COM 	/* Tell mac_srs_poll_state_change to disable polling if necessary */
165110491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_state_flags & MIS_POLL_DISABLE)
165210491SRishi.Srivatsavai@Sun.COM 		enable_classifier = B_TRUE;
165310491SRishi.Srivatsavai@Sun.COM 
16548275SEric Cheng 	/*
16558275SEric Cheng 	 * If receive function has already been configured correctly for
16568275SEric Cheng 	 * current subflow configuration, do nothing.
16578275SEric Cheng 	 */
16588275SEric Cheng 	if (flent->fe_cb_fn == (flow_fn_t)rx_func)
16598275SEric Cheng 		return;
16608275SEric Cheng 
16618275SEric Cheng 	rx_srs_cnt = flent->fe_rx_srs_cnt;
16628275SEric Cheng 	for (i = 0; i < rx_srs_cnt; i++) {
16638275SEric Cheng 		ASSERT(flent->fe_rx_srs[i] != NULL);
16648275SEric Cheng 		mac_srs_poll_state_change(flent->fe_rx_srs[i],
16658275SEric Cheng 		    enable_classifier, rx_func);
16668275SEric Cheng 	}
16678275SEric Cheng 
16688275SEric Cheng 	/*
16698275SEric Cheng 	 * Change the S/W classifier so that we can land in the
16708275SEric Cheng 	 * correct processing function with correct argument.
16718275SEric Cheng 	 * If all subflows have been removed we can revert to
16728275SEric Cheng 	 * mac_rx_srsprocess, else we need mac_rx_srs_subflow_process.
16738275SEric Cheng 	 */
16748275SEric Cheng 	mutex_enter(&flent->fe_lock);
16758275SEric Cheng 	flent->fe_cb_fn = (flow_fn_t)rx_func;
16768275SEric Cheng 	flent->fe_cb_arg1 = (void *)mip;
16778275SEric Cheng 	flent->fe_cb_arg2 = flent->fe_rx_srs[0];
16788275SEric Cheng 	mutex_exit(&flent->fe_lock);
16798275SEric Cheng }
16808275SEric Cheng 
16818275SEric Cheng static void
mac_srs_update_fanout_list(mac_soft_ring_set_t * mac_srs)16828275SEric Cheng mac_srs_update_fanout_list(mac_soft_ring_set_t *mac_srs)
16838275SEric Cheng {
168411878SVenu.Iyer@Sun.COM 	int tcp_count = 0, udp_count = 0, oth_count = 0, tx_count = 0;
16858275SEric Cheng 	mac_soft_ring_t *softring;
16868275SEric Cheng 
16878275SEric Cheng 	softring = mac_srs->srs_soft_ring_head;
16888275SEric Cheng 	if (softring == NULL) {
16898275SEric Cheng 		ASSERT(mac_srs->srs_soft_ring_count == 0);
16908275SEric Cheng 		mac_srs->srs_tcp_ring_count = 0;
16918275SEric Cheng 		mac_srs->srs_udp_ring_count = 0;
16928275SEric Cheng 		mac_srs->srs_oth_ring_count = 0;
169311878SVenu.Iyer@Sun.COM 		mac_srs->srs_tx_ring_count = 0;
16948275SEric Cheng 		return;
16958275SEric Cheng 	}
16968275SEric Cheng 
16978275SEric Cheng 	while (softring != NULL) {
169811878SVenu.Iyer@Sun.COM 		if (softring->s_ring_type & ST_RING_TCP) {
16998275SEric Cheng 			mac_srs->srs_tcp_soft_rings[tcp_count++] = softring;
170011878SVenu.Iyer@Sun.COM 		} else if (softring->s_ring_type & ST_RING_UDP) {
17018275SEric Cheng 			mac_srs->srs_udp_soft_rings[udp_count++] = softring;
170211878SVenu.Iyer@Sun.COM 		} else if (softring->s_ring_type & ST_RING_OTH) {
17038275SEric Cheng 			mac_srs->srs_oth_soft_rings[oth_count++] = softring;
170411878SVenu.Iyer@Sun.COM 		} else {
170511878SVenu.Iyer@Sun.COM 			ASSERT(softring->s_ring_type & ST_RING_TX);
170611878SVenu.Iyer@Sun.COM 			mac_srs->srs_tx_soft_rings[tx_count++] = softring;
170711878SVenu.Iyer@Sun.COM 		}
17088275SEric Cheng 		softring = softring->s_ring_next;
17098275SEric Cheng 	}
17108275SEric Cheng 
17118275SEric Cheng 	ASSERT(mac_srs->srs_soft_ring_count ==
171211878SVenu.Iyer@Sun.COM 	    (tcp_count + udp_count + oth_count + tx_count));
17138275SEric Cheng 	mac_srs->srs_tcp_ring_count = tcp_count;
17148275SEric Cheng 	mac_srs->srs_udp_ring_count = udp_count;
17158275SEric Cheng 	mac_srs->srs_oth_ring_count = oth_count;
171611878SVenu.Iyer@Sun.COM 	mac_srs->srs_tx_ring_count = tx_count;
17178275SEric Cheng }
17188275SEric Cheng 
17198275SEric Cheng void
mac_srs_create_proto_softrings(int id,uint16_t type,pri_t pri,mac_client_impl_t * mcip,mac_soft_ring_set_t * mac_srs,processorid_t cpuid,mac_direct_rx_t rx_func,void * x_arg1,mac_resource_handle_t x_arg2,boolean_t set_bypass)172011878SVenu.Iyer@Sun.COM mac_srs_create_proto_softrings(int id, uint16_t type, pri_t pri,
172111878SVenu.Iyer@Sun.COM     mac_client_impl_t *mcip, mac_soft_ring_set_t *mac_srs,
17228275SEric Cheng     processorid_t cpuid, mac_direct_rx_t rx_func, void *x_arg1,
17238275SEric Cheng     mac_resource_handle_t x_arg2, boolean_t set_bypass)
17248275SEric Cheng {
17258275SEric Cheng 	mac_soft_ring_t	*softring;
17268275SEric Cheng 	mac_rx_fifo_t	mrf;
17278275SEric Cheng 
17288275SEric Cheng 	bzero(&mrf, sizeof (mac_rx_fifo_t));
17298275SEric Cheng 	mrf.mrf_type = MAC_RX_FIFO;
17308275SEric Cheng 	mrf.mrf_receive = (mac_receive_t)mac_soft_ring_poll;
17318275SEric Cheng 	mrf.mrf_intr_enable =
17328275SEric Cheng 	    (mac_intr_enable_t)mac_soft_ring_intr_enable;
17338275SEric Cheng 	mrf.mrf_intr_disable =
17348275SEric Cheng 	    (mac_intr_disable_t)mac_soft_ring_intr_disable;
17358275SEric Cheng 	mrf.mrf_flow_priority = pri;
17368275SEric Cheng 
17378275SEric Cheng 	softring = mac_soft_ring_create(id, mac_soft_ring_worker_wait,
173811878SVenu.Iyer@Sun.COM 	    (type|ST_RING_TCP), pri, mcip, mac_srs,
17398275SEric Cheng 	    cpuid, rx_func, x_arg1, x_arg2);
17408275SEric Cheng 	softring->s_ring_rx_arg2 = NULL;
17418275SEric Cheng 
17428275SEric Cheng 	/*
17438275SEric Cheng 	 * TCP and UDP support DLS bypass. In addition TCP
17448275SEric Cheng 	 * squeue can also poll their corresponding soft rings.
17458275SEric Cheng 	 */
17468275SEric Cheng 	if (set_bypass && (mcip->mci_resource_arg != NULL)) {
17478275SEric Cheng 		mac_soft_ring_dls_bypass(softring,
17488275SEric Cheng 		    mcip->mci_direct_rx_fn,
17498275SEric Cheng 		    mcip->mci_direct_rx_arg);
17508275SEric Cheng 
17518275SEric Cheng 		mrf.mrf_rx_arg = softring;
17528275SEric Cheng 		mrf.mrf_intr_handle = (mac_intr_handle_t)softring;
17538275SEric Cheng 
17548275SEric Cheng 		/*
17558275SEric Cheng 		 * Make a call in IP to get a TCP squeue assigned to
17568275SEric Cheng 		 * this softring to maintain full CPU locality through
17578275SEric Cheng 		 * the stack and allow the squeue to be able to poll
17588275SEric Cheng 		 * the softring so the flow control can be pushed
17598275SEric Cheng 		 * all the way to H/W.
17608275SEric Cheng 		 */
17618275SEric Cheng 		softring->s_ring_rx_arg2 =
17628275SEric Cheng 		    mcip->mci_resource_add((void *)mcip->mci_resource_arg,
17638275SEric Cheng 		    (mac_resource_t *)&mrf);
17648275SEric Cheng 	}
17658275SEric Cheng 
17668275SEric Cheng 	/*
17678275SEric Cheng 	 * Non-TCP protocols don't support squeues. Hence we
17688275SEric Cheng 	 * don't make any ring addition callbacks for non-TCP
17698275SEric Cheng 	 * rings. Now create the UDP softring and allow it to
17708275SEric Cheng 	 * bypass the DLS layer.
17718275SEric Cheng 	 */
17728275SEric Cheng 	softring = mac_soft_ring_create(id, mac_soft_ring_worker_wait,
177311878SVenu.Iyer@Sun.COM 	    (type|ST_RING_UDP), pri, mcip, mac_srs,
17748275SEric Cheng 	    cpuid, rx_func, x_arg1, x_arg2);
17758275SEric Cheng 	softring->s_ring_rx_arg2 = NULL;
17768275SEric Cheng 
17778275SEric Cheng 	if (set_bypass && (mcip->mci_resource_arg != NULL)) {
17788275SEric Cheng 		mac_soft_ring_dls_bypass(softring,
17798275SEric Cheng 		    mcip->mci_direct_rx_fn,
17808275SEric Cheng 		    mcip->mci_direct_rx_arg);
17818275SEric Cheng 	}
17828275SEric Cheng 
17838275SEric Cheng 	/* Create the Oth softrings which has to go through the DLS */
17848275SEric Cheng 	softring = mac_soft_ring_create(id, mac_soft_ring_worker_wait,
178511878SVenu.Iyer@Sun.COM 	    (type|ST_RING_OTH), pri, mcip, mac_srs,
17868275SEric Cheng 	    cpuid, rx_func, x_arg1, x_arg2);
17878275SEric Cheng 	softring->s_ring_rx_arg2 = NULL;
17888275SEric Cheng }
17898275SEric Cheng 
17908275SEric Cheng /*
17918275SEric Cheng  * This routine associates a CPU or a set of CPU to process incoming
17928275SEric Cheng  * traffic from a mac client. If multiple CPUs are specified, then
17938275SEric Cheng  * so many soft rings are created with each soft ring worker thread
17948275SEric Cheng  * bound to a CPU in the set. Each soft ring in turn will be
17958275SEric Cheng  * associated with an squeue and the squeue will be moved to the
17968275SEric Cheng  * same CPU as that of the soft ring's.
17978275SEric Cheng  */
17988275SEric Cheng static void
mac_srs_fanout_modify(mac_client_impl_t * mcip,mac_direct_rx_t rx_func,void * x_arg1,mac_resource_handle_t x_arg2,mac_soft_ring_set_t * mac_rx_srs,mac_soft_ring_set_t * mac_tx_srs)179911878SVenu.Iyer@Sun.COM mac_srs_fanout_modify(mac_client_impl_t *mcip, mac_direct_rx_t rx_func,
180011878SVenu.Iyer@Sun.COM     void *x_arg1, mac_resource_handle_t x_arg2,
180111878SVenu.Iyer@Sun.COM     mac_soft_ring_set_t *mac_rx_srs, mac_soft_ring_set_t *mac_tx_srs)
18028275SEric Cheng {
18038275SEric Cheng 	mac_soft_ring_t *softring;
18048833SVenu.Iyer@Sun.COM 	uint32_t soft_ring_flag = 0;
18058275SEric Cheng 	processorid_t cpuid = -1;
18068275SEric Cheng 	int i, srings_present, new_fanout_cnt;
18078275SEric Cheng 	mac_cpus_t *srs_cpu;
18088275SEric Cheng 
18098275SEric Cheng 	/* fanout state is REINIT. Set it back to INIT */
18108275SEric Cheng 	ASSERT(mac_rx_srs->srs_fanout_state == SRS_FANOUT_REINIT);
18118275SEric Cheng 	mac_rx_srs->srs_fanout_state = SRS_FANOUT_INIT;
18128275SEric Cheng 
18138275SEric Cheng 	/* how many are present right now */
18148275SEric Cheng 	srings_present = mac_rx_srs->srs_tcp_ring_count;
18158275SEric Cheng 	/* new request */
18168275SEric Cheng 	srs_cpu = &mac_rx_srs->srs_cpu;
181711878SVenu.Iyer@Sun.COM 	new_fanout_cnt = srs_cpu->mc_rx_fanout_cnt;
18188275SEric Cheng 
18198275SEric Cheng 	mutex_enter(&mac_rx_srs->srs_lock);
18208275SEric Cheng 	if (mac_rx_srs->srs_type & SRST_BW_CONTROL)
18218275SEric Cheng 		soft_ring_flag |= ST_RING_BW_CTL;
18228275SEric Cheng 	mutex_exit(&mac_rx_srs->srs_lock);
18238275SEric Cheng 
18248275SEric Cheng 	if (new_fanout_cnt > srings_present) {
18258275SEric Cheng 		/* soft rings increased */
18268275SEric Cheng 		mutex_enter(&mac_rx_srs->srs_lock);
18278275SEric Cheng 		mac_rx_srs->srs_type |= SRST_FANOUT_SRC_IP;
18288275SEric Cheng 		mutex_exit(&mac_rx_srs->srs_lock);
18298275SEric Cheng 
18308275SEric Cheng 		for (i = mac_rx_srs->srs_tcp_ring_count;
18318275SEric Cheng 		    i < new_fanout_cnt; i++) {
18328275SEric Cheng 			/*
18338275SEric Cheng 			 * Create the protocol softrings and set the
18348275SEric Cheng 			 * DLS bypass where possible.
18358275SEric Cheng 			 */
183611878SVenu.Iyer@Sun.COM 			mac_srs_create_proto_softrings(i, soft_ring_flag,
18378275SEric Cheng 			    mac_rx_srs->srs_pri, mcip, mac_rx_srs, cpuid,
18388275SEric Cheng 			    rx_func, x_arg1, x_arg2, B_TRUE);
18398275SEric Cheng 		}
18408275SEric Cheng 		mac_srs_update_fanout_list(mac_rx_srs);
18418275SEric Cheng 	} else if (new_fanout_cnt < srings_present) {
18428275SEric Cheng 		/* soft rings decreased */
18438275SEric Cheng 		if (new_fanout_cnt == 1) {
18448275SEric Cheng 			mutex_enter(&mac_rx_srs->srs_lock);
18458275SEric Cheng 			mac_rx_srs->srs_type &= ~SRST_FANOUT_SRC_IP;
18468275SEric Cheng 			ASSERT(mac_rx_srs->srs_type & SRST_FANOUT_PROTO);
18478275SEric Cheng 			mutex_exit(&mac_rx_srs->srs_lock);
18488275SEric Cheng 		}
18498275SEric Cheng 		/* Get rid of extra soft rings */
18508275SEric Cheng 		for (i = new_fanout_cnt;
18518275SEric Cheng 		    i < mac_rx_srs->srs_tcp_ring_count; i++) {
18528275SEric Cheng 			softring = mac_rx_srs->srs_tcp_soft_rings[i];
18538275SEric Cheng 			if (softring->s_ring_rx_arg2 != NULL) {
18548275SEric Cheng 				mcip->mci_resource_remove(
18558275SEric Cheng 				    (void *)mcip->mci_resource_arg,
18568275SEric Cheng 				    softring->s_ring_rx_arg2);
18578275SEric Cheng 			}
18588275SEric Cheng 			mac_soft_ring_remove(mac_rx_srs,
18598275SEric Cheng 			    mac_rx_srs->srs_tcp_soft_rings[i]);
18608275SEric Cheng 			mac_soft_ring_remove(mac_rx_srs,
18618275SEric Cheng 			    mac_rx_srs->srs_udp_soft_rings[i]);
18628275SEric Cheng 			mac_soft_ring_remove(mac_rx_srs,
18638275SEric Cheng 			    mac_rx_srs->srs_oth_soft_rings[i]);
18648275SEric Cheng 		}
18658275SEric Cheng 		mac_srs_update_fanout_list(mac_rx_srs);
18668275SEric Cheng 	}
18678275SEric Cheng 
18688275SEric Cheng 	ASSERT(new_fanout_cnt == mac_rx_srs->srs_tcp_ring_count);
18698275SEric Cheng 	mutex_enter(&cpu_lock);
18708275SEric Cheng 	for (i = 0; i < mac_rx_srs->srs_tcp_ring_count; i++) {
187111878SVenu.Iyer@Sun.COM 		cpuid = srs_cpu->mc_rx_fanout_cpus[i];
18728275SEric Cheng 		(void) mac_soft_ring_bind(mac_rx_srs->srs_udp_soft_rings[i],
18738275SEric Cheng 		    cpuid);
18748275SEric Cheng 		(void) mac_soft_ring_bind(mac_rx_srs->srs_oth_soft_rings[i],
18758275SEric Cheng 		    cpuid);
18768275SEric Cheng 		(void) mac_soft_ring_bind(mac_rx_srs->srs_tcp_soft_rings[i],
18778275SEric Cheng 		    cpuid);
18788275SEric Cheng 		softring = mac_rx_srs->srs_tcp_soft_rings[i];
18798275SEric Cheng 		if (softring->s_ring_rx_arg2 != NULL) {
18808275SEric Cheng 			mcip->mci_resource_bind((void *)mcip->mci_resource_arg,
18818275SEric Cheng 			    softring->s_ring_rx_arg2, cpuid);
18828275SEric Cheng 		}
18838275SEric Cheng 	}
18848275SEric Cheng 
188511878SVenu.Iyer@Sun.COM 	mac_srs_worker_bind(mac_rx_srs, srs_cpu->mc_rx_workerid);
188611878SVenu.Iyer@Sun.COM 	mac_srs_poll_bind(mac_rx_srs, srs_cpu->mc_rx_pollid);
188711878SVenu.Iyer@Sun.COM 	mac_rx_srs_retarget_intr(mac_rx_srs, srs_cpu->mc_rx_intr_cpu);
18888275SEric Cheng 	/*
18898275SEric Cheng 	 * Bind Tx srs and soft ring threads too. Let's bind tx
18908275SEric Cheng 	 * srs to the last cpu in mrp list.
18918275SEric Cheng 	 */
189211878SVenu.Iyer@Sun.COM 	if (mac_tx_srs != NULL) {
18938275SEric Cheng 		BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp);
189411878SVenu.Iyer@Sun.COM 		mac_tx_srs_retarget_intr(mac_tx_srs);
18958275SEric Cheng 	}
18968275SEric Cheng 	mutex_exit(&cpu_lock);
18978275SEric Cheng }
18988275SEric Cheng 
18998275SEric Cheng /*
19008275SEric Cheng  * Bind SRS threads and soft rings to CPUs/create fanout list.
19018275SEric Cheng  */
19028275SEric Cheng void
mac_srs_fanout_init(mac_client_impl_t * mcip,mac_resource_props_t * mrp,mac_direct_rx_t rx_func,void * x_arg1,mac_resource_handle_t x_arg2,mac_soft_ring_set_t * mac_rx_srs,mac_soft_ring_set_t * mac_tx_srs,cpupart_t * cpupart)190311878SVenu.Iyer@Sun.COM mac_srs_fanout_init(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
190411878SVenu.Iyer@Sun.COM     mac_direct_rx_t rx_func, void *x_arg1, mac_resource_handle_t x_arg2,
190511878SVenu.Iyer@Sun.COM     mac_soft_ring_set_t *mac_rx_srs, mac_soft_ring_set_t *mac_tx_srs,
190611878SVenu.Iyer@Sun.COM     cpupart_t *cpupart)
19078275SEric Cheng {
19088275SEric Cheng 	int		i;
190911878SVenu.Iyer@Sun.COM 	processorid_t	cpuid;
19108833SVenu.Iyer@Sun.COM 	uint32_t	soft_ring_flag = 0;
19118275SEric Cheng 	int soft_ring_cnt;
19128275SEric Cheng 	mac_cpus_t *srs_cpu = &mac_rx_srs->srs_cpu;
19138275SEric Cheng 
19148275SEric Cheng 	/*
19158275SEric Cheng 	 * Remove the no soft ring flag and we will adjust it
19168275SEric Cheng 	 * appropriately further down.
19178275SEric Cheng 	 */
19188275SEric Cheng 	mutex_enter(&mac_rx_srs->srs_lock);
19198275SEric Cheng 	mac_rx_srs->srs_type &= ~SRST_NO_SOFT_RINGS;
19208275SEric Cheng 	mutex_exit(&mac_rx_srs->srs_lock);
19218275SEric Cheng 
19228275SEric Cheng 	ASSERT(mac_rx_srs->srs_soft_ring_head == NULL);
19238275SEric Cheng 
19248275SEric Cheng 	if (mac_rx_srs->srs_type & SRST_BW_CONTROL)
19258275SEric Cheng 		soft_ring_flag |= ST_RING_BW_CTL;
19268275SEric Cheng 
19278275SEric Cheng 	ASSERT(mac_rx_srs->srs_fanout_state == SRS_FANOUT_UNINIT);
19288275SEric Cheng 	mac_rx_srs->srs_fanout_state = SRS_FANOUT_INIT;
19298275SEric Cheng 	/*
19308275SEric Cheng 	 * Ring count can be 0 if no fanout is required and no cpu
19318275SEric Cheng 	 * were specified. Leave the SRS worker and poll thread
19328275SEric Cheng 	 * unbound
19338275SEric Cheng 	 */
19348275SEric Cheng 	ASSERT(mrp != NULL);
193511878SVenu.Iyer@Sun.COM 	soft_ring_cnt = srs_cpu->mc_rx_fanout_cnt;
19368275SEric Cheng 
19378275SEric Cheng 	/* Step 1: bind cpu contains cpu list where threads need to bind */
19388275SEric Cheng 	if (soft_ring_cnt > 0) {
19398275SEric Cheng 		mutex_enter(&cpu_lock);
19408275SEric Cheng 		for (i = 0; i < soft_ring_cnt; i++) {
194111878SVenu.Iyer@Sun.COM 			cpuid = srs_cpu->mc_rx_fanout_cpus[i];
19428275SEric Cheng 			/* Create the protocol softrings */
194311878SVenu.Iyer@Sun.COM 			mac_srs_create_proto_softrings(i, soft_ring_flag,
194411878SVenu.Iyer@Sun.COM 			    mac_rx_srs->srs_pri, mcip, mac_rx_srs, cpuid,
194511878SVenu.Iyer@Sun.COM 			    rx_func, x_arg1, x_arg2, B_FALSE);
19468275SEric Cheng 		}
194711878SVenu.Iyer@Sun.COM 		mac_srs_worker_bind(mac_rx_srs, srs_cpu->mc_rx_workerid);
194811878SVenu.Iyer@Sun.COM 		mac_srs_poll_bind(mac_rx_srs, srs_cpu->mc_rx_pollid);
194911878SVenu.Iyer@Sun.COM 		mac_rx_srs_retarget_intr(mac_rx_srs, srs_cpu->mc_rx_intr_cpu);
19508275SEric Cheng 		/*
19518275SEric Cheng 		 * Bind Tx srs and soft ring threads too.
19528275SEric Cheng 		 * Let's bind tx srs to the last cpu in
19538275SEric Cheng 		 * mrp list.
19548275SEric Cheng 		 */
19558275SEric Cheng 		if (mac_tx_srs == NULL) {
19568275SEric Cheng 			mutex_exit(&cpu_lock);
19578275SEric Cheng 			goto alldone;
19588275SEric Cheng 		}
19598275SEric Cheng 
196011878SVenu.Iyer@Sun.COM 		BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp);
196111878SVenu.Iyer@Sun.COM 		mac_tx_srs_retarget_intr(mac_tx_srs);
19628275SEric Cheng 		mutex_exit(&cpu_lock);
19638275SEric Cheng 	} else {
19648275SEric Cheng 		mutex_enter(&cpu_lock);
19658275SEric Cheng 		/*
19668275SEric Cheng 		 * For a subflow, mrp_workerid and mrp_pollid
19678275SEric Cheng 		 * is not set.
19688275SEric Cheng 		 */
196911878SVenu.Iyer@Sun.COM 		mac_srs_worker_bind(mac_rx_srs, mrp->mrp_rx_workerid);
197011878SVenu.Iyer@Sun.COM 		mac_srs_poll_bind(mac_rx_srs, mrp->mrp_rx_pollid);
19718275SEric Cheng 		mutex_exit(&cpu_lock);
19728275SEric Cheng 		goto no_softrings;
19738275SEric Cheng 	}
19748275SEric Cheng 
19758275SEric Cheng alldone:
19768275SEric Cheng 	if (soft_ring_cnt > 1)
19778275SEric Cheng 		mac_rx_srs->srs_type |= SRST_FANOUT_SRC_IP;
19788275SEric Cheng 	mac_srs_update_fanout_list(mac_rx_srs);
19798275SEric Cheng 	mac_srs_client_poll_enable(mcip, mac_rx_srs);
19808275SEric Cheng 	return;
19818275SEric Cheng 
19828275SEric Cheng no_softrings:
19838275SEric Cheng 	if (mac_rx_srs->srs_type & SRST_FANOUT_PROTO) {
19848275SEric Cheng 		mutex_enter(&cpu_lock);
198511878SVenu.Iyer@Sun.COM 		cpuid = mac_next_bind_cpu(cpupart);
19868275SEric Cheng 		/* Create the protocol softrings */
198711878SVenu.Iyer@Sun.COM 		mac_srs_create_proto_softrings(0, soft_ring_flag,
198811878SVenu.Iyer@Sun.COM 		    mac_rx_srs->srs_pri, mcip, mac_rx_srs, cpuid,
198911878SVenu.Iyer@Sun.COM 		    rx_func, x_arg1, x_arg2, B_FALSE);
19908275SEric Cheng 		mutex_exit(&cpu_lock);
19918275SEric Cheng 	} else {
19928275SEric Cheng 		/*
19938275SEric Cheng 		 * This is the case when there is no fanout which is
19948275SEric Cheng 		 * true for subflows.
19958275SEric Cheng 		 */
19968275SEric Cheng 		mac_rx_srs->srs_type |= SRST_NO_SOFT_RINGS;
19978275SEric Cheng 	}
19988275SEric Cheng 	mac_srs_update_fanout_list(mac_rx_srs);
19998275SEric Cheng 	mac_srs_client_poll_enable(mcip, mac_rx_srs);
20008275SEric Cheng }
20018275SEric Cheng 
20028275SEric Cheng /*
20038275SEric Cheng  * mac_fanout_setup:
20048275SEric Cheng  *
20058275SEric Cheng  * Calls mac_srs_fanout_init() or modify() depending upon whether
20068275SEric Cheng  * the SRS is getting initialized or re-initialized.
20078275SEric Cheng  */
20088275SEric Cheng void
mac_fanout_setup(mac_client_impl_t * mcip,flow_entry_t * flent,mac_resource_props_t * mrp,mac_direct_rx_t rx_func,void * x_arg1,mac_resource_handle_t x_arg2,cpupart_t * cpupart)20098275SEric Cheng mac_fanout_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
20108275SEric Cheng     mac_resource_props_t *mrp, mac_direct_rx_t rx_func, void *x_arg1,
201111878SVenu.Iyer@Sun.COM     mac_resource_handle_t x_arg2, cpupart_t *cpupart)
20128275SEric Cheng {
20138275SEric Cheng 	mac_soft_ring_set_t *mac_rx_srs, *mac_tx_srs;
20148275SEric Cheng 	int i, rx_srs_cnt;
20158275SEric Cheng 
20168275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
20178275SEric Cheng 	/*
20188275SEric Cheng 	 * This is an aggregation port. Fanout will be setup
20198275SEric Cheng 	 * over the aggregation itself.
20208275SEric Cheng 	 */
202111878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE)
20228275SEric Cheng 		return;
20238275SEric Cheng 
20248275SEric Cheng 	mac_rx_srs = flent->fe_rx_srs[0];
20258275SEric Cheng 	/*
20268275SEric Cheng 	 * Set up the fanout on the tx side only once, with the
20278275SEric Cheng 	 * first rx SRS. The CPU binding, fanout, and bandwidth
20288275SEric Cheng 	 * criteria are common to both RX and TX, so
20298275SEric Cheng 	 * initializing them along side avoids redundant code.
20308275SEric Cheng 	 */
20318275SEric Cheng 	mac_tx_srs = flent->fe_tx_srs;
20328275SEric Cheng 	rx_srs_cnt = flent->fe_rx_srs_cnt;
20338275SEric Cheng 
20348275SEric Cheng 	/* No fanout for subflows */
20358275SEric Cheng 	if (flent->fe_type & FLOW_USER) {
203611878SVenu.Iyer@Sun.COM 		mac_srs_fanout_init(mcip, mrp, rx_func,
203711878SVenu.Iyer@Sun.COM 		    x_arg1, x_arg2, mac_rx_srs, mac_tx_srs,
203811878SVenu.Iyer@Sun.COM 		    cpupart);
20398275SEric Cheng 		return;
20408275SEric Cheng 	}
20418275SEric Cheng 
204211878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_CPUS_USERSPEC)
204311878SVenu.Iyer@Sun.COM 		mac_flow_user_cpu_init(flent, mrp);
204411878SVenu.Iyer@Sun.COM 	else
204511878SVenu.Iyer@Sun.COM 		mac_flow_cpu_init(flent, cpupart);
204611878SVenu.Iyer@Sun.COM 
204711878SVenu.Iyer@Sun.COM 	mrp->mrp_rx_fanout_cnt = mac_rx_srs->srs_cpu.mc_rx_fanout_cnt;
20488275SEric Cheng 
20498275SEric Cheng 	/*
20508275SEric Cheng 	 * Set up fanout for both SW (0th SRS) and HW classified
20518275SEric Cheng 	 * SRS (the rest of Rx SRSs in flent).
20528275SEric Cheng 	 */
20538275SEric Cheng 	for (i = 0; i < rx_srs_cnt; i++) {
20548275SEric Cheng 		mac_rx_srs = flent->fe_rx_srs[i];
20558275SEric Cheng 		if (i != 0)
20568275SEric Cheng 			mac_tx_srs = NULL;
20578275SEric Cheng 		switch (mac_rx_srs->srs_fanout_state) {
20588275SEric Cheng 		case SRS_FANOUT_UNINIT:
205911878SVenu.Iyer@Sun.COM 			mac_srs_fanout_init(mcip, mrp, rx_func,
206011878SVenu.Iyer@Sun.COM 			    x_arg1, x_arg2, mac_rx_srs, mac_tx_srs,
206111878SVenu.Iyer@Sun.COM 			    cpupart);
20628275SEric Cheng 			break;
20638275SEric Cheng 		case SRS_FANOUT_INIT:
20648275SEric Cheng 			break;
20658275SEric Cheng 		case SRS_FANOUT_REINIT:
20668275SEric Cheng 			mac_rx_srs_quiesce(mac_rx_srs, SRS_QUIESCE);
206711878SVenu.Iyer@Sun.COM 			mac_srs_fanout_modify(mcip, rx_func, x_arg1,
206811878SVenu.Iyer@Sun.COM 			    x_arg2, mac_rx_srs, mac_tx_srs);
20698275SEric Cheng 			mac_rx_srs_restart(mac_rx_srs);
20708275SEric Cheng 			break;
20718275SEric Cheng 		default:
20728275SEric Cheng 			VERIFY(mac_rx_srs->srs_fanout_state <=
20738275SEric Cheng 			    SRS_FANOUT_REINIT);
20748275SEric Cheng 			break;
20758275SEric Cheng 		}
20768275SEric Cheng 	}
20778275SEric Cheng }
20788275SEric Cheng 
20798275SEric Cheng /*
208011878SVenu.Iyer@Sun.COM  * mac_srs_create:
20818275SEric Cheng  *
20828275SEric Cheng  * Create a mac_soft_ring_set_t (SRS). If soft_ring_fanout_type is
20838275SEric Cheng  * SRST_TX, an SRS for Tx side is created. Otherwise an SRS for Rx side
20848275SEric Cheng  * processing is created.
20858275SEric Cheng  *
20868275SEric Cheng  * Details on Rx SRS:
20878275SEric Cheng  * Create a SRS and also add the necessary soft rings for TCP and
20888275SEric Cheng  * non-TCP based on fanout type and count specified.
20898275SEric Cheng  *
20908275SEric Cheng  * mac_soft_ring_fanout, mac_srs_fanout_modify (?),
20918275SEric Cheng  * mac_soft_ring_stop_workers, mac_soft_ring_set_destroy, etc need
20928275SEric Cheng  * to be heavily modified.
20938275SEric Cheng  *
20948275SEric Cheng  * mi_soft_ring_list_size, mi_soft_ring_size, etc need to disappear.
20958275SEric Cheng  */
20968275SEric Cheng mac_soft_ring_set_t *
mac_srs_create(mac_client_impl_t * mcip,flow_entry_t * flent,uint32_t srs_type,mac_direct_rx_t rx_func,void * x_arg1,mac_resource_handle_t x_arg2,mac_ring_t * ring)20978275SEric Cheng mac_srs_create(mac_client_impl_t *mcip, flow_entry_t *flent, uint32_t srs_type,
20988275SEric Cheng     mac_direct_rx_t rx_func, void *x_arg1, mac_resource_handle_t x_arg2,
20998275SEric Cheng     mac_ring_t *ring)
21008275SEric Cheng {
21018275SEric Cheng 	mac_soft_ring_set_t 	*mac_srs;
21028275SEric Cheng 	mac_srs_rx_t		*srs_rx;
21038275SEric Cheng 	mac_srs_tx_t		*srs_tx;
21048275SEric Cheng 	mac_bw_ctl_t		*mac_bw;
21058275SEric Cheng 	mac_resource_props_t	*mrp;
21068275SEric Cheng 	boolean_t		is_tx_srs = ((srs_type & SRST_TX) != 0);
21078275SEric Cheng 
21088275SEric Cheng 	mac_srs = kmem_cache_alloc(mac_srs_cache, KM_SLEEP);
21098275SEric Cheng 	bzero(mac_srs, sizeof (mac_soft_ring_set_t));
21108275SEric Cheng 	srs_rx = &mac_srs->srs_rx;
21118275SEric Cheng 	srs_tx = &mac_srs->srs_tx;
21128275SEric Cheng 
21138275SEric Cheng 	mutex_enter(&flent->fe_lock);
21148275SEric Cheng 
21158275SEric Cheng 	/*
21168275SEric Cheng 	 * Get the bandwidth control structure from the flent. Get
21178275SEric Cheng 	 * rid of any residual values in the control structure for
21188275SEric Cheng 	 * the tx bw struct and also for the rx, if the rx srs is
21198275SEric Cheng 	 * the 1st one being brought up (the rx bw ctl struct may
21208275SEric Cheng 	 * be shared by multiple SRSs)
21218275SEric Cheng 	 */
21228275SEric Cheng 	if (is_tx_srs) {
21238275SEric Cheng 		mac_srs->srs_bw = &flent->fe_tx_bw;
21248275SEric Cheng 		bzero(mac_srs->srs_bw, sizeof (mac_bw_ctl_t));
21258275SEric Cheng 		flent->fe_tx_srs = mac_srs;
21268275SEric Cheng 	} else {
21278275SEric Cheng 		/*
21288275SEric Cheng 		 * The bw counter (stored in the flent) is shared
21298275SEric Cheng 		 * by SRS's within an rx group.
21308275SEric Cheng 		 */
21318275SEric Cheng 		mac_srs->srs_bw = &flent->fe_rx_bw;
21328275SEric Cheng 		/* First rx SRS, clear the bw structure */
21338275SEric Cheng 		if (flent->fe_rx_srs_cnt == 0)
21348275SEric Cheng 			bzero(mac_srs->srs_bw, sizeof (mac_bw_ctl_t));
21359700SVenu.Iyer@Sun.COM 
21369700SVenu.Iyer@Sun.COM 		/*
21379700SVenu.Iyer@Sun.COM 		 * It is better to panic here rather than just assert because
21389700SVenu.Iyer@Sun.COM 		 * on a non-debug kernel we might end up courrupting memory
21399700SVenu.Iyer@Sun.COM 		 * and making it difficult to debug.
21409700SVenu.Iyer@Sun.COM 		 */
21419700SVenu.Iyer@Sun.COM 		if (flent->fe_rx_srs_cnt >= MAX_RINGS_PER_GROUP) {
21429700SVenu.Iyer@Sun.COM 			panic("Array Overrun detected due to MAC client %p "
21439700SVenu.Iyer@Sun.COM 			    " having more rings than %d", (void *)mcip,
21449700SVenu.Iyer@Sun.COM 			    MAX_RINGS_PER_GROUP);
21459700SVenu.Iyer@Sun.COM 		}
21468275SEric Cheng 		flent->fe_rx_srs[flent->fe_rx_srs_cnt] = mac_srs;
21478275SEric Cheng 		flent->fe_rx_srs_cnt++;
21488275SEric Cheng 	}
21498275SEric Cheng 	mac_srs->srs_flent = flent;
21508275SEric Cheng 	mutex_exit(&flent->fe_lock);
21518275SEric Cheng 
21528275SEric Cheng 	mac_srs->srs_state = 0;
21538275SEric Cheng 	mac_srs->srs_type = (srs_type | SRST_NO_SOFT_RINGS);
21548275SEric Cheng 	mac_srs->srs_worker_cpuid = mac_srs->srs_worker_cpuid_save = -1;
21558275SEric Cheng 	mac_srs->srs_poll_cpuid = mac_srs->srs_poll_cpuid_save = -1;
215611878SVenu.Iyer@Sun.COM 	mac_srs->srs_mcip = mcip;
21578275SEric Cheng 	mac_srs_fanout_list_alloc(mac_srs);
21588275SEric Cheng 
21598275SEric Cheng 	/*
21608275SEric Cheng 	 * For a flow we use the underlying MAC client's priority range with
21618275SEric Cheng 	 * the priority value to find an absolute priority value. For a MAC
21628275SEric Cheng 	 * client we use the MAC client's maximum priority as the value.
21638275SEric Cheng 	 */
21648275SEric Cheng 	mrp = &flent->fe_effective_props;
21658275SEric Cheng 	if ((mac_srs->srs_type & SRST_FLOW) != 0) {
21668275SEric Cheng 		mac_srs->srs_pri = FLOW_PRIORITY(mcip->mci_min_pri,
21678275SEric Cheng 		    mcip->mci_max_pri, mrp->mrp_priority);
21688275SEric Cheng 	} else {
21698275SEric Cheng 		mac_srs->srs_pri = mcip->mci_max_pri;
21708275SEric Cheng 	}
21718275SEric Cheng 	/*
21728275SEric Cheng 	 * We need to insert the SRS in the global list before
21738275SEric Cheng 	 * binding the SRS and SR threads. Otherwise there is a
21748275SEric Cheng 	 * is a small window where the cpu reconfig callbacks
21758275SEric Cheng 	 * may miss the SRS in the list walk and DR could fail
21768275SEric Cheng 	 * as there are bound threads.
21778275SEric Cheng 	 */
21788275SEric Cheng 	mac_srs_add_glist(mac_srs);
21798275SEric Cheng 
21808275SEric Cheng 	/* Initialize bw limit */
21818275SEric Cheng 	if ((mrp->mrp_mask & MRP_MAXBW) != 0) {
21828275SEric Cheng 		mac_srs->srs_drain_func = mac_rx_srs_drain_bw;
21838275SEric Cheng 
21848275SEric Cheng 		mac_bw = mac_srs->srs_bw;
21858275SEric Cheng 		mutex_enter(&mac_bw->mac_bw_lock);
21868275SEric Cheng 		mac_bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
21878275SEric Cheng 
21888275SEric Cheng 		/*
21898275SEric Cheng 		 * Give twice the queuing capability before
21908275SEric Cheng 		 * dropping packets. The unit is bytes/tick.
21918275SEric Cheng 		 */
21928275SEric Cheng 		mac_bw->mac_bw_drop_threshold = mac_bw->mac_bw_limit << 1;
21938275SEric Cheng 		mutex_exit(&mac_bw->mac_bw_lock);
21948275SEric Cheng 		mac_srs->srs_type |= SRST_BW_CONTROL;
21958275SEric Cheng 	} else {
21968275SEric Cheng 		mac_srs->srs_drain_func = mac_rx_srs_drain;
21978275SEric Cheng 	}
21988275SEric Cheng 
21998275SEric Cheng 	/*
22008275SEric Cheng 	 * We use the following policy to control Receive
22018275SEric Cheng 	 * Side Dynamic Polling:
22028275SEric Cheng 	 * 1) We switch to poll mode anytime the processing thread causes
22038275SEric Cheng 	 *    a backlog to build up in SRS and its associated Soft Rings
22048275SEric Cheng 	 *    (sr_poll_pkt_cnt > 0).
22058275SEric Cheng 	 * 2) As long as the backlog stays under the low water mark
22068275SEric Cheng 	 *    (sr_lowat), we poll the H/W for more packets.
22078275SEric Cheng 	 * 3) If the backlog (sr_poll_pkt_cnt) exceeds low water mark, we
22088275SEric Cheng 	 *    stay in poll mode but don't poll the H/W for more packets.
22098275SEric Cheng 	 * 4) Anytime in polling mode, if we poll the H/W for packets and
22108275SEric Cheng 	 *    find nothing plus we have an existing backlog
22118275SEric Cheng 	 *    (sr_poll_pkt_cnt > 0), we stay in polling mode but don't poll
22128275SEric Cheng 	 *    the H/W for packets anymore (let the polling thread go to sleep).
22138275SEric Cheng 	 * 5) Once the backlog is relived (packets are processed) we reenable
22148275SEric Cheng 	 *    polling (by signalling the poll thread) only when the backlog
22158275SEric Cheng 	 *    dips below sr_poll_thres.
22168275SEric Cheng 	 * 6) sr_hiwat is used exclusively when we are not polling capable
22178275SEric Cheng 	 *    and is used to decide when to drop packets so the SRS queue
22188275SEric Cheng 	 *    length doesn't grow infinitely.
22198275SEric Cheng 	 */
22208275SEric Cheng 	if (!is_tx_srs) {
22218275SEric Cheng 		srs_rx->sr_hiwat = mac_soft_ring_max_q_cnt;
22228275SEric Cheng 		/* Low water mark needs to be less than high water mark */
22238275SEric Cheng 		srs_rx->sr_lowat = mac_soft_ring_min_q_cnt <=
22248275SEric Cheng 		    mac_soft_ring_max_q_cnt ? mac_soft_ring_min_q_cnt :
22258275SEric Cheng 		    (mac_soft_ring_max_q_cnt >> 2);
22268275SEric Cheng 		/* Poll threshold need to be half of low water mark or less */
22278275SEric Cheng 		srs_rx->sr_poll_thres = mac_soft_ring_poll_thres <=
22288275SEric Cheng 		    (srs_rx->sr_lowat >> 1) ? mac_soft_ring_poll_thres :
22298275SEric Cheng 		    (srs_rx->sr_lowat >> 1);
22308275SEric Cheng 		if (mac_latency_optimize)
22319618SRajagopal.Kunhappan@Sun.COM 			mac_srs->srs_state |= SRS_LATENCY_OPT;
22329618SRajagopal.Kunhappan@Sun.COM 		else
22339618SRajagopal.Kunhappan@Sun.COM 			mac_srs->srs_state |= SRS_SOFTRING_QUEUE;
22348275SEric Cheng 	}
22358275SEric Cheng 
22368275SEric Cheng 	mac_srs->srs_worker = thread_create(NULL, 0,
22378275SEric Cheng 	    mac_srs_worker, mac_srs, 0, &p0, TS_RUN, mac_srs->srs_pri);
22388275SEric Cheng 
22398275SEric Cheng 	if (is_tx_srs) {
22408275SEric Cheng 		/* Handle everything about Tx SRS and return */
22418275SEric Cheng 		mac_srs->srs_drain_func = mac_tx_srs_drain;
22428275SEric Cheng 		srs_tx->st_max_q_cnt = mac_tx_srs_max_q_cnt;
22438275SEric Cheng 		srs_tx->st_hiwat =
22448275SEric Cheng 		    (mac_tx_srs_hiwat > mac_tx_srs_max_q_cnt) ?
22458275SEric Cheng 		    mac_tx_srs_max_q_cnt : mac_tx_srs_hiwat;
22468275SEric Cheng 		srs_tx->st_arg1 = x_arg1;
22478275SEric Cheng 		srs_tx->st_arg2 = x_arg2;
224811878SVenu.Iyer@Sun.COM 		goto done;
22498275SEric Cheng 	}
22508275SEric Cheng 
22518275SEric Cheng 	if ((srs_type & SRST_FLOW) != 0 ||
22528275SEric Cheng 	    FLOW_TAB_EMPTY(mcip->mci_subflow_tab))
22538275SEric Cheng 		srs_rx->sr_lower_proc = mac_rx_srs_process;
22548275SEric Cheng 	else
22558275SEric Cheng 		srs_rx->sr_lower_proc = mac_rx_srs_subflow_process;
22568275SEric Cheng 
22578275SEric Cheng 	srs_rx->sr_func = rx_func;
22588275SEric Cheng 	srs_rx->sr_arg1 = x_arg1;
22598275SEric Cheng 	srs_rx->sr_arg2 = x_arg2;
22608275SEric Cheng 
22618275SEric Cheng 	if (ring != NULL) {
226211878SVenu.Iyer@Sun.COM 		uint_t ring_info;
226311878SVenu.Iyer@Sun.COM 
22648275SEric Cheng 		/* Is the mac_srs created over the RX default group? */
22658275SEric Cheng 		if (ring->mr_gh == (mac_group_handle_t)
226611878SVenu.Iyer@Sun.COM 		    MAC_DEFAULT_RX_GROUP(mcip->mci_mip)) {
22678275SEric Cheng 			mac_srs->srs_type |= SRST_DEFAULT_GRP;
226811878SVenu.Iyer@Sun.COM 		}
22698275SEric Cheng 		mac_srs->srs_ring = ring;
22708275SEric Cheng 		ring->mr_srs = mac_srs;
22718275SEric Cheng 		ring->mr_classify_type = MAC_HW_CLASSIFIER;
22728275SEric Cheng 		ring->mr_flag |= MR_INCIPIENT;
22738275SEric Cheng 
227410491SRishi.Srivatsavai@Sun.COM 		if (!(mcip->mci_mip->mi_state_flags & MIS_POLL_DISABLE) &&
227510491SRishi.Srivatsavai@Sun.COM 		    FLOW_TAB_EMPTY(mcip->mci_subflow_tab) && mac_poll_enable)
22768275SEric Cheng 			mac_srs->srs_state |= SRS_POLLING_CAPAB;
22778275SEric Cheng 
22788275SEric Cheng 		mac_srs->srs_poll_thr = thread_create(NULL, 0,
22798275SEric Cheng 		    mac_rx_srs_poll_ring, mac_srs, 0, &p0, TS_RUN,
22808275SEric Cheng 		    mac_srs->srs_pri);
22818833SVenu.Iyer@Sun.COM 		/*
22828833SVenu.Iyer@Sun.COM 		 * Some drivers require serialization and don't send
22838833SVenu.Iyer@Sun.COM 		 * packet chains in interrupt context. For such
22848833SVenu.Iyer@Sun.COM 		 * drivers, we should always queue in soft ring
22858833SVenu.Iyer@Sun.COM 		 * so that we get a chance to switch into a polling
22868833SVenu.Iyer@Sun.COM 		 * mode under backlog.
22878833SVenu.Iyer@Sun.COM 		 */
228811878SVenu.Iyer@Sun.COM 		ring_info = mac_hwring_getinfo((mac_ring_handle_t)ring);
228911878SVenu.Iyer@Sun.COM 		if (ring_info & MAC_RING_RX_ENQUEUE)
22908833SVenu.Iyer@Sun.COM 			mac_srs->srs_state |= SRS_SOFTRING_QUEUE;
22918275SEric Cheng 	}
229211878SVenu.Iyer@Sun.COM done:
229311878SVenu.Iyer@Sun.COM 	mac_srs_stat_create(mac_srs);
22948275SEric Cheng 	return (mac_srs);
22958275SEric Cheng }
22968275SEric Cheng 
22978275SEric Cheng /*
22988275SEric Cheng  * Figure out the number of soft rings required. Its dependant on
22998275SEric Cheng  * if protocol fanout is required (for LINKs), global settings
23008275SEric Cheng  * require us to do fanout for performance (based on mac_soft_ring_enable),
23018275SEric Cheng  * or user has specifically requested fanout.
23028275SEric Cheng  */
23038275SEric Cheng static uint32_t
mac_find_fanout(flow_entry_t * flent,uint32_t link_type)23048275SEric Cheng mac_find_fanout(flow_entry_t *flent, uint32_t link_type)
23058275SEric Cheng {
23068275SEric Cheng 	uint32_t			fanout_type;
23078275SEric Cheng 	mac_resource_props_t		*mrp = &flent->fe_effective_props;
23088275SEric Cheng 
23098275SEric Cheng 	/* no fanout for subflows */
23108275SEric Cheng 	switch (link_type) {
23118275SEric Cheng 	case SRST_FLOW:
23128275SEric Cheng 		fanout_type = SRST_NO_SOFT_RINGS;
23138275SEric Cheng 		break;
23148275SEric Cheng 	case SRST_LINK:
23158275SEric Cheng 		fanout_type = SRST_FANOUT_PROTO;
23168275SEric Cheng 		break;
23178275SEric Cheng 	}
23188275SEric Cheng 
23198275SEric Cheng 	/* A primary NIC/link is being plumbed */
23208275SEric Cheng 	if (flent->fe_type & FLOW_PRIMARY_MAC) {
23218275SEric Cheng 		if (mac_soft_ring_enable && mac_rx_soft_ring_count > 1) {
23228275SEric Cheng 			fanout_type |= SRST_FANOUT_SRC_IP;
23238275SEric Cheng 		}
23248275SEric Cheng 	} else if (flent->fe_type & FLOW_VNIC) {
23258275SEric Cheng 		/* A VNIC is being created */
23268275SEric Cheng 		if (mrp != NULL && mrp->mrp_ncpus > 0) {
23278275SEric Cheng 			fanout_type |= SRST_FANOUT_SRC_IP;
23288275SEric Cheng 		}
23298275SEric Cheng 	}
23308275SEric Cheng 
23318275SEric Cheng 	return (fanout_type);
23328275SEric Cheng }
23338275SEric Cheng 
23348275SEric Cheng /*
23358275SEric Cheng  * Change a group from h/w to s/w classification.
23368275SEric Cheng  */
233711878SVenu.Iyer@Sun.COM void
mac_rx_switch_grp_to_sw(mac_group_t * group)23388275SEric Cheng mac_rx_switch_grp_to_sw(mac_group_t *group)
23398275SEric Cheng {
23408275SEric Cheng 	mac_ring_t		*ring;
23418275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
23428275SEric Cheng 
23438275SEric Cheng 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) {
23448275SEric Cheng 		if (ring->mr_classify_type == MAC_HW_CLASSIFIER) {
23458275SEric Cheng 			/*
23468275SEric Cheng 			 * Remove the SRS associated with the HW ring.
23478275SEric Cheng 			 * As a result, polling will be disabled.
23488275SEric Cheng 			 */
23498275SEric Cheng 			mac_srs = ring->mr_srs;
23508275SEric Cheng 			ASSERT(mac_srs != NULL);
23518275SEric Cheng 			mac_rx_srs_remove(mac_srs);
23528275SEric Cheng 			ring->mr_srs = NULL;
23538275SEric Cheng 		}
23548275SEric Cheng 
23558275SEric Cheng 		if (ring->mr_state != MR_INUSE)
23568275SEric Cheng 			(void) mac_start_ring(ring);
235711878SVenu.Iyer@Sun.COM 
23588275SEric Cheng 		/*
23598275SEric Cheng 		 * We need to perform SW classification
23608275SEric Cheng 		 * for packets landing in these rings
23618275SEric Cheng 		 */
23628275SEric Cheng 		ring->mr_flag = 0;
23638275SEric Cheng 		ring->mr_classify_type = MAC_SW_CLASSIFIER;
23648275SEric Cheng 	}
23658275SEric Cheng }
23668275SEric Cheng 
23678275SEric Cheng /*
23688275SEric Cheng  * Create the Rx SRS for S/W classifier and for each ring in the
23698275SEric Cheng  * group (if exclusive group). Also create the Tx SRS.
23708275SEric Cheng  */
23718275SEric Cheng void
mac_srs_group_setup(mac_client_impl_t * mcip,flow_entry_t * flent,uint32_t link_type)23728275SEric Cheng mac_srs_group_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
237311878SVenu.Iyer@Sun.COM     uint32_t link_type)
237411878SVenu.Iyer@Sun.COM {
237511878SVenu.Iyer@Sun.COM 	cpupart_t		*cpupart;
237611878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
237711878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*emrp = MCIP_EFFECTIVE_PROPS(mcip);
237811878SVenu.Iyer@Sun.COM 	boolean_t		use_default = B_FALSE;
237911878SVenu.Iyer@Sun.COM 
238011878SVenu.Iyer@Sun.COM 	mac_rx_srs_group_setup(mcip, flent, link_type);
238111878SVenu.Iyer@Sun.COM 	mac_tx_srs_group_setup(mcip, flent, link_type);
238211878SVenu.Iyer@Sun.COM 
238311878SVenu.Iyer@Sun.COM 	pool_lock();
238411878SVenu.Iyer@Sun.COM 	cpupart = mac_pset_find(mrp, &use_default);
238511878SVenu.Iyer@Sun.COM 	mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip),
238611878SVenu.Iyer@Sun.COM 	    mac_rx_deliver, mcip, NULL, cpupart);
238711878SVenu.Iyer@Sun.COM 	mac_set_pool_effective(use_default, cpupart, mrp, emrp);
238811878SVenu.Iyer@Sun.COM 	pool_unlock();
238911878SVenu.Iyer@Sun.COM }
239011878SVenu.Iyer@Sun.COM 
239111878SVenu.Iyer@Sun.COM /*
239211878SVenu.Iyer@Sun.COM  * Set up the RX SRSs. If the S/W SRS is not set, set  it up, if there
239311878SVenu.Iyer@Sun.COM  * is a group associated with this MAC client, set up SRSs for individual
239411878SVenu.Iyer@Sun.COM  * h/w rings.
239511878SVenu.Iyer@Sun.COM  */
239611878SVenu.Iyer@Sun.COM void
mac_rx_srs_group_setup(mac_client_impl_t * mcip,flow_entry_t * flent,uint32_t link_type)239711878SVenu.Iyer@Sun.COM mac_rx_srs_group_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
239811878SVenu.Iyer@Sun.COM     uint32_t link_type)
23998275SEric Cheng {
24008275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
24018275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
24028275SEric Cheng 	mac_ring_t 		*ring;
24038275SEric Cheng 	uint32_t		fanout_type;
240411878SVenu.Iyer@Sun.COM 	mac_group_t		*rx_group = flent->fe_rx_ring_group;
24058275SEric Cheng 
24068275SEric Cheng 	fanout_type = mac_find_fanout(flent, link_type);
24078275SEric Cheng 
24088275SEric Cheng 	/* Create the SRS for S/W classification if none exists */
24098275SEric Cheng 	if (flent->fe_rx_srs[0] == NULL) {
24108275SEric Cheng 		ASSERT(flent->fe_rx_srs_cnt == 0);
24118275SEric Cheng 		/* Setup the Rx SRS */
24128275SEric Cheng 		mac_srs = mac_srs_create(mcip, flent, fanout_type | link_type,
24138275SEric Cheng 		    mac_rx_deliver, mcip, NULL, NULL);
24148275SEric Cheng 		mutex_enter(&flent->fe_lock);
24158275SEric Cheng 		flent->fe_cb_fn = (flow_fn_t)mac_srs->srs_rx.sr_lower_proc;
24168275SEric Cheng 		flent->fe_cb_arg1 = (void *)mip;
24178275SEric Cheng 		flent->fe_cb_arg2 = (void *)mac_srs;
24188275SEric Cheng 		mutex_exit(&flent->fe_lock);
24198275SEric Cheng 	}
24208275SEric Cheng 
242111878SVenu.Iyer@Sun.COM 	if (rx_group == NULL)
24228275SEric Cheng 		return;
24238275SEric Cheng 	/*
24248275SEric Cheng 	 * fanout for default SRS is done when default SRS are created
24258275SEric Cheng 	 * above. As each ring is added to the group, we setup the
24268275SEric Cheng 	 * SRS and fanout to it.
24278275SEric Cheng 	 */
242811878SVenu.Iyer@Sun.COM 	switch (rx_group->mrg_state) {
24298275SEric Cheng 	case MAC_GROUP_STATE_RESERVED:
243011878SVenu.Iyer@Sun.COM 		for (ring = rx_group->mrg_rings; ring != NULL;
24318275SEric Cheng 		    ring = ring->mr_next) {
24328275SEric Cheng 			switch (ring->mr_state) {
24338275SEric Cheng 			case MR_INUSE:
24348275SEric Cheng 			case MR_FREE:
24358275SEric Cheng 				if (ring->mr_srs != NULL)
24368275SEric Cheng 					break;
24378275SEric Cheng 				if (ring->mr_state != MR_INUSE)
24388275SEric Cheng 					(void) mac_start_ring(ring);
24398275SEric Cheng 
244011878SVenu.Iyer@Sun.COM 				/*
244111878SVenu.Iyer@Sun.COM 				 * Since the group is exclusively ours create
244211878SVenu.Iyer@Sun.COM 				 * an SRS for this ring to allow the
244311878SVenu.Iyer@Sun.COM 				 * individual SRS to dynamically poll the
244411878SVenu.Iyer@Sun.COM 				 * ring. Do this only if the  client is not
244511878SVenu.Iyer@Sun.COM 				 * a VLAN MAC client, since for VLAN we do
244611878SVenu.Iyer@Sun.COM 				 * s/w classification for the VID check, and
244711878SVenu.Iyer@Sun.COM 				 * if it has a unicast address.
244811878SVenu.Iyer@Sun.COM 				 */
244911878SVenu.Iyer@Sun.COM 				if ((mcip->mci_state_flags &
245011878SVenu.Iyer@Sun.COM 				    MCIS_NO_UNICAST_ADDR) ||
245111878SVenu.Iyer@Sun.COM 				    i_mac_flow_vid(mcip->mci_flent) !=
245211878SVenu.Iyer@Sun.COM 				    VLAN_ID_NONE) {
245311878SVenu.Iyer@Sun.COM 					break;
245411878SVenu.Iyer@Sun.COM 				}
24558275SEric Cheng 				mac_srs = mac_srs_create(mcip, flent,
24568275SEric Cheng 				    fanout_type | link_type,
24578275SEric Cheng 				    mac_rx_deliver, mcip, NULL, ring);
24588275SEric Cheng 				break;
24598275SEric Cheng 			default:
246011878SVenu.Iyer@Sun.COM 				cmn_err(CE_PANIC,
246111878SVenu.Iyer@Sun.COM 				    "srs_setup: mcip = %p "
24628275SEric Cheng 				    "trying to add UNKNOWN ring = %p\n",
24638275SEric Cheng 				    (void *)mcip, (void *)ring);
24648275SEric Cheng 				break;
24658275SEric Cheng 			}
24668275SEric Cheng 		}
24678275SEric Cheng 		break;
24688275SEric Cheng 	case MAC_GROUP_STATE_SHARED:
24698275SEric Cheng 		/*
24708275SEric Cheng 		 * Set all rings of this group to software classified.
24718275SEric Cheng 		 *
247211878SVenu.Iyer@Sun.COM 		 * If the group is current RESERVED, the existing mac
247311878SVenu.Iyer@Sun.COM 		 * client (the only client on this group) is using
247411878SVenu.Iyer@Sun.COM 		 * this group exclusively.  In that case we need to
247511878SVenu.Iyer@Sun.COM 		 * disable polling on the rings of the group (if it
247611878SVenu.Iyer@Sun.COM 		 * was enabled), and free the SRS associated with the
247711878SVenu.Iyer@Sun.COM 		 * rings.
24788275SEric Cheng 		 */
247911878SVenu.Iyer@Sun.COM 		mac_rx_switch_grp_to_sw(rx_group);
24808275SEric Cheng 		break;
24818275SEric Cheng 	default:
24828275SEric Cheng 		ASSERT(B_FALSE);
24838275SEric Cheng 		break;
24848275SEric Cheng 	}
24858275SEric Cheng }
24868275SEric Cheng 
248711878SVenu.Iyer@Sun.COM /*
248811878SVenu.Iyer@Sun.COM  * Set up the TX SRS.
248911878SVenu.Iyer@Sun.COM  */
24908275SEric Cheng void
mac_tx_srs_group_setup(mac_client_impl_t * mcip,flow_entry_t * flent,uint32_t link_type)249111878SVenu.Iyer@Sun.COM mac_tx_srs_group_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
24928275SEric Cheng     uint32_t link_type)
24938275SEric Cheng {
249411878SVenu.Iyer@Sun.COM 	int			cnt;
249511878SVenu.Iyer@Sun.COM 	int			ringcnt;
249611878SVenu.Iyer@Sun.COM 	mac_ring_t		*ring;
249711878SVenu.Iyer@Sun.COM 	mac_group_t		*grp;
249811878SVenu.Iyer@Sun.COM 
249911878SVenu.Iyer@Sun.COM 	/*
250011878SVenu.Iyer@Sun.COM 	 * If we are opened exclusively (like aggr does for aggr_ports),
250111878SVenu.Iyer@Sun.COM 	 * don't set up Tx SRS and Tx soft rings as they won't be used.
250211878SVenu.Iyer@Sun.COM 	 * The same thing has to be done for Rx side also. See bug:
250311878SVenu.Iyer@Sun.COM 	 * 6880080
250411878SVenu.Iyer@Sun.COM 	 */
250511878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
250611878SVenu.Iyer@Sun.COM 		/*
250711878SVenu.Iyer@Sun.COM 		 * If we have rings, start them here.
250811878SVenu.Iyer@Sun.COM 		 */
250911878SVenu.Iyer@Sun.COM 		if (flent->fe_tx_ring_group == NULL)
251011878SVenu.Iyer@Sun.COM 			return;
251111878SVenu.Iyer@Sun.COM 		grp = (mac_group_t *)flent->fe_tx_ring_group;
251211878SVenu.Iyer@Sun.COM 		ringcnt = grp->mrg_cur_count;
251311878SVenu.Iyer@Sun.COM 		ring = grp->mrg_rings;
251411878SVenu.Iyer@Sun.COM 		for (cnt = 0; cnt < ringcnt; cnt++) {
251511878SVenu.Iyer@Sun.COM 			if (ring->mr_state != MR_INUSE) {
251611878SVenu.Iyer@Sun.COM 				(void) mac_start_ring(ring);
251711878SVenu.Iyer@Sun.COM 			}
251811878SVenu.Iyer@Sun.COM 			ring = ring->mr_next;
251911878SVenu.Iyer@Sun.COM 		}
252011878SVenu.Iyer@Sun.COM 		return;
252111878SVenu.Iyer@Sun.COM 	}
252211878SVenu.Iyer@Sun.COM 	if (flent->fe_tx_srs == NULL) {
252311878SVenu.Iyer@Sun.COM 		(void) mac_srs_create(mcip, flent, SRST_TX | link_type,
252411878SVenu.Iyer@Sun.COM 		    NULL, mcip, NULL, NULL);
252511878SVenu.Iyer@Sun.COM 	}
252611878SVenu.Iyer@Sun.COM 	mac_tx_srs_setup(mcip, flent);
252711878SVenu.Iyer@Sun.COM }
252811878SVenu.Iyer@Sun.COM 
252911878SVenu.Iyer@Sun.COM /*
253011878SVenu.Iyer@Sun.COM  * Remove all the RX SRSs. If we want to remove only the SRSs associated
253111878SVenu.Iyer@Sun.COM  * with h/w rings, leave the S/W SRS alone. This is used when we want to
253211878SVenu.Iyer@Sun.COM  * move the MAC client from one group to another, so we need to teardown
253311878SVenu.Iyer@Sun.COM  * on the h/w SRSs.
253411878SVenu.Iyer@Sun.COM  */
253511878SVenu.Iyer@Sun.COM void
mac_rx_srs_group_teardown(flow_entry_t * flent,boolean_t hwonly)253611878SVenu.Iyer@Sun.COM mac_rx_srs_group_teardown(flow_entry_t *flent, boolean_t hwonly)
253711878SVenu.Iyer@Sun.COM {
25388275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
253911878SVenu.Iyer@Sun.COM 	int			i;
254011878SVenu.Iyer@Sun.COM 	int			count = flent->fe_rx_srs_cnt;
254111878SVenu.Iyer@Sun.COM 
254211878SVenu.Iyer@Sun.COM 	for (i = 0; i < count; i++) {
254311878SVenu.Iyer@Sun.COM 		if (i == 0 && hwonly)
254411878SVenu.Iyer@Sun.COM 			continue;
254511878SVenu.Iyer@Sun.COM 		mac_srs = flent->fe_rx_srs[i];
254611878SVenu.Iyer@Sun.COM 		mac_rx_srs_quiesce(mac_srs, SRS_CONDEMNED);
254711878SVenu.Iyer@Sun.COM 		mac_srs_free(mac_srs);
254811878SVenu.Iyer@Sun.COM 		flent->fe_rx_srs[i] = NULL;
254911878SVenu.Iyer@Sun.COM 		flent->fe_rx_srs_cnt--;
255011878SVenu.Iyer@Sun.COM 	}
255111878SVenu.Iyer@Sun.COM 	ASSERT(!hwonly || flent->fe_rx_srs_cnt == 1);
255211878SVenu.Iyer@Sun.COM 	ASSERT(hwonly || flent->fe_rx_srs_cnt == 0);
255311878SVenu.Iyer@Sun.COM }
255411878SVenu.Iyer@Sun.COM 
255511878SVenu.Iyer@Sun.COM /*
255611878SVenu.Iyer@Sun.COM  * Remove the TX SRS.
255711878SVenu.Iyer@Sun.COM  */
255811878SVenu.Iyer@Sun.COM void
mac_tx_srs_group_teardown(mac_client_impl_t * mcip,flow_entry_t * flent,uint32_t link_type)255911878SVenu.Iyer@Sun.COM mac_tx_srs_group_teardown(mac_client_impl_t *mcip, flow_entry_t *flent,
256011878SVenu.Iyer@Sun.COM     uint32_t link_type)
256111878SVenu.Iyer@Sun.COM {
25628275SEric Cheng 	mac_soft_ring_set_t	*tx_srs;
25638275SEric Cheng 	mac_srs_tx_t		*tx;
256411878SVenu.Iyer@Sun.COM 
256511878SVenu.Iyer@Sun.COM 	if ((tx_srs = flent->fe_tx_srs) == NULL)
256611878SVenu.Iyer@Sun.COM 		return;
256711878SVenu.Iyer@Sun.COM 
25688275SEric Cheng 	tx = &tx_srs->srs_tx;
25698275SEric Cheng 	switch (link_type) {
25708275SEric Cheng 	case SRST_FLOW:
25718275SEric Cheng 		/*
25728275SEric Cheng 		 * For flows, we need to work with passed
25738275SEric Cheng 		 * flent to find the Rx/Tx SRS.
25748275SEric Cheng 		 */
25758275SEric Cheng 		mac_tx_srs_quiesce(tx_srs, SRS_CONDEMNED);
25768275SEric Cheng 		break;
25778275SEric Cheng 	case SRST_LINK:
257811878SVenu.Iyer@Sun.COM 		mac_tx_client_condemn((mac_client_handle_t)mcip);
25798275SEric Cheng 		if (tx->st_arg2 != NULL) {
25808275SEric Cheng 			ASSERT(tx_srs->srs_type & SRST_TX);
258111878SVenu.Iyer@Sun.COM 			/*
258211878SVenu.Iyer@Sun.COM 			 * The ring itself will be stopped when
258311878SVenu.Iyer@Sun.COM 			 * we release the group or in the
258411878SVenu.Iyer@Sun.COM 			 * mac_datapath_teardown (for the default
258511878SVenu.Iyer@Sun.COM 			 * group)
258611878SVenu.Iyer@Sun.COM 			 */
258711878SVenu.Iyer@Sun.COM 			tx->st_arg2 = NULL;
25888275SEric Cheng 		}
25898275SEric Cheng 		break;
25908275SEric Cheng 	default:
25918275SEric Cheng 		ASSERT(B_FALSE);
25928275SEric Cheng 		break;
25938275SEric Cheng 	}
25948275SEric Cheng 	mac_srs_free(tx_srs);
25958275SEric Cheng 	flent->fe_tx_srs = NULL;
25968275SEric Cheng }
25978275SEric Cheng 
25988275SEric Cheng /*
259911878SVenu.Iyer@Sun.COM  * This is the group state machine.
260011878SVenu.Iyer@Sun.COM  *
260111878SVenu.Iyer@Sun.COM  * The state of an Rx group is given by
26028275SEric Cheng  * the following table. The default group and its rings are started in
26038275SEric Cheng  * mac_start itself and the default group stays in SHARED state until
26048275SEric Cheng  * mac_stop at which time the group and rings are stopped and and it
26058275SEric Cheng  * reverts to the Registered state.
26068275SEric Cheng  *
26078275SEric Cheng  * Typically this function is called on a group after adding or removing a
26088275SEric Cheng  * client from it, to find out what should be the new state of the group.
26098275SEric Cheng  * If the new state is RESERVED, then the client that owns this group
26108275SEric Cheng  * exclusively is also returned. Note that adding or removing a client from
26118275SEric Cheng  * a group could also impact the default group and the caller needs to
26128275SEric Cheng  * evaluate the effect on the default group.
26138275SEric Cheng  *
26148275SEric Cheng  * Group type		# of clients	mi_nactiveclients	Group State
26158275SEric Cheng  *			in the group
26168275SEric Cheng  *
26178275SEric Cheng  * Non-default		0		N.A.			REGISTERED
26188275SEric Cheng  * Non-default		1		N.A.			RESERVED
26198275SEric Cheng  *
26208275SEric Cheng  * Default		0		N.A.			SHARED
26218275SEric Cheng  * Default		1		1			RESERVED
26228275SEric Cheng  * Default		1		> 1			SHARED
26238275SEric Cheng  * Default		> 1		N.A.			SHARED
262411878SVenu.Iyer@Sun.COM  *
262511878SVenu.Iyer@Sun.COM  * For a TX group, the following is the state table.
262611878SVenu.Iyer@Sun.COM  *
262711878SVenu.Iyer@Sun.COM  * Group type		# of clients	Group State
262811878SVenu.Iyer@Sun.COM  *			in the group
262911878SVenu.Iyer@Sun.COM  *
263011878SVenu.Iyer@Sun.COM  * Non-default		0		REGISTERED
263111878SVenu.Iyer@Sun.COM  * Non-default		1		RESERVED
263211878SVenu.Iyer@Sun.COM  *
263311878SVenu.Iyer@Sun.COM  * Default		0		REGISTERED
263411878SVenu.Iyer@Sun.COM  * Default		1		RESERVED
263511878SVenu.Iyer@Sun.COM  * Default		> 1		SHARED
26368275SEric Cheng  */
26378275SEric Cheng mac_group_state_t
mac_group_next_state(mac_group_t * grp,mac_client_impl_t ** group_only_mcip,mac_group_t * defgrp,boolean_t rx_group)263811878SVenu.Iyer@Sun.COM mac_group_next_state(mac_group_t *grp, mac_client_impl_t **group_only_mcip,
263911878SVenu.Iyer@Sun.COM     mac_group_t *defgrp, boolean_t rx_group)
26408275SEric Cheng {
26418275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)grp->mrg_mh;
26428275SEric Cheng 
26438275SEric Cheng 	*group_only_mcip = NULL;
26448275SEric Cheng 
26458275SEric Cheng 	/* Non-default group */
26468275SEric Cheng 
264711878SVenu.Iyer@Sun.COM 	if (grp != defgrp) {
264811878SVenu.Iyer@Sun.COM 		if (MAC_GROUP_NO_CLIENT(grp))
26498275SEric Cheng 			return (MAC_GROUP_STATE_REGISTERED);
26508275SEric Cheng 
265111878SVenu.Iyer@Sun.COM 		*group_only_mcip = MAC_GROUP_ONLY_CLIENT(grp);
26528275SEric Cheng 		if (*group_only_mcip != NULL)
26538275SEric Cheng 			return (MAC_GROUP_STATE_RESERVED);
26548275SEric Cheng 
26558275SEric Cheng 		return (MAC_GROUP_STATE_SHARED);
26568275SEric Cheng 	}
26578275SEric Cheng 
26588275SEric Cheng 	/* Default group */
26598275SEric Cheng 
266011878SVenu.Iyer@Sun.COM 	if (MAC_GROUP_NO_CLIENT(grp)) {
266111878SVenu.Iyer@Sun.COM 		if (rx_group)
266211878SVenu.Iyer@Sun.COM 			return (MAC_GROUP_STATE_SHARED);
266311878SVenu.Iyer@Sun.COM 		else
266411878SVenu.Iyer@Sun.COM 			return (MAC_GROUP_STATE_REGISTERED);
266511878SVenu.Iyer@Sun.COM 	}
266611878SVenu.Iyer@Sun.COM 	*group_only_mcip = MAC_GROUP_ONLY_CLIENT(grp);
266711878SVenu.Iyer@Sun.COM 	if (*group_only_mcip == NULL)
26688275SEric Cheng 		return (MAC_GROUP_STATE_SHARED);
26698275SEric Cheng 
267011878SVenu.Iyer@Sun.COM 	if (rx_group && mip->mi_nactiveclients != 1)
267111878SVenu.Iyer@Sun.COM 		return (MAC_GROUP_STATE_SHARED);
267211878SVenu.Iyer@Sun.COM 
26738275SEric Cheng 	ASSERT(*group_only_mcip != NULL);
26748275SEric Cheng 	return (MAC_GROUP_STATE_RESERVED);
26758275SEric Cheng }
26768275SEric Cheng 
26778275SEric Cheng /*
26788275SEric Cheng  * OVERVIEW NOTES FOR DATAPATH
26798275SEric Cheng  * ===========================
26808275SEric Cheng  *
26818275SEric Cheng  * Create an SRS and setup the corresponding flow function and args.
26828275SEric Cheng  * Add a classification rule for the flow specified by 'flent' and program
26838275SEric Cheng  * the hardware classifier when applicable.
26848275SEric Cheng  *
26858275SEric Cheng  * Rx ring assignment, SRS, polling and B/W enforcement
26868275SEric Cheng  * ----------------------------------------------------
26878275SEric Cheng  *
26888275SEric Cheng  * We try to use H/W classification on NIC and assign traffic to a
26898275SEric Cheng  * MAC address to a particular Rx ring. There is a 1-1 mapping
26908275SEric Cheng  * between a SRS and a Rx ring. The SRS (short for soft ring set)
26918275SEric Cheng  * dynamically switches the underlying Rx ring between interrupt
26928275SEric Cheng  * and polling mode and enforces any specified B/W control.
26938275SEric Cheng  *
26948275SEric Cheng  * There is always a SRS created and tied to each H/W and S/W rule.
26958275SEric Cheng  * Whenever we create a H/W rule, we always add the the same rule to
26968275SEric Cheng  * S/W classifier and tie a SRS to it.
26978275SEric Cheng  *
26988275SEric Cheng  * In case a B/W control is specified, its broken into bytes
26998275SEric Cheng  * per ticks and as soon as the quota for a tick is exhausted,
27008275SEric Cheng  * the underlying Rx ring is forced into poll mode for remianing
27018275SEric Cheng  * tick. The SRS poll thread only polls for bytes that are
27028275SEric Cheng  * allowed to come in the SRS. We typically let 4x the configured
27038275SEric Cheng  * B/W worth of packets to come in the SRS (to prevent unnecessary
27048275SEric Cheng  * drops due to bursts) but only process the specified amount.
27058275SEric Cheng  *
27068275SEric Cheng  * A Link (primary NIC, VNIC, VLAN or aggr) can have 1 or more
27078275SEric Cheng  * Rx rings (and corresponding SRSs) assigned to it. The SRS
27088275SEric Cheng  * in turn can have softrings to do protocol level fanout or
27098275SEric Cheng  * softrings to do S/W based fanout or both. In case the NIC
27108275SEric Cheng  * has no Rx rings, we do S/W classification to respective SRS.
27118275SEric Cheng  * The S/W classification rule is always setup and ready. This
27128275SEric Cheng  * allows the MAC layer to reassign Rx rings whenever needed
27138275SEric Cheng  * but packets still continue to flow via the default path and
27148275SEric Cheng  * getting S/W classified to correct SRS.
27158275SEric Cheng  *
27168275SEric Cheng  * In other cases where a NIC or VNIC is plumbed, our goal is use
27178275SEric Cheng  * H/W classifier and get two Rx ring assigned for the Link. One
27188275SEric Cheng  * for TCP and one for UDP|SCTP. The respective SRS still do the
27198275SEric Cheng  * polling on the Rx ring. For Link that is plumbed for IP, there
27208275SEric Cheng  * is a TCP squeue which also does polling and can control the
27218275SEric Cheng  * the Rx ring directly (where SRS is just pass through). For
27228275SEric Cheng  * the following cases, the SRS does the polling underneath.
27238275SEric Cheng  * 1) non IP based Links (Links which are not plumbed via ifconfig)
27248275SEric Cheng  *    and paths which have no IP squeues (UDP & SCTP)
27258275SEric Cheng  * 2) If B/W control is specified on the Link
27268275SEric Cheng  * 3) If S/W fanout is secified
27278275SEric Cheng  *
27288275SEric Cheng  * Note1: As of current implementation, we try to assign only 1 Rx
27298275SEric Cheng  * ring per Link and more than 1 Rx ring for primary Link for
27308275SEric Cheng  * H/W based fanout. We always create following softrings per SRS:
27318275SEric Cheng  * 1) TCP softring which is polled by TCP squeue where possible
27328275SEric Cheng  *    (and also bypasses DLS)
27338275SEric Cheng  * 2) UDP/SCTP based which bypasses DLS
27348275SEric Cheng  * 3) OTH softring which goes via DLS (currently deal with IPv6
27358275SEric Cheng  *    and non TCP/UDP/SCTP for IPv4 packets).
27368275SEric Cheng  *
27378275SEric Cheng  * It is necessary to create 3 softrings since SRS has to poll
27388275SEric Cheng  * the single Rx ring underneath and enforce any link level B/W
27398275SEric Cheng  * control (we can't switch the Rx ring in poll mode just based
27408275SEric Cheng  * on TCP squeue if the same Rx ring is sharing UDP and other
27418275SEric Cheng  * traffic as well). Once polling is done and any Link level B/W
27428275SEric Cheng  * control is specified, the packets are assigned to respective
27438275SEric Cheng  * softring based on protocol. Since TCP has IP based squeue
27448275SEric Cheng  * which benefits by polling, we separate TCP packets into
27458275SEric Cheng  * its own softring which can be polled by IP squeue. We need
27468275SEric Cheng  * to separate out UDP/SCTP to UDP softring since it can bypass
27478275SEric Cheng  * the DLS layer which has heavy performance advanatges and we
27488275SEric Cheng  * need a softring (OTH) for rest.
27498275SEric Cheng  *
27508275SEric Cheng  * ToDo: The 3 softrings for protocol are needed only till we can
27518275SEric Cheng  * get rid of DLS from datapath, make IPv4 and IPv6 paths
27528275SEric Cheng  * symmetric (deal with mac_header_info for v6 and polling for
27538275SEric Cheng  * IPv4 TCP - ip_accept_tcp is IPv4 specific although squeues
27548275SEric Cheng  * are generic), and bring SAP based classification to MAC layer
27558275SEric Cheng  *
27568275SEric Cheng  * H/W and S/W based fanout and multiple Rx rings per Link
27578275SEric Cheng  * -------------------------------------------------------
27588275SEric Cheng  *
27598275SEric Cheng  * In case, fanout is requested (or determined automatically based
27608275SEric Cheng  * on Link speed and processor speed), we try to assign multiple
27618275SEric Cheng  * Rx rings per Link with their respective SRS. In this case
27628275SEric Cheng  * the NIC should be capable of fanning out incoming packets between
27638275SEric Cheng  * the assigned Rx rings (H/W based fanout). All the SRS
27648275SEric Cheng  * individually switch their Rx ring between interrupt and polling
27658275SEric Cheng  * mode but share a common B/W control counter in case of Link
27668275SEric Cheng  * level B/W is specified.
27678275SEric Cheng  *
27688275SEric Cheng  * If S/W based fanout is specified in lieu of H/W based fanout,
27698275SEric Cheng  * the Link SRS creates the specified number of softrings for
27708275SEric Cheng  * each protocol (TCP, UDP, OTH). Incoming packets are fanned
27718275SEric Cheng  * out to the correct softring based on their protocol and
27728275SEric Cheng  * protocol specific hash function.
27738275SEric Cheng  *
27748275SEric Cheng  * Primary and non primary MAC clients
27758275SEric Cheng  * -----------------------------------
27768275SEric Cheng  *
27778275SEric Cheng  * The NICs, VNICs, Vlans, and Aggrs are typically termed as Links
27788275SEric Cheng  * and are a Layer 2 construct.
27798275SEric Cheng  *
27808275SEric Cheng  * Primary NIC:
27818275SEric Cheng  *	The Link that owns the primary MAC address and typically
27828275SEric Cheng  *	is used as the data NIC in non virtualized cases. As such
27838275SEric Cheng  *	H/W resources are preferntially given to primary NIC. As
27848275SEric Cheng  *	far as code is concerned, there is no difference in the
27858275SEric Cheng  *	primary NIC vs VNICs. They are all treated as Links.
27868275SEric Cheng  *	At the very first call to mac_unicast_add() we program the S/W
27878275SEric Cheng  *	classifier for the primary MAC address, get a soft ring set
27888275SEric Cheng  *	(and soft rings based on 'ip_soft_ring_cnt')
27898275SEric Cheng  *	and a Rx ring assigned for polling to get enabled.
27908275SEric Cheng  *	When IP get plumbed and negotiates polling, we can
27918275SEric Cheng  *	let squeue do the polling on TCP softring.
27928275SEric Cheng  *
27938275SEric Cheng  * VNICs:
27948275SEric Cheng  *	Same as any other Link. As long as the H/W resource assignments
27958275SEric Cheng  *	are equal, the data path and setup for all Links is same.
27968275SEric Cheng  *
27978275SEric Cheng  * Flows:
27988275SEric Cheng  *	Can be configured on Links. They have their own SRS and the
27998275SEric Cheng  *	S/W classifier is programmed appropriately based on the flow.
28008275SEric Cheng  *	The flows typically deal with layer 3 and above and
28018275SEric Cheng  *	creates a soft ring set specific to the flow. The receive
28028275SEric Cheng  *	side function is switched from mac_rx_srs_process to
28038275SEric Cheng  *	mac_rx_srs_subflow_process which first tries to assign the
28048275SEric Cheng  *	packet to appropriate flow SRS and failing which assigns it
28058275SEric Cheng  *	to link SRS. This allows us to avoid the layered approach
28068275SEric Cheng  *	which gets complex.
28078275SEric Cheng  *
28088275SEric Cheng  * By the time mac_datapath_setup() completes, we already have the
28098275SEric Cheng  * soft rings set, Rx rings, soft rings, etc figured out and both H/W
28108275SEric Cheng  * and S/W classifiers programmed. IP is not plumbed yet (and might
28118275SEric Cheng  * never be for Virtual Machines guest OS path). When IP is plumbed
28128275SEric Cheng  * (for both NIC and VNIC), we do a capability negotiation for polling
28138275SEric Cheng  * and upcall functions etc.
28148275SEric Cheng  *
28158275SEric Cheng  * Rx ring Assignement NOTES
28168275SEric Cheng  * -------------------------
28178275SEric Cheng  *
28188275SEric Cheng  * For NICs which have only 1 Rx ring (we treat  NICs with no Rx rings
28198275SEric Cheng  * as NIC with a single default ring), we assign the only ring to
282011878SVenu.Iyer@Sun.COM  * primary Link. The primary Link SRS can do polling on it as long as
282111878SVenu.Iyer@Sun.COM  * it is the only link in use and we compare the MAC address for unicast
282211878SVenu.Iyer@Sun.COM  * packets before accepting an incoming packet (there is no need for S/W
282311878SVenu.Iyer@Sun.COM  * classification in this case). We disable polling on the only ring the
282411878SVenu.Iyer@Sun.COM  * moment 2nd link gets created (the polling remains enabled even though
282511878SVenu.Iyer@Sun.COM  * there are broadcast and * multicast flows created).
28268275SEric Cheng  *
28278275SEric Cheng  * If the NIC has more than 1 Rx ring, we assign the default ring (the
28288275SEric Cheng  * 1st ring) to deal with broadcast, multicast and traffic for other
28298275SEric Cheng  * NICs which needs S/W classification. We assign the primary mac
28308275SEric Cheng  * addresses to another ring by specifiying a classification rule for
28318275SEric Cheng  * primary unicast MAC address to the selected ring. The primary Link
28328275SEric Cheng  * (and its SRS) can continue to poll the assigned Rx ring at all times
28338275SEric Cheng  * independantly.
28348275SEric Cheng  *
28358275SEric Cheng  * Note: In future, if no fanout is specified, we try to assign 2 Rx
28368275SEric Cheng  * rings for the primary Link with the primary MAC address + TCP going
28378275SEric Cheng  * to one ring and primary MAC address + UDP|SCTP going to other ring.
28388275SEric Cheng  * Any remaining traffic for primary MAC address can go to the default
28398275SEric Cheng  * Rx ring and get S/W classified. This way the respective SRSs don't
28408275SEric Cheng  * need to do proto fanout and don't need to have softrings at all and
28418275SEric Cheng  * can poll their respective Rx rings.
28428275SEric Cheng  *
28438275SEric Cheng  * As an optimization, when a new NIC or VNIC is created, we can get
28448275SEric Cheng  * only one Rx ring and make it a TCP specific Rx ring and use the
28458275SEric Cheng  * H/W default Rx ring for the rest (this Rx ring is never polled).
284611878SVenu.Iyer@Sun.COM  *
284711878SVenu.Iyer@Sun.COM  * For clients that don't have MAC address, but want to receive and
284811878SVenu.Iyer@Sun.COM  * transmit packets (e.g, bpf, gvrp etc.), we need to setup the datapath.
284911878SVenu.Iyer@Sun.COM  * For such clients (identified by the MCIS_NO_UNICAST_ADDR flag) we
285011878SVenu.Iyer@Sun.COM  * always give the default group and use software classification (i.e.
285111878SVenu.Iyer@Sun.COM  * even if this is the only client in the default group, we will
285211878SVenu.Iyer@Sun.COM  * leave group as shared).
28538275SEric Cheng  */
28548275SEric Cheng int
mac_datapath_setup(mac_client_impl_t * mcip,flow_entry_t * flent,uint32_t link_type)28558275SEric Cheng mac_datapath_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
28568275SEric Cheng     uint32_t link_type)
28578275SEric Cheng {
28588275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
285911878SVenu.Iyer@Sun.COM 	mac_group_t		*rgroup = NULL;
286011878SVenu.Iyer@Sun.COM 	mac_group_t		*tgroup = NULL;
286111878SVenu.Iyer@Sun.COM 	mac_group_t		*default_rgroup;
286211878SVenu.Iyer@Sun.COM 	mac_group_t		*default_tgroup;
28638275SEric Cheng 	int			err;
28648275SEric Cheng 	uint8_t 		*mac_addr;
28658275SEric Cheng 	mac_group_state_t	next_state;
28668275SEric Cheng 	mac_client_impl_t	*group_only_mcip;
286711878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
286811878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*emrp = MCIP_EFFECTIVE_PROPS(mcip);
286911878SVenu.Iyer@Sun.COM 	boolean_t		rxhw;
287011878SVenu.Iyer@Sun.COM 	boolean_t		txhw;
287111878SVenu.Iyer@Sun.COM 	boolean_t		use_default = B_FALSE;
287211878SVenu.Iyer@Sun.COM 	cpupart_t		*cpupart;
287311878SVenu.Iyer@Sun.COM 	boolean_t		no_unicast;
287411878SVenu.Iyer@Sun.COM 	boolean_t		isprimary = flent->fe_type & FLOW_PRIMARY_MAC;
287511878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*reloc_pmcip = NULL;
28768275SEric Cheng 
28778275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
28788275SEric Cheng 
28798275SEric Cheng 	switch (link_type) {
28808275SEric Cheng 	case SRST_FLOW:
288111878SVenu.Iyer@Sun.COM 		mac_srs_group_setup(mcip, flent, link_type);
28828275SEric Cheng 		return (0);
28838275SEric Cheng 
28848275SEric Cheng 	case SRST_LINK:
288511878SVenu.Iyer@Sun.COM 		no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
28868275SEric Cheng 		mac_addr = flent->fe_flow_desc.fd_dst_mac;
28878275SEric Cheng 
288811878SVenu.Iyer@Sun.COM 		/* Default RX group */
288911878SVenu.Iyer@Sun.COM 		default_rgroup = MAC_DEFAULT_RX_GROUP(mip);
289011878SVenu.Iyer@Sun.COM 
289111878SVenu.Iyer@Sun.COM 		/* Default TX group */
289211878SVenu.Iyer@Sun.COM 		default_tgroup = MAC_DEFAULT_TX_GROUP(mip);
289311878SVenu.Iyer@Sun.COM 
289411878SVenu.Iyer@Sun.COM 		if (no_unicast) {
289511878SVenu.Iyer@Sun.COM 			rgroup = default_rgroup;
289611878SVenu.Iyer@Sun.COM 			tgroup = default_tgroup;
289711878SVenu.Iyer@Sun.COM 			goto grp_found;
28988275SEric Cheng 		}
289911878SVenu.Iyer@Sun.COM 		rxhw = (mrp->mrp_mask & MRP_RX_RINGS) &&
290011878SVenu.Iyer@Sun.COM 		    (mrp->mrp_nrxrings > 0 ||
290111878SVenu.Iyer@Sun.COM 		    (mrp->mrp_mask & MRP_RXRINGS_UNSPEC));
290211878SVenu.Iyer@Sun.COM 		txhw = (mrp->mrp_mask & MRP_TX_RINGS) &&
290311878SVenu.Iyer@Sun.COM 		    (mrp->mrp_ntxrings > 0 ||
290411878SVenu.Iyer@Sun.COM 		    (mrp->mrp_mask & MRP_TXRINGS_UNSPEC));
290511878SVenu.Iyer@Sun.COM 
290611878SVenu.Iyer@Sun.COM 		/*
290711878SVenu.Iyer@Sun.COM 		 * By default we have given the primary all the rings
290811878SVenu.Iyer@Sun.COM 		 * i.e. the default group. Let's see if the primary
290911878SVenu.Iyer@Sun.COM 		 * needs to be relocated so that the addition of this
291011878SVenu.Iyer@Sun.COM 		 * client doesn't impact the primary's performance,
291111878SVenu.Iyer@Sun.COM 		 * i.e. if the primary is in the default group and
291211878SVenu.Iyer@Sun.COM 		 * we add this client, the primary will lose polling.
291311878SVenu.Iyer@Sun.COM 		 * We do this only for NICs supporting dynamic ring
291411878SVenu.Iyer@Sun.COM 		 * grouping and only when this is the first client
291511878SVenu.Iyer@Sun.COM 		 * after the primary (i.e. nactiveclients is 2)
291611878SVenu.Iyer@Sun.COM 		 */
291711878SVenu.Iyer@Sun.COM 		if (!isprimary && mip->mi_nactiveclients == 2 &&
291811878SVenu.Iyer@Sun.COM 		    (group_only_mcip = mac_primary_client_handle(mip)) !=
291911878SVenu.Iyer@Sun.COM 		    NULL && mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
292011878SVenu.Iyer@Sun.COM 			reloc_pmcip = mac_check_primary_relocation(
292111878SVenu.Iyer@Sun.COM 			    group_only_mcip, rxhw);
292211878SVenu.Iyer@Sun.COM 		}
292311878SVenu.Iyer@Sun.COM 		/*
292411878SVenu.Iyer@Sun.COM 		 * Check to see if we can get an exclusive group for
292511878SVenu.Iyer@Sun.COM 		 * this mac address or if there already exists a
292611878SVenu.Iyer@Sun.COM 		 * group that has this mac address (case of VLANs).
292711878SVenu.Iyer@Sun.COM 		 * If no groups are available, use the default group.
292811878SVenu.Iyer@Sun.COM 		 */
292911878SVenu.Iyer@Sun.COM 		rgroup = mac_reserve_rx_group(mcip, mac_addr, B_FALSE);
293011878SVenu.Iyer@Sun.COM 		if (rgroup == NULL && rxhw) {
293111878SVenu.Iyer@Sun.COM 			err = ENOSPC;
293211878SVenu.Iyer@Sun.COM 			goto setup_failed;
293311878SVenu.Iyer@Sun.COM 		} else if (rgroup == NULL) {
293411878SVenu.Iyer@Sun.COM 			rgroup = default_rgroup;
293511878SVenu.Iyer@Sun.COM 		}
293611878SVenu.Iyer@Sun.COM 		/*
293711878SVenu.Iyer@Sun.COM 		 * Check to see if we can get an exclusive group for
293811878SVenu.Iyer@Sun.COM 		 * this mac client. If no groups are available, use
293911878SVenu.Iyer@Sun.COM 		 * the default group.
294011878SVenu.Iyer@Sun.COM 		 */
294111878SVenu.Iyer@Sun.COM 		tgroup = mac_reserve_tx_group(mcip, B_FALSE);
294211878SVenu.Iyer@Sun.COM 		if (tgroup == NULL && txhw) {
294311878SVenu.Iyer@Sun.COM 			if (rgroup != NULL && rgroup != default_rgroup)
294411878SVenu.Iyer@Sun.COM 				mac_release_rx_group(mcip, rgroup);
294511878SVenu.Iyer@Sun.COM 			err = ENOSPC;
294611878SVenu.Iyer@Sun.COM 			goto setup_failed;
294711878SVenu.Iyer@Sun.COM 		} else if (tgroup == NULL) {
294811878SVenu.Iyer@Sun.COM 			tgroup = default_tgroup;
29498275SEric Cheng 		}
29508275SEric Cheng 
29518275SEric Cheng 		/*
29528275SEric Cheng 		 * Some NICs don't support any Rx rings, so there may not
29538275SEric Cheng 		 * even be a default group.
29548275SEric Cheng 		 */
295511878SVenu.Iyer@Sun.COM 	grp_found:
295611878SVenu.Iyer@Sun.COM 		if (rgroup != NULL) {
295711878SVenu.Iyer@Sun.COM 			if (rgroup != default_rgroup &&
295811878SVenu.Iyer@Sun.COM 			    MAC_GROUP_NO_CLIENT(rgroup) &&
295911878SVenu.Iyer@Sun.COM 			    (rxhw || mcip->mci_share != NULL)) {
296011878SVenu.Iyer@Sun.COM 				MAC_RX_GRP_RESERVED(mip);
296111878SVenu.Iyer@Sun.COM 				if (mip->mi_rx_group_type ==
296211878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
296311878SVenu.Iyer@Sun.COM 					MAC_RX_RING_RESERVED(mip,
296411878SVenu.Iyer@Sun.COM 					    rgroup->mrg_cur_count);
296511878SVenu.Iyer@Sun.COM 				}
296611878SVenu.Iyer@Sun.COM 			}
296711878SVenu.Iyer@Sun.COM 			flent->fe_rx_ring_group = rgroup;
29688275SEric Cheng 			/*
29698275SEric Cheng 			 * Add the client to the group. This could cause
29708275SEric Cheng 			 * either this group to move to the shared state or
29718275SEric Cheng 			 * cause the default group to move to the shared state.
29728275SEric Cheng 			 * The actions on this group are done here, while the
29738275SEric Cheng 			 * actions on the default group are postponed to
29748275SEric Cheng 			 * the end of this function.
29758275SEric Cheng 			 */
297611878SVenu.Iyer@Sun.COM 			mac_group_add_client(rgroup, mcip);
297711878SVenu.Iyer@Sun.COM 			next_state = mac_group_next_state(rgroup,
297811878SVenu.Iyer@Sun.COM 			    &group_only_mcip, default_rgroup, B_TRUE);
297911878SVenu.Iyer@Sun.COM 			mac_set_group_state(rgroup, next_state);
29808275SEric Cheng 		}
29818275SEric Cheng 
298211878SVenu.Iyer@Sun.COM 		if (tgroup != NULL) {
298311878SVenu.Iyer@Sun.COM 			if (tgroup != default_tgroup &&
298411878SVenu.Iyer@Sun.COM 			    MAC_GROUP_NO_CLIENT(tgroup) &&
298511878SVenu.Iyer@Sun.COM 			    (txhw || mcip->mci_share != NULL)) {
298611878SVenu.Iyer@Sun.COM 				MAC_TX_GRP_RESERVED(mip);
298711878SVenu.Iyer@Sun.COM 				if (mip->mi_tx_group_type ==
298811878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
298911878SVenu.Iyer@Sun.COM 					MAC_TX_RING_RESERVED(mip,
299011878SVenu.Iyer@Sun.COM 					    tgroup->mrg_cur_count);
299111878SVenu.Iyer@Sun.COM 				}
299211878SVenu.Iyer@Sun.COM 			}
299311878SVenu.Iyer@Sun.COM 			flent->fe_tx_ring_group = tgroup;
299411878SVenu.Iyer@Sun.COM 			mac_group_add_client(tgroup, mcip);
299511878SVenu.Iyer@Sun.COM 			next_state = mac_group_next_state(tgroup,
299611878SVenu.Iyer@Sun.COM 			    &group_only_mcip, default_tgroup, B_FALSE);
299711878SVenu.Iyer@Sun.COM 			tgroup->mrg_state = next_state;
299811878SVenu.Iyer@Sun.COM 		}
29998275SEric Cheng 		/*
30008275SEric Cheng 		 * Setup the Rx and Tx SRSes. If we got a pristine group
30018275SEric Cheng 		 * exclusively above, mac_srs_group_setup would simply create
30028275SEric Cheng 		 * the required SRSes. If we ended up sharing a previously
30038275SEric Cheng 		 * reserved group, mac_srs_group_setup would also dismantle the
30048275SEric Cheng 		 * SRSes of the previously exclusive group
30058275SEric Cheng 		 */
300611878SVenu.Iyer@Sun.COM 		mac_srs_group_setup(mcip, flent, link_type);
300711878SVenu.Iyer@Sun.COM 
300811878SVenu.Iyer@Sun.COM 		/* We are setting up minimal datapath only */
300911878SVenu.Iyer@Sun.COM 		if (no_unicast)
301011878SVenu.Iyer@Sun.COM 			break;
30118275SEric Cheng 		/* Program the S/W Classifer */
30128275SEric Cheng 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0)
30138275SEric Cheng 			goto setup_failed;
30148275SEric Cheng 
30158275SEric Cheng 		/* Program the H/W Classifier */
301611878SVenu.Iyer@Sun.COM 		if ((err = mac_add_macaddr(mip, rgroup, mac_addr,
30178400SNicolas.Droux@Sun.COM 		    (mcip->mci_state_flags & MCIS_UNICAST_HW) != 0)) != 0)
30188275SEric Cheng 			goto setup_failed;
30198275SEric Cheng 		mcip->mci_unicast = mac_find_macaddr(mip, mac_addr);
30208275SEric Cheng 		ASSERT(mcip->mci_unicast != NULL);
302111878SVenu.Iyer@Sun.COM 		/* Initialize the v6 local addr used by link protection */
302211878SVenu.Iyer@Sun.COM 		mac_protect_update_v6_local_addr(mcip);
30238275SEric Cheng 		break;
30248275SEric Cheng 
30258275SEric Cheng 	default:
30268275SEric Cheng 		ASSERT(B_FALSE);
30278275SEric Cheng 		break;
30288275SEric Cheng 	}
30298275SEric Cheng 
30308275SEric Cheng 	/*
30318275SEric Cheng 	 * All broadcast and multicast traffic is received only on the default
30328275SEric Cheng 	 * group. If we have setup the datapath for a non-default group above
30338275SEric Cheng 	 * then move the default group to shared state to allow distribution of
30348275SEric Cheng 	 * incoming broadcast traffic to the other groups and dismantle the
30358275SEric Cheng 	 * SRSes over the default group.
30368275SEric Cheng 	 */
303711878SVenu.Iyer@Sun.COM 	if (rgroup != NULL) {
303811878SVenu.Iyer@Sun.COM 		if (rgroup != default_rgroup) {
303911878SVenu.Iyer@Sun.COM 			if (default_rgroup->mrg_state ==
30408275SEric Cheng 			    MAC_GROUP_STATE_RESERVED) {
304111878SVenu.Iyer@Sun.COM 				group_only_mcip = MAC_GROUP_ONLY_CLIENT(
304211878SVenu.Iyer@Sun.COM 				    default_rgroup);
30438275SEric Cheng 				ASSERT(group_only_mcip != NULL &&
30448275SEric Cheng 				    mip->mi_nactiveclients > 1);
30458275SEric Cheng 
304611878SVenu.Iyer@Sun.COM 				mac_set_group_state(default_rgroup,
30478275SEric Cheng 				    MAC_GROUP_STATE_SHARED);
304811878SVenu.Iyer@Sun.COM 				mac_rx_srs_group_setup(group_only_mcip,
304911878SVenu.Iyer@Sun.COM 				    group_only_mcip->mci_flent, SRST_LINK);
305011878SVenu.Iyer@Sun.COM 				pool_lock();
305111878SVenu.Iyer@Sun.COM 				cpupart = mac_pset_find(mrp, &use_default);
305211878SVenu.Iyer@Sun.COM 				mac_fanout_setup(group_only_mcip,
30538275SEric Cheng 				    group_only_mcip->mci_flent,
305411878SVenu.Iyer@Sun.COM 				    MCIP_RESOURCE_PROPS(group_only_mcip),
305511878SVenu.Iyer@Sun.COM 				    mac_rx_deliver, group_only_mcip, NULL,
305611878SVenu.Iyer@Sun.COM 				    cpupart);
305711878SVenu.Iyer@Sun.COM 				mac_set_pool_effective(use_default, cpupart,
305811878SVenu.Iyer@Sun.COM 				    mrp, emrp);
305911878SVenu.Iyer@Sun.COM 				pool_unlock();
30608275SEric Cheng 			}
306111878SVenu.Iyer@Sun.COM 			ASSERT(default_rgroup->mrg_state ==
30628275SEric Cheng 			    MAC_GROUP_STATE_SHARED);
30638275SEric Cheng 		}
30648275SEric Cheng 		/*
30658275SEric Cheng 		 * If we get an exclusive group for a VLAN MAC client we
30668275SEric Cheng 		 * need to take the s/w path to make the additional check for
30678275SEric Cheng 		 * the vid. Disable polling and set it to s/w classification.
306811878SVenu.Iyer@Sun.COM 		 * Similarly for clients that don't have a unicast address.
30698275SEric Cheng 		 */
307011878SVenu.Iyer@Sun.COM 		if (rgroup->mrg_state == MAC_GROUP_STATE_RESERVED &&
307111878SVenu.Iyer@Sun.COM 		    (i_mac_flow_vid(flent) != VLAN_ID_NONE || no_unicast)) {
307211878SVenu.Iyer@Sun.COM 			mac_rx_switch_grp_to_sw(rgroup);
30738275SEric Cheng 		}
30748275SEric Cheng 	}
307511878SVenu.Iyer@Sun.COM 	mac_set_rings_effective(mcip);
30768275SEric Cheng 	return (0);
30778275SEric Cheng 
30788275SEric Cheng setup_failed:
307911878SVenu.Iyer@Sun.COM 	/* Switch the primary back to default group */
308011878SVenu.Iyer@Sun.COM 	if (reloc_pmcip != NULL) {
308111878SVenu.Iyer@Sun.COM 		(void) mac_rx_switch_group(reloc_pmcip,
308211878SVenu.Iyer@Sun.COM 		    reloc_pmcip->mci_flent->fe_rx_ring_group, default_rgroup);
308311878SVenu.Iyer@Sun.COM 	}
30848275SEric Cheng 	mac_datapath_teardown(mcip, flent, link_type);
30858275SEric Cheng 	return (err);
30868275SEric Cheng }
30878275SEric Cheng 
30888275SEric Cheng void
mac_datapath_teardown(mac_client_impl_t * mcip,flow_entry_t * flent,uint32_t link_type)30898275SEric Cheng mac_datapath_teardown(mac_client_impl_t *mcip, flow_entry_t *flent,
30908275SEric Cheng     uint32_t link_type)
30918275SEric Cheng {
30928275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
30938275SEric Cheng 	mac_group_t		*group = NULL;
30948275SEric Cheng 	mac_client_impl_t	*grp_only_mcip;
30958275SEric Cheng 	flow_entry_t		*group_only_flent;
30968275SEric Cheng 	mac_group_t		*default_group;
30978275SEric Cheng 	boolean_t		check_default_group = B_FALSE;
30988275SEric Cheng 	mac_group_state_t	next_state;
309911878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
31008275SEric Cheng 
31018275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
31028275SEric Cheng 
31038275SEric Cheng 	switch (link_type) {
31048275SEric Cheng 	case SRST_FLOW:
310511878SVenu.Iyer@Sun.COM 		mac_rx_srs_group_teardown(flent, B_FALSE);
310611878SVenu.Iyer@Sun.COM 		mac_tx_srs_group_teardown(mcip, flent, SRST_FLOW);
31078275SEric Cheng 		return;
31088275SEric Cheng 
31098275SEric Cheng 	case SRST_LINK:
31108275SEric Cheng 		/* Stop sending packets */
31118275SEric Cheng 		mac_tx_client_block(mcip);
31128275SEric Cheng 
31138275SEric Cheng 		/* Stop the packets coming from the H/W */
31148275SEric Cheng 		if (mcip->mci_unicast != NULL) {
31158275SEric Cheng 			int err;
31168275SEric Cheng 			err = mac_remove_macaddr(mcip->mci_unicast);
31178275SEric Cheng 			if (err != 0) {
31188275SEric Cheng 				cmn_err(CE_WARN, "%s: failed to remove a MAC"
31198275SEric Cheng 				    " address because of error 0x%x",
31208275SEric Cheng 				    mip->mi_name, err);
31218275SEric Cheng 			}
31228275SEric Cheng 			mcip->mci_unicast = NULL;
31238275SEric Cheng 		}
31248275SEric Cheng 
31258275SEric Cheng 		/* Stop the packets coming from the S/W classifier */
31268275SEric Cheng 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
31278275SEric Cheng 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
31288275SEric Cheng 
31298275SEric Cheng 		/* Now quiesce and destroy all SRS and soft rings */
313011878SVenu.Iyer@Sun.COM 		mac_rx_srs_group_teardown(flent, B_FALSE);
313111878SVenu.Iyer@Sun.COM 		mac_tx_srs_group_teardown(mcip, flent, SRST_LINK);
313211878SVenu.Iyer@Sun.COM 
31338275SEric Cheng 		ASSERT((mcip->mci_flent == flent) &&
31348275SEric Cheng 		    (flent->fe_next == NULL));
31358275SEric Cheng 
31368275SEric Cheng 		/*
31378275SEric Cheng 		 * Release our hold on the group as well. We need
31388275SEric Cheng 		 * to check if the shared group has only one client
31398275SEric Cheng 		 * left who can use it exclusively. Also, if we
31408275SEric Cheng 		 * were the last client, release the group.
31418275SEric Cheng 		 */
31428275SEric Cheng 		group = flent->fe_rx_ring_group;
314311878SVenu.Iyer@Sun.COM 		default_group = MAC_DEFAULT_RX_GROUP(mip);
31448275SEric Cheng 		if (group != NULL) {
314511878SVenu.Iyer@Sun.COM 			mac_group_remove_client(group, mcip);
314611878SVenu.Iyer@Sun.COM 			next_state = mac_group_next_state(group,
314711878SVenu.Iyer@Sun.COM 			    &grp_only_mcip, default_group, B_TRUE);
31488275SEric Cheng 			if (next_state == MAC_GROUP_STATE_RESERVED) {
31498275SEric Cheng 				/*
31508275SEric Cheng 				 * Only one client left on this RX group.
31518275SEric Cheng 				 */
31528275SEric Cheng 				ASSERT(grp_only_mcip != NULL);
315311878SVenu.Iyer@Sun.COM 				mac_set_group_state(group,
31548275SEric Cheng 				    MAC_GROUP_STATE_RESERVED);
31558275SEric Cheng 				group_only_flent = grp_only_mcip->mci_flent;
31568275SEric Cheng 
31578275SEric Cheng 				/*
31588275SEric Cheng 				 * The only remaining client has exclusive
31598275SEric Cheng 				 * access on the group. Allow it to
31608275SEric Cheng 				 * dynamically poll the H/W rings etc.
31618275SEric Cheng 				 */
316211878SVenu.Iyer@Sun.COM 				mac_rx_srs_group_setup(grp_only_mcip,
316311878SVenu.Iyer@Sun.COM 				    group_only_flent, SRST_LINK);
316411878SVenu.Iyer@Sun.COM 				mac_fanout_setup(grp_only_mcip,
316511878SVenu.Iyer@Sun.COM 				    group_only_flent,
316611878SVenu.Iyer@Sun.COM 				    MCIP_RESOURCE_PROPS(grp_only_mcip),
316711878SVenu.Iyer@Sun.COM 				    mac_rx_deliver, grp_only_mcip, NULL, NULL);
31688275SEric Cheng 				mac_rx_group_unmark(group, MR_INCIPIENT);
316911878SVenu.Iyer@Sun.COM 				mac_set_rings_effective(grp_only_mcip);
31708275SEric Cheng 			} else if (next_state == MAC_GROUP_STATE_REGISTERED) {
31718275SEric Cheng 				/*
31728275SEric Cheng 				 * This is a non-default group being freed up.
31738275SEric Cheng 				 * We need to reevaluate the default group
31748275SEric Cheng 				 * to see if the primary client can get
31758275SEric Cheng 				 * exclusive access to the default group.
31768275SEric Cheng 				 */
317711878SVenu.Iyer@Sun.COM 				ASSERT(group != MAC_DEFAULT_RX_GROUP(mip));
317811878SVenu.Iyer@Sun.COM 				if (mrp->mrp_mask & MRP_RX_RINGS) {
317911878SVenu.Iyer@Sun.COM 					MAC_RX_GRP_RELEASED(mip);
318011878SVenu.Iyer@Sun.COM 					if (mip->mi_rx_group_type ==
318111878SVenu.Iyer@Sun.COM 					    MAC_GROUP_TYPE_DYNAMIC) {
318211878SVenu.Iyer@Sun.COM 						MAC_RX_RING_RELEASED(mip,
318311878SVenu.Iyer@Sun.COM 						    group->mrg_cur_count);
318411878SVenu.Iyer@Sun.COM 					}
318511878SVenu.Iyer@Sun.COM 				}
31868275SEric Cheng 				mac_release_rx_group(mcip, group);
318711878SVenu.Iyer@Sun.COM 				mac_set_group_state(group,
31888275SEric Cheng 				    MAC_GROUP_STATE_REGISTERED);
31898275SEric Cheng 				check_default_group = B_TRUE;
31908275SEric Cheng 			} else {
31918275SEric Cheng 				ASSERT(next_state == MAC_GROUP_STATE_SHARED);
319211878SVenu.Iyer@Sun.COM 				mac_set_group_state(group,
31938275SEric Cheng 				    MAC_GROUP_STATE_SHARED);
31948275SEric Cheng 				mac_rx_group_unmark(group, MR_CONDEMNED);
31958275SEric Cheng 			}
31968275SEric Cheng 			flent->fe_rx_ring_group = NULL;
31978275SEric Cheng 		}
319811878SVenu.Iyer@Sun.COM 		/*
319911878SVenu.Iyer@Sun.COM 		 * Remove the client from the TX group. Additionally, if
320011878SVenu.Iyer@Sun.COM 		 * this a non-default group, then we also need to release
320111878SVenu.Iyer@Sun.COM 		 * the group.
320211878SVenu.Iyer@Sun.COM 		 */
320311878SVenu.Iyer@Sun.COM 		group = flent->fe_tx_ring_group;
320411878SVenu.Iyer@Sun.COM 		default_group = MAC_DEFAULT_TX_GROUP(mip);
320511878SVenu.Iyer@Sun.COM 		if (group != NULL) {
320611878SVenu.Iyer@Sun.COM 			mac_group_remove_client(group, mcip);
320711878SVenu.Iyer@Sun.COM 			next_state = mac_group_next_state(group,
320811878SVenu.Iyer@Sun.COM 			    &grp_only_mcip, default_group, B_FALSE);
320911878SVenu.Iyer@Sun.COM 			if (next_state == MAC_GROUP_STATE_REGISTERED) {
321011878SVenu.Iyer@Sun.COM 				if (group != default_group) {
321111878SVenu.Iyer@Sun.COM 					if (mrp->mrp_mask & MRP_TX_RINGS) {
321211878SVenu.Iyer@Sun.COM 						MAC_TX_GRP_RELEASED(mip);
321311878SVenu.Iyer@Sun.COM 						if (mip->mi_tx_group_type ==
321411878SVenu.Iyer@Sun.COM 						    MAC_GROUP_TYPE_DYNAMIC) {
321511878SVenu.Iyer@Sun.COM 							MAC_TX_RING_RELEASED(
321611878SVenu.Iyer@Sun.COM 							    mip, group->
321711878SVenu.Iyer@Sun.COM 							    mrg_cur_count);
321811878SVenu.Iyer@Sun.COM 						}
321911878SVenu.Iyer@Sun.COM 					}
322011878SVenu.Iyer@Sun.COM 					mac_release_tx_group(mcip, group);
322111878SVenu.Iyer@Sun.COM 					/*
322211878SVenu.Iyer@Sun.COM 					 * If the default group is reserved,
322311878SVenu.Iyer@Sun.COM 					 * then we need to set the effective
322411878SVenu.Iyer@Sun.COM 					 * rings as we would have given
322511878SVenu.Iyer@Sun.COM 					 * back some rings when the group
322611878SVenu.Iyer@Sun.COM 					 * was released
322711878SVenu.Iyer@Sun.COM 					 */
322811878SVenu.Iyer@Sun.COM 					if (mip->mi_tx_group_type ==
322911878SVenu.Iyer@Sun.COM 					    MAC_GROUP_TYPE_DYNAMIC &&
323011878SVenu.Iyer@Sun.COM 					    default_group->mrg_state ==
323111878SVenu.Iyer@Sun.COM 					    MAC_GROUP_STATE_RESERVED) {
323211878SVenu.Iyer@Sun.COM 						grp_only_mcip =
323311878SVenu.Iyer@Sun.COM 						    MAC_GROUP_ONLY_CLIENT
323411878SVenu.Iyer@Sun.COM 						    (default_group);
323511878SVenu.Iyer@Sun.COM 						mac_set_rings_effective(
323611878SVenu.Iyer@Sun.COM 						    grp_only_mcip);
323711878SVenu.Iyer@Sun.COM 					}
323811878SVenu.Iyer@Sun.COM 				} else {
323911878SVenu.Iyer@Sun.COM 					mac_ring_t	*ring;
324011878SVenu.Iyer@Sun.COM 					int		cnt;
324111878SVenu.Iyer@Sun.COM 					int		ringcnt;
324211878SVenu.Iyer@Sun.COM 
324311878SVenu.Iyer@Sun.COM 					/*
324411878SVenu.Iyer@Sun.COM 					 * Stop all the rings except the
324511878SVenu.Iyer@Sun.COM 					 * default ring.
324611878SVenu.Iyer@Sun.COM 					 */
324711878SVenu.Iyer@Sun.COM 					ringcnt = group->mrg_cur_count;
324811878SVenu.Iyer@Sun.COM 					ring = group->mrg_rings;
324911878SVenu.Iyer@Sun.COM 					for (cnt = 0; cnt < ringcnt; cnt++) {
325011878SVenu.Iyer@Sun.COM 						if (ring->mr_state ==
325111878SVenu.Iyer@Sun.COM 						    MR_INUSE && ring !=
325211878SVenu.Iyer@Sun.COM 						    (mac_ring_t *)
325311878SVenu.Iyer@Sun.COM 						    mip->mi_default_tx_ring) {
325411878SVenu.Iyer@Sun.COM 							mac_stop_ring(ring);
325511878SVenu.Iyer@Sun.COM 							ring->mr_flag = 0;
325611878SVenu.Iyer@Sun.COM 						}
325711878SVenu.Iyer@Sun.COM 						ring = ring->mr_next;
325811878SVenu.Iyer@Sun.COM 					}
325911878SVenu.Iyer@Sun.COM 				}
326011878SVenu.Iyer@Sun.COM 			} else if (next_state == MAC_GROUP_STATE_RESERVED) {
326111878SVenu.Iyer@Sun.COM 				mac_set_rings_effective(grp_only_mcip);
326211878SVenu.Iyer@Sun.COM 			}
326311878SVenu.Iyer@Sun.COM 			flent->fe_tx_ring_group = NULL;
326411878SVenu.Iyer@Sun.COM 			group->mrg_state = next_state;
326511878SVenu.Iyer@Sun.COM 		}
32668275SEric Cheng 		break;
32678275SEric Cheng 	default:
32688275SEric Cheng 		ASSERT(B_FALSE);
32698275SEric Cheng 		break;
32708275SEric Cheng 	}
32718275SEric Cheng 
32728275SEric Cheng 	/*
32738275SEric Cheng 	 * The mac client using the default group gets exclusive access to the
32748275SEric Cheng 	 * default group if and only if it is the sole client on the entire
32758275SEric Cheng 	 * mip. If so set the group state to reserved, and set up the SRSes
32768275SEric Cheng 	 * over the default group.
32778275SEric Cheng 	 */
32788275SEric Cheng 	if (check_default_group) {
327911878SVenu.Iyer@Sun.COM 		default_group = MAC_DEFAULT_RX_GROUP(mip);
32808275SEric Cheng 		ASSERT(default_group->mrg_state == MAC_GROUP_STATE_SHARED);
328111878SVenu.Iyer@Sun.COM 		next_state = mac_group_next_state(default_group,
328211878SVenu.Iyer@Sun.COM 		    &grp_only_mcip, default_group, B_TRUE);
32838275SEric Cheng 		if (next_state == MAC_GROUP_STATE_RESERVED) {
32848275SEric Cheng 			ASSERT(grp_only_mcip != NULL &&
32858275SEric Cheng 			    mip->mi_nactiveclients == 1);
328611878SVenu.Iyer@Sun.COM 			mac_set_group_state(default_group,
32878275SEric Cheng 			    MAC_GROUP_STATE_RESERVED);
328811878SVenu.Iyer@Sun.COM 			mac_rx_srs_group_setup(grp_only_mcip,
328911878SVenu.Iyer@Sun.COM 			    grp_only_mcip->mci_flent, SRST_LINK);
329011878SVenu.Iyer@Sun.COM 			mac_fanout_setup(grp_only_mcip,
32918275SEric Cheng 			    grp_only_mcip->mci_flent,
329211878SVenu.Iyer@Sun.COM 			    MCIP_RESOURCE_PROPS(grp_only_mcip), mac_rx_deliver,
329311878SVenu.Iyer@Sun.COM 			    grp_only_mcip, NULL, NULL);
32948833SVenu.Iyer@Sun.COM 			mac_rx_group_unmark(default_group, MR_INCIPIENT);
329511878SVenu.Iyer@Sun.COM 			mac_set_rings_effective(grp_only_mcip);
32968275SEric Cheng 		}
32978275SEric Cheng 	}
329811878SVenu.Iyer@Sun.COM 
329911878SVenu.Iyer@Sun.COM 	/*
330011878SVenu.Iyer@Sun.COM 	 * If the primary is the only one left and the MAC supports
330111878SVenu.Iyer@Sun.COM 	 * dynamic grouping, we need to see if the primary needs to
330211878SVenu.Iyer@Sun.COM 	 * be moved to the default group so that it can use all the
330311878SVenu.Iyer@Sun.COM 	 * H/W rings.
330411878SVenu.Iyer@Sun.COM 	 */
330511878SVenu.Iyer@Sun.COM 	if (!(flent->fe_type & FLOW_PRIMARY_MAC) &&
330611878SVenu.Iyer@Sun.COM 	    mip->mi_nactiveclients == 1 &&
330711878SVenu.Iyer@Sun.COM 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
330811878SVenu.Iyer@Sun.COM 		default_group = MAC_DEFAULT_RX_GROUP(mip);
330911878SVenu.Iyer@Sun.COM 		grp_only_mcip = mac_primary_client_handle(mip);
331011878SVenu.Iyer@Sun.COM 		if (grp_only_mcip == NULL)
331111878SVenu.Iyer@Sun.COM 			return;
331211878SVenu.Iyer@Sun.COM 		group_only_flent = grp_only_mcip->mci_flent;
331311878SVenu.Iyer@Sun.COM 		mrp = MCIP_RESOURCE_PROPS(grp_only_mcip);
331411878SVenu.Iyer@Sun.COM 		/*
331511878SVenu.Iyer@Sun.COM 		 * If the primary has an explicit property set, leave it
331611878SVenu.Iyer@Sun.COM 		 * alone.
331711878SVenu.Iyer@Sun.COM 		 */
331811878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RX_RINGS)
331911878SVenu.Iyer@Sun.COM 			return;
332011878SVenu.Iyer@Sun.COM 		/*
332111878SVenu.Iyer@Sun.COM 		 * Switch the primary to the default group.
332211878SVenu.Iyer@Sun.COM 		 */
332311878SVenu.Iyer@Sun.COM 		(void) mac_rx_switch_group(grp_only_mcip,
332411878SVenu.Iyer@Sun.COM 		    group_only_flent->fe_rx_ring_group, default_group);
332511878SVenu.Iyer@Sun.COM 	}
33268275SEric Cheng }
33278275SEric Cheng 
33288275SEric Cheng /* DATAPATH TEAR DOWN ROUTINES (SRS and FANOUT teardown) */
33298275SEric Cheng 
33308275SEric Cheng static void
mac_srs_fanout_list_free(mac_soft_ring_set_t * mac_srs)33318275SEric Cheng mac_srs_fanout_list_free(mac_soft_ring_set_t *mac_srs)
33328275SEric Cheng {
333311878SVenu.Iyer@Sun.COM 	if (mac_srs->srs_type & SRST_TX) {
333411878SVenu.Iyer@Sun.COM 		mac_srs_tx_t *tx;
333511878SVenu.Iyer@Sun.COM 
333611878SVenu.Iyer@Sun.COM 		ASSERT(mac_srs->srs_tcp_soft_rings == NULL);
333711878SVenu.Iyer@Sun.COM 		ASSERT(mac_srs->srs_udp_soft_rings == NULL);
333811878SVenu.Iyer@Sun.COM 		ASSERT(mac_srs->srs_oth_soft_rings == NULL);
333911878SVenu.Iyer@Sun.COM 		ASSERT(mac_srs->srs_tx_soft_rings != NULL);
334011878SVenu.Iyer@Sun.COM 		kmem_free(mac_srs->srs_tx_soft_rings,
334111878SVenu.Iyer@Sun.COM 		    sizeof (mac_soft_ring_t *) * MAX_RINGS_PER_GROUP);
334211878SVenu.Iyer@Sun.COM 		mac_srs->srs_tx_soft_rings = NULL;
334311878SVenu.Iyer@Sun.COM 		tx = &mac_srs->srs_tx;
334411878SVenu.Iyer@Sun.COM 		if (tx->st_soft_rings != NULL) {
334511878SVenu.Iyer@Sun.COM 			kmem_free(tx->st_soft_rings,
334611878SVenu.Iyer@Sun.COM 			    sizeof (mac_soft_ring_t *) * MAX_RINGS_PER_GROUP);
334711878SVenu.Iyer@Sun.COM 		}
334811878SVenu.Iyer@Sun.COM 	} else {
334911878SVenu.Iyer@Sun.COM 		ASSERT(mac_srs->srs_tx_soft_rings == NULL);
335011878SVenu.Iyer@Sun.COM 		ASSERT(mac_srs->srs_tcp_soft_rings != NULL);
335111878SVenu.Iyer@Sun.COM 		kmem_free(mac_srs->srs_tcp_soft_rings,
335211878SVenu.Iyer@Sun.COM 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
335311878SVenu.Iyer@Sun.COM 		mac_srs->srs_tcp_soft_rings = NULL;
335411878SVenu.Iyer@Sun.COM 		ASSERT(mac_srs->srs_udp_soft_rings != NULL);
335511878SVenu.Iyer@Sun.COM 		kmem_free(mac_srs->srs_udp_soft_rings,
335611878SVenu.Iyer@Sun.COM 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
335711878SVenu.Iyer@Sun.COM 		mac_srs->srs_udp_soft_rings = NULL;
335811878SVenu.Iyer@Sun.COM 		ASSERT(mac_srs->srs_oth_soft_rings != NULL);
335911878SVenu.Iyer@Sun.COM 		kmem_free(mac_srs->srs_oth_soft_rings,
336011878SVenu.Iyer@Sun.COM 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
336111878SVenu.Iyer@Sun.COM 		mac_srs->srs_oth_soft_rings = NULL;
336211878SVenu.Iyer@Sun.COM 	}
33638275SEric Cheng }
33648275SEric Cheng 
33658275SEric Cheng /*
33668275SEric Cheng  * An RX SRS is attached to at most one mac_ring.
33678275SEric Cheng  * A TX SRS  has no  rings.
33688275SEric Cheng  */
33698275SEric Cheng static void
mac_srs_ring_free(mac_soft_ring_set_t * mac_srs)33708275SEric Cheng mac_srs_ring_free(mac_soft_ring_set_t *mac_srs)
33718275SEric Cheng {
33728275SEric Cheng 	mac_client_impl_t	*mcip;
33738275SEric Cheng 	mac_ring_t		*ring;
33748275SEric Cheng 	flow_entry_t		*flent;
33758275SEric Cheng 
33768275SEric Cheng 	ring = mac_srs->srs_ring;
33778275SEric Cheng 	if (mac_srs->srs_type & SRST_TX) {
33788275SEric Cheng 		ASSERT(ring == NULL);
33798275SEric Cheng 		return;
33808275SEric Cheng 	}
33818275SEric Cheng 
33828275SEric Cheng 	if (ring == NULL)
33838275SEric Cheng 		return;
33848275SEric Cheng 
33858275SEric Cheng 	/*
33868275SEric Cheng 	 * Broadcast flows don't have a client impl association, but they
33878275SEric Cheng 	 * use only soft rings.
33888275SEric Cheng 	 */
33898275SEric Cheng 	flent = mac_srs->srs_flent;
33908275SEric Cheng 	mcip = flent->fe_mcip;
33918275SEric Cheng 	ASSERT(mcip != NULL);
33928275SEric Cheng 
33938275SEric Cheng 	ring->mr_classify_type = MAC_NO_CLASSIFIER;
33948275SEric Cheng 	ring->mr_srs = NULL;
33958275SEric Cheng }
33968275SEric Cheng 
33978275SEric Cheng /*
33988275SEric Cheng  * Physical unlink and free of the data structures happen below. This is
33998275SEric Cheng  * driven from mac_flow_destroy(), on the last refrele of a flow.
34008275SEric Cheng  *
34018275SEric Cheng  * Assumes Rx srs is 1-1 mapped with an ring.
34028275SEric Cheng  */
34038275SEric Cheng void
mac_srs_free(mac_soft_ring_set_t * mac_srs)34048275SEric Cheng mac_srs_free(mac_soft_ring_set_t *mac_srs)
34058275SEric Cheng {
34068275SEric Cheng 	ASSERT(mac_srs->srs_mcip == NULL ||
34078275SEric Cheng 	    MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
34088275SEric Cheng 	ASSERT((mac_srs->srs_state & (SRS_CONDEMNED | SRS_CONDEMNED_DONE |
34098275SEric Cheng 	    SRS_PROC | SRS_PROC_FAST)) == (SRS_CONDEMNED | SRS_CONDEMNED_DONE));
34108275SEric Cheng 
34118275SEric Cheng 	mac_pkt_drop(NULL, NULL, mac_srs->srs_first, B_FALSE);
34128275SEric Cheng 	mac_srs_ring_free(mac_srs);
341311878SVenu.Iyer@Sun.COM 	mac_srs_soft_rings_free(mac_srs);
34148275SEric Cheng 	mac_srs_fanout_list_free(mac_srs);
34158275SEric Cheng 
34168275SEric Cheng 	mac_srs->srs_bw = NULL;
341711878SVenu.Iyer@Sun.COM 	mac_srs_stat_delete(mac_srs);
34188275SEric Cheng 	kmem_cache_free(mac_srs_cache, mac_srs);
34198275SEric Cheng }
34208275SEric Cheng 
34218275SEric Cheng static void
mac_srs_soft_rings_quiesce(mac_soft_ring_set_t * mac_srs,uint_t s_ring_flag)34228275SEric Cheng mac_srs_soft_rings_quiesce(mac_soft_ring_set_t *mac_srs, uint_t s_ring_flag)
34238275SEric Cheng {
34248275SEric Cheng 	mac_soft_ring_t	*softring;
34258275SEric Cheng 
34268275SEric Cheng 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
34278275SEric Cheng 
34288275SEric Cheng 	mac_srs_soft_rings_signal(mac_srs, s_ring_flag);
34298275SEric Cheng 	if (s_ring_flag == S_RING_CONDEMNED) {
34308275SEric Cheng 		while (mac_srs->srs_soft_ring_condemned_count !=
34318275SEric Cheng 		    mac_srs->srs_soft_ring_count)
34328275SEric Cheng 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
34338275SEric Cheng 	} else {
34348275SEric Cheng 		while (mac_srs->srs_soft_ring_quiesced_count !=
34358275SEric Cheng 		    mac_srs->srs_soft_ring_count)
34368275SEric Cheng 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
34378275SEric Cheng 	}
34388275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
34398275SEric Cheng 
34408275SEric Cheng 	for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
34418275SEric Cheng 	    softring = softring->s_ring_next)
34428275SEric Cheng 		(void) untimeout(softring->s_ring_tid);
34438275SEric Cheng 
34448275SEric Cheng 	(void) untimeout(mac_srs->srs_tid);
34458275SEric Cheng 
34468275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
34478275SEric Cheng }
34488275SEric Cheng 
34498275SEric Cheng /*
34508275SEric Cheng  * The block comment above mac_rx_classify_flow_state_change explains the
34518275SEric Cheng  * background. At this point upcalls from the driver (both hardware classified
34528275SEric Cheng  * and software classified) have been cut off. We now need to quiesce the
34538275SEric Cheng  * SRS worker, poll, and softring threads. The SRS worker thread serves as
34548275SEric Cheng  * the master controller. The steps involved are described below in the function
34558275SEric Cheng  */
34568275SEric Cheng void
mac_srs_worker_quiesce(mac_soft_ring_set_t * mac_srs)34578275SEric Cheng mac_srs_worker_quiesce(mac_soft_ring_set_t *mac_srs)
34588275SEric Cheng {
34598275SEric Cheng 	uint_t			s_ring_flag;
34608275SEric Cheng 	uint_t			srs_poll_wait_flag;
34618275SEric Cheng 
34628275SEric Cheng 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
34638275SEric Cheng 	ASSERT(mac_srs->srs_state & (SRS_CONDEMNED | SRS_QUIESCE));
34648275SEric Cheng 
34658275SEric Cheng 	if (mac_srs->srs_state & SRS_CONDEMNED) {
34668275SEric Cheng 		s_ring_flag = S_RING_CONDEMNED;
34678275SEric Cheng 		srs_poll_wait_flag = SRS_POLL_THR_EXITED;
34688275SEric Cheng 	} else {
34698275SEric Cheng 		s_ring_flag = S_RING_QUIESCE;
34708275SEric Cheng 		srs_poll_wait_flag = SRS_POLL_THR_QUIESCED;
34718275SEric Cheng 	}
34728275SEric Cheng 
34738275SEric Cheng 	/*
34748275SEric Cheng 	 * In the case of Rx SRS wait till the poll thread is done.
34758275SEric Cheng 	 */
34768275SEric Cheng 	if ((mac_srs->srs_type & SRST_TX) == 0 &&
34778275SEric Cheng 	    mac_srs->srs_poll_thr != NULL) {
34788275SEric Cheng 		while (!(mac_srs->srs_state & srs_poll_wait_flag))
34798275SEric Cheng 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
34808275SEric Cheng 
34818275SEric Cheng 		/*
34828275SEric Cheng 		 * Turn off polling as part of the quiesce operation.
34838275SEric Cheng 		 */
34848275SEric Cheng 		MAC_SRS_POLLING_OFF(mac_srs);
34858275SEric Cheng 		mac_srs->srs_state &= ~(SRS_POLLING | SRS_GET_PKTS);
34868275SEric Cheng 	}
34878275SEric Cheng 
34888275SEric Cheng 	/*
34898275SEric Cheng 	 * Then signal the soft ring worker threads to quiesce or quit
34908275SEric Cheng 	 * as needed and then wait till that happens.
34918275SEric Cheng 	 */
34928275SEric Cheng 	mac_srs_soft_rings_quiesce(mac_srs, s_ring_flag);
34938275SEric Cheng 
34948275SEric Cheng 	if (mac_srs->srs_state & SRS_CONDEMNED)
34958275SEric Cheng 		mac_srs->srs_state |= (SRS_QUIESCE_DONE | SRS_CONDEMNED_DONE);
34968275SEric Cheng 	else
34978275SEric Cheng 		mac_srs->srs_state |= SRS_QUIESCE_DONE;
34988275SEric Cheng 	cv_signal(&mac_srs->srs_quiesce_done_cv);
34998275SEric Cheng }
35008275SEric Cheng 
35018275SEric Cheng /*
35028275SEric Cheng  * Signal an SRS to start a temporary quiesce, or permanent removal, or restart
35038275SEric Cheng  * a quiesced SRS by setting the appropriate flags and signaling the SRS worker
35048275SEric Cheng  * or poll thread. This function is internal to the quiescing logic and is
35058275SEric Cheng  * called internally from the SRS quiesce or flow quiesce or client quiesce
35068275SEric Cheng  * higher level functions.
35078275SEric Cheng  */
35088275SEric Cheng void
mac_srs_signal(mac_soft_ring_set_t * mac_srs,uint_t srs_flag)35098275SEric Cheng mac_srs_signal(mac_soft_ring_set_t *mac_srs, uint_t srs_flag)
35108275SEric Cheng {
35118275SEric Cheng 	mac_ring_t	*ring;
35128275SEric Cheng 
35138275SEric Cheng 	ring = mac_srs->srs_ring;
35148275SEric Cheng 	ASSERT(ring == NULL || ring->mr_refcnt == 0);
35158275SEric Cheng 
35168275SEric Cheng 	if (srs_flag == SRS_CONDEMNED) {
35178275SEric Cheng 		/*
35188275SEric Cheng 		 * The SRS is going away. We need to unbind the SRS and SR
35198275SEric Cheng 		 * threads before removing from the global SRS list. Otherwise
35208275SEric Cheng 		 * there is a small window where the cpu reconfig callbacks
35218275SEric Cheng 		 * may miss the SRS in the list walk and DR could fail since
35228275SEric Cheng 		 * there are still bound threads.
35238275SEric Cheng 		 */
35248275SEric Cheng 		mac_srs_threads_unbind(mac_srs);
35258275SEric Cheng 		mac_srs_remove_glist(mac_srs);
35268275SEric Cheng 	}
35278275SEric Cheng 	/*
35288275SEric Cheng 	 * Wakeup the SRS worker and poll threads.
35298275SEric Cheng 	 */
35308275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
35318275SEric Cheng 	mac_srs->srs_state |= srs_flag;
35328275SEric Cheng 	cv_signal(&mac_srs->srs_async);
35338275SEric Cheng 	cv_signal(&mac_srs->srs_cv);
35348275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
35358275SEric Cheng }
35368275SEric Cheng 
35378275SEric Cheng /*
35388275SEric Cheng  * In the Rx side, the quiescing is done bottom up. After the Rx upcalls
35398275SEric Cheng  * from the driver are done, then the Rx SRS is quiesced and only then can
35408275SEric Cheng  * we signal the soft rings. Thus this function can't be called arbitrarily
35418275SEric Cheng  * without satisfying the prerequisites. On the Tx side, the threads from
35428275SEric Cheng  * top need to quiesced, then the Tx SRS and only then can we signal the
35438275SEric Cheng  * Tx soft rings.
35448275SEric Cheng  */
35458275SEric Cheng static void
mac_srs_soft_rings_signal(mac_soft_ring_set_t * mac_srs,uint_t sr_flag)35468275SEric Cheng mac_srs_soft_rings_signal(mac_soft_ring_set_t *mac_srs, uint_t sr_flag)
35478275SEric Cheng {
35488275SEric Cheng 	mac_soft_ring_t		*softring;
35498275SEric Cheng 
35508275SEric Cheng 	for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
35518275SEric Cheng 	    softring = softring->s_ring_next)
35528275SEric Cheng 		mac_soft_ring_signal(softring, sr_flag);
35538275SEric Cheng }
35548275SEric Cheng 
35558275SEric Cheng /*
35568275SEric Cheng  * The block comment above mac_rx_classify_flow_state_change explains the
35578275SEric Cheng  * background. At this point the SRS is quiesced and we need to restart the
35588275SEric Cheng  * SRS worker, poll, and softring threads. The SRS worker thread serves as
35598275SEric Cheng  * the master controller. The steps involved are described below in the function
35608275SEric Cheng  */
35618275SEric Cheng void
mac_srs_worker_restart(mac_soft_ring_set_t * mac_srs)35628275SEric Cheng mac_srs_worker_restart(mac_soft_ring_set_t *mac_srs)
35638275SEric Cheng {
35648275SEric Cheng 	boolean_t	iam_rx_srs;
35658275SEric Cheng 	mac_soft_ring_t	*softring;
35668275SEric Cheng 
35678275SEric Cheng 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
35688275SEric Cheng 	if ((mac_srs->srs_type & SRST_TX) != 0) {
35698275SEric Cheng 		iam_rx_srs = B_FALSE;
35708275SEric Cheng 		ASSERT((mac_srs->srs_state &
35718275SEric Cheng 		    (SRS_POLL_THR_QUIESCED | SRS_QUIESCE_DONE | SRS_QUIESCE)) ==
35728275SEric Cheng 		    (SRS_QUIESCE_DONE | SRS_QUIESCE));
35738275SEric Cheng 	} else {
35748275SEric Cheng 		iam_rx_srs = B_TRUE;
35758275SEric Cheng 		ASSERT((mac_srs->srs_state &
35768275SEric Cheng 		    (SRS_QUIESCE_DONE | SRS_QUIESCE)) ==
35778275SEric Cheng 		    (SRS_QUIESCE_DONE | SRS_QUIESCE));
35788275SEric Cheng 		if (mac_srs->srs_poll_thr != NULL) {
35798275SEric Cheng 			ASSERT((mac_srs->srs_state & SRS_POLL_THR_QUIESCED) ==
35808275SEric Cheng 			    SRS_POLL_THR_QUIESCED);
35818275SEric Cheng 		}
35828275SEric Cheng 	}
35838275SEric Cheng 
35848275SEric Cheng 	/*
35858275SEric Cheng 	 * Signal any quiesced soft ring workers to restart and wait for the
35868275SEric Cheng 	 * soft ring down count to come down to zero.
35878275SEric Cheng 	 */
35888275SEric Cheng 	if (mac_srs->srs_soft_ring_quiesced_count != 0) {
35898275SEric Cheng 		for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
35908275SEric Cheng 		    softring = softring->s_ring_next) {
35918275SEric Cheng 			if (!(softring->s_ring_state & S_RING_QUIESCE))
35928275SEric Cheng 				continue;
35938275SEric Cheng 			mac_soft_ring_signal(softring, S_RING_RESTART);
35948275SEric Cheng 		}
35958275SEric Cheng 		while (mac_srs->srs_soft_ring_quiesced_count != 0)
35968275SEric Cheng 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
35978275SEric Cheng 	}
35988275SEric Cheng 
35998275SEric Cheng 	mac_srs->srs_state &= ~(SRS_QUIESCE_DONE | SRS_QUIESCE | SRS_RESTART);
36008275SEric Cheng 	if (iam_rx_srs && mac_srs->srs_poll_thr != NULL) {
36018275SEric Cheng 		/*
36028275SEric Cheng 		 * Signal the poll thread and ask it to restart. Wait till it
36038275SEric Cheng 		 * actually restarts and the SRS_POLL_THR_QUIESCED flag gets
36048275SEric Cheng 		 * cleared.
36058275SEric Cheng 		 */
36068275SEric Cheng 		mac_srs->srs_state |= SRS_POLL_THR_RESTART;
36078275SEric Cheng 		cv_signal(&mac_srs->srs_cv);
36088275SEric Cheng 		while (mac_srs->srs_state & SRS_POLL_THR_QUIESCED)
36098275SEric Cheng 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
36108275SEric Cheng 		ASSERT(!(mac_srs->srs_state & SRS_POLL_THR_RESTART));
36118275SEric Cheng 	}
36128275SEric Cheng 	/* Wake up any waiter waiting for the restart to complete */
36138275SEric Cheng 	mac_srs->srs_state |= SRS_RESTART_DONE;
36148275SEric Cheng 	cv_signal(&mac_srs->srs_quiesce_done_cv);
36158275SEric Cheng }
36168275SEric Cheng 
36178275SEric Cheng static void
mac_srs_worker_unbind(mac_soft_ring_set_t * mac_srs)36188275SEric Cheng mac_srs_worker_unbind(mac_soft_ring_set_t *mac_srs)
36198275SEric Cheng {
36208275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
36218275SEric Cheng 	if (!(mac_srs->srs_state & SRS_WORKER_BOUND)) {
36228275SEric Cheng 		ASSERT(mac_srs->srs_worker_cpuid == -1);
36238275SEric Cheng 		mutex_exit(&mac_srs->srs_lock);
36248275SEric Cheng 		return;
36258275SEric Cheng 	}
36268275SEric Cheng 
36278275SEric Cheng 	mac_srs->srs_worker_cpuid = -1;
36288275SEric Cheng 	mac_srs->srs_state &= ~SRS_WORKER_BOUND;
36298275SEric Cheng 	thread_affinity_clear(mac_srs->srs_worker);
36308275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
36318275SEric Cheng }
36328275SEric Cheng 
36338275SEric Cheng static void
mac_srs_poll_unbind(mac_soft_ring_set_t * mac_srs)36348275SEric Cheng mac_srs_poll_unbind(mac_soft_ring_set_t *mac_srs)
36358275SEric Cheng {
36368275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
36378275SEric Cheng 	if (mac_srs->srs_poll_thr == NULL ||
36388275SEric Cheng 	    (mac_srs->srs_state & SRS_POLL_BOUND) == 0) {
36398275SEric Cheng 		ASSERT(mac_srs->srs_poll_cpuid == -1);
36408275SEric Cheng 		mutex_exit(&mac_srs->srs_lock);
36418275SEric Cheng 		return;
36428275SEric Cheng 	}
36438275SEric Cheng 
36448275SEric Cheng 	mac_srs->srs_poll_cpuid = -1;
36458275SEric Cheng 	mac_srs->srs_state &= ~SRS_POLL_BOUND;
36468275SEric Cheng 	thread_affinity_clear(mac_srs->srs_poll_thr);
36478275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
36488275SEric Cheng }
36498275SEric Cheng 
36508275SEric Cheng static void
mac_srs_threads_unbind(mac_soft_ring_set_t * mac_srs)36518275SEric Cheng mac_srs_threads_unbind(mac_soft_ring_set_t *mac_srs)
36528275SEric Cheng {
36538275SEric Cheng 	mac_soft_ring_t	*soft_ring;
36548275SEric Cheng 
36558275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
36568275SEric Cheng 
36578275SEric Cheng 	mutex_enter(&cpu_lock);
36588275SEric Cheng 	mac_srs_worker_unbind(mac_srs);
36598275SEric Cheng 	if (!(mac_srs->srs_type & SRST_TX))
36608275SEric Cheng 		mac_srs_poll_unbind(mac_srs);
36618275SEric Cheng 
36628275SEric Cheng 	for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
36638275SEric Cheng 	    soft_ring = soft_ring->s_ring_next) {
36648275SEric Cheng 		mac_soft_ring_unbind(soft_ring);
36658275SEric Cheng 	}
36668275SEric Cheng 	mutex_exit(&cpu_lock);
36678275SEric Cheng }
36688275SEric Cheng 
36698275SEric Cheng /*
36708275SEric Cheng  * When a CPU is going away, unbind all MAC threads which are bound
36718275SEric Cheng  * to that CPU. The affinity of the thread to the CPU is saved to allow
36728275SEric Cheng  * the thread to be rebound to the CPU if it comes back online.
36738275SEric Cheng  */
36748275SEric Cheng static void
mac_walk_srs_and_unbind(int cpuid)36758275SEric Cheng mac_walk_srs_and_unbind(int cpuid)
36768275SEric Cheng {
36778275SEric Cheng 	mac_soft_ring_set_t *mac_srs;
36788275SEric Cheng 	mac_soft_ring_t *soft_ring;
36798275SEric Cheng 
36808275SEric Cheng 	rw_enter(&mac_srs_g_lock, RW_READER);
36818275SEric Cheng 
36828275SEric Cheng 	if ((mac_srs = mac_srs_g_list) == NULL)
36838275SEric Cheng 		goto done;
36848275SEric Cheng 
36858275SEric Cheng 	for (; mac_srs != NULL; mac_srs = mac_srs->srs_next) {
36868275SEric Cheng 		if (mac_srs->srs_worker_cpuid == cpuid) {
36878275SEric Cheng 			mac_srs->srs_worker_cpuid_save = cpuid;
36888275SEric Cheng 			mac_srs_worker_unbind(mac_srs);
36898275SEric Cheng 		}
36908275SEric Cheng 
36918275SEric Cheng 		if (!(mac_srs->srs_type & SRST_TX)) {
36928275SEric Cheng 			if (mac_srs->srs_poll_cpuid == cpuid) {
36938275SEric Cheng 				mac_srs->srs_poll_cpuid_save = cpuid;
36948275SEric Cheng 				mac_srs_poll_unbind(mac_srs);
36958275SEric Cheng 			}
36968275SEric Cheng 		}
36978275SEric Cheng 
36988275SEric Cheng 		/* Next tackle the soft rings associated with the srs */
36998275SEric Cheng 		mutex_enter(&mac_srs->srs_lock);
37008275SEric Cheng 		for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
37018275SEric Cheng 		    soft_ring = soft_ring->s_ring_next) {
37028275SEric Cheng 			if (soft_ring->s_ring_cpuid == cpuid) {
37038275SEric Cheng 				soft_ring->s_ring_cpuid_save = cpuid;
37048275SEric Cheng 				mac_soft_ring_unbind(soft_ring);
37058275SEric Cheng 			}
37068275SEric Cheng 		}
37078275SEric Cheng 		mutex_exit(&mac_srs->srs_lock);
37088275SEric Cheng 	}
37098275SEric Cheng done:
37108275SEric Cheng 	rw_exit(&mac_srs_g_lock);
37118275SEric Cheng }
37128275SEric Cheng 
37138275SEric Cheng /* TX SETUP and TEARDOWN ROUTINES */
37148275SEric Cheng 
37158275SEric Cheng /*
37168275SEric Cheng  * XXXHIO need to make sure the two mac_tx_srs_{add,del}_ring()
37178275SEric Cheng  * handle the case where the number of rings is one. I.e. there is
37188275SEric Cheng  * a ring pointed to by mac_srs->srs_tx_arg2.
37198275SEric Cheng  */
37208275SEric Cheng void
mac_tx_srs_add_ring(mac_soft_ring_set_t * mac_srs,mac_ring_t * tx_ring)37218275SEric Cheng mac_tx_srs_add_ring(mac_soft_ring_set_t *mac_srs, mac_ring_t *tx_ring)
37228275SEric Cheng {
37238275SEric Cheng 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
37248275SEric Cheng 	mac_soft_ring_t *soft_ring;
372511878SVenu.Iyer@Sun.COM 	int count = mac_srs->srs_tx_ring_count;
372611878SVenu.Iyer@Sun.COM 	uint32_t soft_ring_type = ST_RING_TX;
372711878SVenu.Iyer@Sun.COM 	uint_t ring_info;
37288275SEric Cheng 
37298275SEric Cheng 	ASSERT(mac_srs->srs_state & SRS_QUIESCE);
373011878SVenu.Iyer@Sun.COM 	ring_info = mac_hwring_getinfo((mac_ring_handle_t)tx_ring);
373111878SVenu.Iyer@Sun.COM 	if (mac_tx_serialize || (ring_info & MAC_RING_TX_SERIALIZE))
373211878SVenu.Iyer@Sun.COM 		soft_ring_type |= ST_RING_WORKER_ONLY;
373311878SVenu.Iyer@Sun.COM 	soft_ring = mac_soft_ring_create(count, 0,
373411878SVenu.Iyer@Sun.COM 	    soft_ring_type, maxclsyspri, mcip, mac_srs, -1,
37358275SEric Cheng 	    NULL, mcip, (mac_resource_handle_t)tx_ring);
373611878SVenu.Iyer@Sun.COM 	mac_srs->srs_tx_ring_count++;
373711878SVenu.Iyer@Sun.COM 	mac_srs_update_fanout_list(mac_srs);
37388275SEric Cheng 	/*
37398275SEric Cheng 	 * put this soft ring in quiesce mode too so when we restart
37408275SEric Cheng 	 * all soft rings in the srs are in the same state.
37418275SEric Cheng 	 */
37428275SEric Cheng 	mac_soft_ring_signal(soft_ring, S_RING_QUIESCE);
37438275SEric Cheng }
37448275SEric Cheng 
37458275SEric Cheng static void
mac_soft_ring_remove(mac_soft_ring_set_t * mac_srs,mac_soft_ring_t * softring)37468275SEric Cheng mac_soft_ring_remove(mac_soft_ring_set_t *mac_srs, mac_soft_ring_t *softring)
37478275SEric Cheng {
37488275SEric Cheng 	int sringcnt;
37498275SEric Cheng 
37508275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
37518275SEric Cheng 	sringcnt = mac_srs->srs_soft_ring_count;
37528275SEric Cheng 	ASSERT(sringcnt > 0);
37538275SEric Cheng 	mac_soft_ring_signal(softring, S_RING_CONDEMNED);
37548275SEric Cheng 
37558275SEric Cheng 	ASSERT(mac_srs->srs_soft_ring_condemned_count == 0);
37568275SEric Cheng 	while (mac_srs->srs_soft_ring_condemned_count != 1)
37578275SEric Cheng 		cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
37588275SEric Cheng 
37598275SEric Cheng 	if (softring == mac_srs->srs_soft_ring_head) {
37608275SEric Cheng 		mac_srs->srs_soft_ring_head = softring->s_ring_next;
37618275SEric Cheng 		if (mac_srs->srs_soft_ring_head != NULL) {
37628275SEric Cheng 			mac_srs->srs_soft_ring_head->s_ring_prev = NULL;
37638275SEric Cheng 		} else {
37648275SEric Cheng 			mac_srs->srs_soft_ring_tail = NULL;
37658275SEric Cheng 		}
37668275SEric Cheng 	} else {
37678275SEric Cheng 		softring->s_ring_prev->s_ring_next =
37688275SEric Cheng 		    softring->s_ring_next;
37698275SEric Cheng 		if (softring->s_ring_next != NULL) {
37708275SEric Cheng 			softring->s_ring_next->s_ring_prev =
37718275SEric Cheng 			    softring->s_ring_prev;
37728275SEric Cheng 		} else {
37738275SEric Cheng 			mac_srs->srs_soft_ring_tail =
37748275SEric Cheng 			    softring->s_ring_prev;
37758275SEric Cheng 		}
37768275SEric Cheng 	}
37778275SEric Cheng 	mac_srs->srs_soft_ring_count--;
37788275SEric Cheng 
37798275SEric Cheng 	mac_srs->srs_soft_ring_condemned_count--;
37808275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
37818275SEric Cheng 
378211878SVenu.Iyer@Sun.COM 	mac_soft_ring_free(softring);
37838275SEric Cheng }
37848275SEric Cheng 
37858275SEric Cheng void
mac_tx_srs_del_ring(mac_soft_ring_set_t * mac_srs,mac_ring_t * tx_ring)37868275SEric Cheng mac_tx_srs_del_ring(mac_soft_ring_set_t *mac_srs, mac_ring_t *tx_ring)
37878275SEric Cheng {
37888275SEric Cheng 	int i;
37898275SEric Cheng 	mac_soft_ring_t *soft_ring, *remove_sring;
379011878SVenu.Iyer@Sun.COM 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
37918275SEric Cheng 
37928275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
379311878SVenu.Iyer@Sun.COM 	for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
379411878SVenu.Iyer@Sun.COM 		soft_ring =  mac_srs->srs_tx_soft_rings[i];
37958275SEric Cheng 		if (soft_ring->s_ring_tx_arg2 == tx_ring)
37968275SEric Cheng 			break;
37978275SEric Cheng 	}
37988275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
379911878SVenu.Iyer@Sun.COM 	ASSERT(i < mac_srs->srs_tx_ring_count);
38008275SEric Cheng 	remove_sring = soft_ring;
380111878SVenu.Iyer@Sun.COM 	/*
380211878SVenu.Iyer@Sun.COM 	 * In the case of aggr, the soft ring associated with a Tx ring
380311878SVenu.Iyer@Sun.COM 	 * is also stored in st_soft_rings[] array. That entry should
380411878SVenu.Iyer@Sun.COM 	 * be removed.
380511878SVenu.Iyer@Sun.COM 	 */
380611878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_IS_AGGR) {
380711878SVenu.Iyer@Sun.COM 		mac_srs_tx_t *tx = &mac_srs->srs_tx;
380811878SVenu.Iyer@Sun.COM 
380911878SVenu.Iyer@Sun.COM 		ASSERT(tx->st_soft_rings[tx_ring->mr_index] == remove_sring);
381011878SVenu.Iyer@Sun.COM 		tx->st_soft_rings[tx_ring->mr_index] = NULL;
381111878SVenu.Iyer@Sun.COM 	}
38128275SEric Cheng 	mac_soft_ring_remove(mac_srs, remove_sring);
38138275SEric Cheng 	mac_srs_update_fanout_list(mac_srs);
38148275SEric Cheng }
38158275SEric Cheng 
38168275SEric Cheng /*
38178275SEric Cheng  * mac_tx_srs_setup():
38188275SEric Cheng  * Used to setup Tx rings. If no free Tx ring is available, then default
38198275SEric Cheng  * Tx ring is used.
38208275SEric Cheng  */
38218275SEric Cheng void
mac_tx_srs_setup(mac_client_impl_t * mcip,flow_entry_t * flent)382211878SVenu.Iyer@Sun.COM mac_tx_srs_setup(mac_client_impl_t *mcip, flow_entry_t *flent)
38238275SEric Cheng {
382411878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
382511878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t	*tx_srs = flent->fe_tx_srs;
382611878SVenu.Iyer@Sun.COM 	int			i;
382711878SVenu.Iyer@Sun.COM 	int			tx_ring_count = 0;
382811878SVenu.Iyer@Sun.COM 	uint32_t		soft_ring_type;
382911878SVenu.Iyer@Sun.COM 	mac_group_t		*grp = NULL;
383011878SVenu.Iyer@Sun.COM 	mac_ring_t		*ring;
383111878SVenu.Iyer@Sun.COM 	mac_srs_tx_t		*tx = &tx_srs->srs_tx;
383211878SVenu.Iyer@Sun.COM 	boolean_t		is_aggr;
383311878SVenu.Iyer@Sun.COM 	uint_t			ring_info = 0;
383411878SVenu.Iyer@Sun.COM 
383511878SVenu.Iyer@Sun.COM 	is_aggr = (mcip->mci_state_flags & MCIS_IS_AGGR) != 0;
383611878SVenu.Iyer@Sun.COM 	grp = flent->fe_tx_ring_group;
383711878SVenu.Iyer@Sun.COM 	if (grp == NULL) {
383811878SVenu.Iyer@Sun.COM 		ring = (mac_ring_t *)mip->mi_default_tx_ring;
383911878SVenu.Iyer@Sun.COM 		goto no_group;
38408275SEric Cheng 	}
384111878SVenu.Iyer@Sun.COM 	tx_ring_count = grp->mrg_cur_count;
384211878SVenu.Iyer@Sun.COM 	ring = grp->mrg_rings;
38438275SEric Cheng 	/*
38448275SEric Cheng 	 * An attempt is made to reserve 'tx_ring_count' number
38458275SEric Cheng 	 * of Tx rings. If tx_ring_count is 0, default Tx ring
38468275SEric Cheng 	 * is used. If it is 1, an attempt is made to reserve one
38478275SEric Cheng 	 * Tx ring. In both the cases, the ring information is
38488275SEric Cheng 	 * stored in Tx SRS. If multiple Tx rings are specified,
38498275SEric Cheng 	 * then each Tx ring will have a Tx-side soft ring. All
38508275SEric Cheng 	 * these soft rings will be hang off Tx SRS.
38518275SEric Cheng 	 */
385211878SVenu.Iyer@Sun.COM 	switch (grp->mrg_state) {
385311878SVenu.Iyer@Sun.COM 		case MAC_GROUP_STATE_SHARED:
385411878SVenu.Iyer@Sun.COM 		case MAC_GROUP_STATE_RESERVED:
385511878SVenu.Iyer@Sun.COM 			if (tx_ring_count <= 1 && !is_aggr) {
385611878SVenu.Iyer@Sun.COM no_group:
385711878SVenu.Iyer@Sun.COM 				if (ring != NULL &&
385811878SVenu.Iyer@Sun.COM 				    ring->mr_state != MR_INUSE) {
385911878SVenu.Iyer@Sun.COM 					(void) mac_start_ring(ring);
386011878SVenu.Iyer@Sun.COM 					ring_info = mac_hwring_getinfo(
386111878SVenu.Iyer@Sun.COM 					    (mac_ring_handle_t)ring);
386211878SVenu.Iyer@Sun.COM 				}
386311878SVenu.Iyer@Sun.COM 				tx->st_arg2 = (void *)ring;
386411878SVenu.Iyer@Sun.COM 				mac_tx_srs_stat_recreate(tx_srs, B_FALSE);
386511878SVenu.Iyer@Sun.COM 				if (tx_srs->srs_type & SRST_BW_CONTROL) {
386611878SVenu.Iyer@Sun.COM 					tx->st_mode = SRS_TX_BW;
386711878SVenu.Iyer@Sun.COM 				} else if (mac_tx_serialize ||
386811878SVenu.Iyer@Sun.COM 				    (ring_info & MAC_RING_TX_SERIALIZE)) {
386911878SVenu.Iyer@Sun.COM 					tx->st_mode = SRS_TX_SERIALIZE;
387011878SVenu.Iyer@Sun.COM 				} else {
387111878SVenu.Iyer@Sun.COM 					tx->st_mode = SRS_TX_DEFAULT;
387211878SVenu.Iyer@Sun.COM 				}
38738275SEric Cheng 				break;
387410309SSriharsha.Basavapatna@Sun.COM 			}
387511878SVenu.Iyer@Sun.COM 			soft_ring_type = ST_RING_TX;
387611878SVenu.Iyer@Sun.COM 			if (tx_srs->srs_type & SRST_BW_CONTROL) {
387711878SVenu.Iyer@Sun.COM 				tx->st_mode = is_aggr ?
387811878SVenu.Iyer@Sun.COM 				    SRS_TX_BW_AGGR : SRS_TX_BW_FANOUT;
387911878SVenu.Iyer@Sun.COM 			} else {
388011878SVenu.Iyer@Sun.COM 				tx->st_mode = is_aggr ? SRS_TX_AGGR :
388111878SVenu.Iyer@Sun.COM 				    SRS_TX_FANOUT;
388211878SVenu.Iyer@Sun.COM 			}
388311878SVenu.Iyer@Sun.COM 			for (i = 0; i < tx_ring_count; i++) {
388411878SVenu.Iyer@Sun.COM 				ASSERT(ring != NULL);
388511878SVenu.Iyer@Sun.COM 				switch (ring->mr_state) {
388611878SVenu.Iyer@Sun.COM 				case MR_INUSE:
388711878SVenu.Iyer@Sun.COM 				case MR_FREE:
388811878SVenu.Iyer@Sun.COM 					ASSERT(ring->mr_srs == NULL);
388911878SVenu.Iyer@Sun.COM 
389011878SVenu.Iyer@Sun.COM 					if (ring->mr_state != MR_INUSE)
389111878SVenu.Iyer@Sun.COM 						(void) mac_start_ring(ring);
389211878SVenu.Iyer@Sun.COM 					ring_info = mac_hwring_getinfo(
389311878SVenu.Iyer@Sun.COM 					    (mac_ring_handle_t)ring);
389411878SVenu.Iyer@Sun.COM 					if (mac_tx_serialize || (ring_info &
389511878SVenu.Iyer@Sun.COM 					    MAC_RING_TX_SERIALIZE)) {
389611878SVenu.Iyer@Sun.COM 						soft_ring_type |=
389711878SVenu.Iyer@Sun.COM 						    ST_RING_WORKER_ONLY;
389811878SVenu.Iyer@Sun.COM 					}
389911878SVenu.Iyer@Sun.COM 					(void) mac_soft_ring_create(i, 0,
390011878SVenu.Iyer@Sun.COM 					    soft_ring_type, maxclsyspri,
390111878SVenu.Iyer@Sun.COM 					    mcip, tx_srs, -1, NULL, mcip,
390211878SVenu.Iyer@Sun.COM 					    (mac_resource_handle_t)ring);
390311878SVenu.Iyer@Sun.COM 					break;
390411878SVenu.Iyer@Sun.COM 				default:
390511878SVenu.Iyer@Sun.COM 					cmn_err(CE_PANIC,
390611878SVenu.Iyer@Sun.COM 					    "srs_setup: mcip = %p "
390711878SVenu.Iyer@Sun.COM 					    "trying to add UNKNOWN ring = %p\n",
390811878SVenu.Iyer@Sun.COM 					    (void *)mcip, (void *)ring);
390911878SVenu.Iyer@Sun.COM 					break;
391011878SVenu.Iyer@Sun.COM 				}
391111878SVenu.Iyer@Sun.COM 				ring = ring->mr_next;
391211878SVenu.Iyer@Sun.COM 			}
391311878SVenu.Iyer@Sun.COM 			mac_srs_update_fanout_list(tx_srs);
391411878SVenu.Iyer@Sun.COM 			break;
391511878SVenu.Iyer@Sun.COM 		default:
391611878SVenu.Iyer@Sun.COM 			ASSERT(B_FALSE);
391711878SVenu.Iyer@Sun.COM 			break;
39188275SEric Cheng 	}
39198275SEric Cheng 	tx->st_func = mac_tx_get_func(tx->st_mode);
392011878SVenu.Iyer@Sun.COM 	if (is_aggr) {
392111878SVenu.Iyer@Sun.COM 		VERIFY(i_mac_capab_get((mac_handle_t)mip,
392211878SVenu.Iyer@Sun.COM 		    MAC_CAPAB_AGGR, &tx->st_capab_aggr));
392311878SVenu.Iyer@Sun.COM 	}
39248275SEric Cheng 	DTRACE_PROBE3(tx__srs___setup__return, mac_soft_ring_set_t *, tx_srs,
392511878SVenu.Iyer@Sun.COM 	    int, tx->st_mode, int, tx_srs->srs_tx_ring_count);
39268275SEric Cheng }
39278275SEric Cheng 
39288275SEric Cheng /*
39298400SNicolas.Droux@Sun.COM  * Update the fanout of a client if its recorded link speed doesn't match
39308400SNicolas.Droux@Sun.COM  * its current link speed.
39318400SNicolas.Droux@Sun.COM  */
39328400SNicolas.Droux@Sun.COM void
mac_fanout_recompute_client(mac_client_impl_t * mcip,cpupart_t * cpupart)393311878SVenu.Iyer@Sun.COM mac_fanout_recompute_client(mac_client_impl_t *mcip, cpupart_t *cpupart)
39348400SNicolas.Droux@Sun.COM {
39358400SNicolas.Droux@Sun.COM 	uint64_t link_speed;
39368400SNicolas.Droux@Sun.COM 	mac_resource_props_t *mcip_mrp;
393711878SVenu.Iyer@Sun.COM 	flow_entry_t *flent = mcip->mci_flent;
393811878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t *rx_srs;
393911878SVenu.Iyer@Sun.COM 	mac_cpus_t *srs_cpu;
394011878SVenu.Iyer@Sun.COM 	int soft_ring_count, maxcpus;
39418400SNicolas.Droux@Sun.COM 
39428400SNicolas.Droux@Sun.COM 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
39438400SNicolas.Droux@Sun.COM 
39448400SNicolas.Droux@Sun.COM 	link_speed = mac_client_stat_get(mcip->mci_flent->fe_mcip,
39458400SNicolas.Droux@Sun.COM 	    MAC_STAT_IFSPEED);
39468400SNicolas.Droux@Sun.COM 
39478400SNicolas.Droux@Sun.COM 	if ((link_speed != 0) &&
39488400SNicolas.Droux@Sun.COM 	    (link_speed != mcip->mci_flent->fe_nic_speed)) {
39498400SNicolas.Droux@Sun.COM 		mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
395011878SVenu.Iyer@Sun.COM 		/*
395111878SVenu.Iyer@Sun.COM 		 * Before calling mac_fanout_setup(), check to see if
395211878SVenu.Iyer@Sun.COM 		 * the SRSes already have the right number of soft
395311878SVenu.Iyer@Sun.COM 		 * rings. mac_fanout_setup() is a heavy duty operation
395411878SVenu.Iyer@Sun.COM 		 * where new cpu bindings are done for SRS and soft
395511878SVenu.Iyer@Sun.COM 		 * ring threads and interrupts re-targeted.
395611878SVenu.Iyer@Sun.COM 		 */
395711878SVenu.Iyer@Sun.COM 		maxcpus = (cpupart != NULL) ? cpupart->cp_ncpus : ncpus;
395811878SVenu.Iyer@Sun.COM 		soft_ring_count = mac_compute_soft_ring_count(flent,
395911878SVenu.Iyer@Sun.COM 		    flent->fe_rx_srs_cnt - 1, maxcpus);
396011878SVenu.Iyer@Sun.COM 		/*
396111878SVenu.Iyer@Sun.COM 		 * If soft_ring_count returned by
396211878SVenu.Iyer@Sun.COM 		 * mac_compute_soft_ring_count() is 0, bump it
396311878SVenu.Iyer@Sun.COM 		 * up by 1 because we always have atleast one
396411878SVenu.Iyer@Sun.COM 		 * TCP, UDP, and OTH soft ring associated with
396511878SVenu.Iyer@Sun.COM 		 * an SRS.
396611878SVenu.Iyer@Sun.COM 		 */
396711878SVenu.Iyer@Sun.COM 		soft_ring_count = (soft_ring_count == 0) ?
396811878SVenu.Iyer@Sun.COM 		    1 : soft_ring_count;
396911878SVenu.Iyer@Sun.COM 		rx_srs = flent->fe_rx_srs[0];
397011878SVenu.Iyer@Sun.COM 		srs_cpu = &rx_srs->srs_cpu;
397111878SVenu.Iyer@Sun.COM 		if (soft_ring_count != srs_cpu->mc_rx_fanout_cnt) {
397211878SVenu.Iyer@Sun.COM 			mac_fanout_setup(mcip, flent, mcip_mrp,
397311878SVenu.Iyer@Sun.COM 			    mac_rx_deliver, mcip, NULL, cpupart);
397411878SVenu.Iyer@Sun.COM 		}
39758400SNicolas.Droux@Sun.COM 	}
39768400SNicolas.Droux@Sun.COM }
39778400SNicolas.Droux@Sun.COM 
39788400SNicolas.Droux@Sun.COM /*
39798275SEric Cheng  * Walk through the list of mac clients for the MAC.
39808275SEric Cheng  * For each active mac client, recompute the number of soft rings
39818275SEric Cheng  * associated with every client, only if current speed is different
39828275SEric Cheng  * from the speed that was previously used for soft ring computation.
39838275SEric Cheng  * If the cable is disconnected whlie the NIC is started, we would get
39848275SEric Cheng  * notification with speed set to 0. We do not recompute in that case.
39858275SEric Cheng  */
39868275SEric Cheng void
mac_fanout_recompute(mac_impl_t * mip)39878275SEric Cheng mac_fanout_recompute(mac_impl_t *mip)
39888275SEric Cheng {
39898275SEric Cheng 	mac_client_impl_t	*mcip;
399011878SVenu.Iyer@Sun.COM 	cpupart_t		*cpupart;
399111878SVenu.Iyer@Sun.COM 	boolean_t		use_default;
399211878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp, *emrp;
39938400SNicolas.Droux@Sun.COM 
39948275SEric Cheng 	i_mac_perim_enter(mip);
39959052SEric Cheng 	if ((mip->mi_state_flags & MIS_IS_VNIC) != 0 ||
39969052SEric Cheng 	    mip->mi_linkstate != LINK_STATE_UP) {
39978275SEric Cheng 		i_mac_perim_exit(mip);
39988275SEric Cheng 		return;
39998275SEric Cheng 	}
40008275SEric Cheng 
40018275SEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
40028275SEric Cheng 	    mcip = mcip->mci_client_next) {
40038400SNicolas.Droux@Sun.COM 		if ((mcip->mci_state_flags & MCIS_SHARE_BOUND) != 0 ||
40048400SNicolas.Droux@Sun.COM 		    !MCIP_DATAPATH_SETUP(mcip))
40058275SEric Cheng 			continue;
400611878SVenu.Iyer@Sun.COM 		mrp = MCIP_RESOURCE_PROPS(mcip);
400711878SVenu.Iyer@Sun.COM 		emrp = MCIP_EFFECTIVE_PROPS(mcip);
400811878SVenu.Iyer@Sun.COM 		use_default = B_FALSE;
400911878SVenu.Iyer@Sun.COM 		pool_lock();
401011878SVenu.Iyer@Sun.COM 		cpupart = mac_pset_find(mrp, &use_default);
401111878SVenu.Iyer@Sun.COM 		mac_fanout_recompute_client(mcip, cpupart);
401211878SVenu.Iyer@Sun.COM 		mac_set_pool_effective(use_default, cpupart, mrp, emrp);
401311878SVenu.Iyer@Sun.COM 		pool_unlock();
40148275SEric Cheng 	}
40158275SEric Cheng 	i_mac_perim_exit(mip);
40168275SEric Cheng }
401710491SRishi.Srivatsavai@Sun.COM 
401810491SRishi.Srivatsavai@Sun.COM /*
401910491SRishi.Srivatsavai@Sun.COM  * Given a MAC, change the polling state for all its MAC clients.  'enable' is
402010491SRishi.Srivatsavai@Sun.COM  * B_TRUE to enable polling or B_FALSE to disable.  Polling is enabled by
402110491SRishi.Srivatsavai@Sun.COM  * default.
402210491SRishi.Srivatsavai@Sun.COM  */
402310491SRishi.Srivatsavai@Sun.COM void
mac_poll_state_change(mac_handle_t mh,boolean_t enable)402410491SRishi.Srivatsavai@Sun.COM mac_poll_state_change(mac_handle_t mh, boolean_t enable)
402510491SRishi.Srivatsavai@Sun.COM {
402610491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
402710491SRishi.Srivatsavai@Sun.COM 	mac_client_impl_t *mcip;
402810491SRishi.Srivatsavai@Sun.COM 
402910491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_enter(mip);
403010491SRishi.Srivatsavai@Sun.COM 	if (enable)
403110491SRishi.Srivatsavai@Sun.COM 		mip->mi_state_flags &= ~MIS_POLL_DISABLE;
403210491SRishi.Srivatsavai@Sun.COM 	else
403310491SRishi.Srivatsavai@Sun.COM 		mip->mi_state_flags |= MIS_POLL_DISABLE;
403410491SRishi.Srivatsavai@Sun.COM 	for (mcip = mip->mi_clients_list; mcip != NULL;
403510491SRishi.Srivatsavai@Sun.COM 	    mcip = mcip->mci_client_next)
403610491SRishi.Srivatsavai@Sun.COM 		mac_client_update_classifier(mcip, B_TRUE);
403710491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_exit(mip);
403810491SRishi.Srivatsavai@Sun.COM }
4039