xref: /illumos-gate/usr/src/uts/common/io/mac/mac_datapath_setup.c (revision 10936bbef9b4a6b108e670b5e53ff9def41839cc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2018 Joyent, Inc.
24  * Copyright 2020 RackTop Systems.
25  * Copyright 2026 Oxide Computer Company
26  */
27 
28 #include <sys/types.h>
29 #include <sys/callb.h>
30 #include <sys/cpupart.h>
31 #include <sys/pool.h>
32 #include <sys/pool_pset.h>
33 #include <sys/sdt.h>
34 #include <sys/strsubr.h>
35 #include <sys/strsun.h>
36 #include <sys/vlan.h>
37 #include <inet/ipsec_impl.h>
38 #include <inet/ip_impl.h>
39 #include <inet/sadb.h>
40 #include <inet/ipsecesp.h>
41 #include <inet/ipsecah.h>
42 
43 #include <sys/mac_impl.h>
44 #include <sys/mac_client_impl.h>
45 #include <sys/mac_client_priv.h>
46 #include <sys/mac_soft_ring.h>
47 #include <sys/mac_flow_impl.h>
48 #include <sys/mac_stat.h>
49 
50 static void mac_srs_soft_rings_signal(mac_soft_ring_set_t *, uint_t);
51 static void mac_srs_update_fanout_list(mac_soft_ring_set_t *);
52 static void mac_srs_poll_unbind(mac_soft_ring_set_t *);
53 static void mac_srs_worker_unbind(mac_soft_ring_set_t *);
54 static void mac_srs_soft_rings_quiesce(mac_soft_ring_set_t *, uint_t);
55 
56 static int mac_srs_cpu_setup(cpu_setup_t, int, void *);
57 static void mac_srs_worker_bind(mac_soft_ring_set_t *, processorid_t);
58 static void mac_srs_poll_bind(mac_soft_ring_set_t *, processorid_t);
59 static void mac_srs_threads_unbind(mac_soft_ring_set_t *);
60 static void mac_srs_add_glist(mac_soft_ring_set_t *);
61 static void mac_srs_remove_glist(mac_soft_ring_set_t *);
62 static void mac_srs_fanout_list_free(mac_soft_ring_set_t *);
63 static void mac_soft_ring_remove(mac_soft_ring_set_t *, mac_soft_ring_t *);
64 
65 static int mac_compute_soft_ring_count(flow_entry_t *, int, int);
66 static void mac_walk_srs_and_bind(int);
67 static void mac_walk_srs_and_unbind(int);
68 
69 extern boolean_t mac_latency_optimize;
70 
71 static kmem_cache_t *mac_srs_cache;
72 kmem_cache_t *mac_soft_ring_cache;
73 
74 /*
75  * The duration in msec we wait before signalling the soft ring
76  * worker thread in case packets get queued.
77  */
78 uint32_t mac_soft_ring_worker_wait = 0;
79 
80 /*
81  * A global tunable for turning polling on/off. By default, dynamic
82  * polling is always on and is always very beneficial. It should be
83  * turned off with absolute care and for the rare workload (very
84  * low latency sensitive traffic).
85  */
86 int mac_poll_enable = B_TRUE;
87 
88 /*
89  * Need to set mac_soft_ring_max_q_cnt based on bandwidth and perhaps latency.
90  * Large values could end up in consuming lot of system memory and cause
91  * system hang.
92  */
93 int mac_soft_ring_max_q_cnt = 1024;
94 int mac_soft_ring_min_q_cnt = 256;
95 int mac_soft_ring_poll_thres = 16;
96 
97 boolean_t mac_tx_serialize = B_FALSE;
98 
99 /*
100  * mac_tx_srs_hiwat is the queue depth threshold at which callers of
101  * mac_tx() will be notified of flow control condition.
102  *
103  * TCP does not honour flow control condition sent up by mac_tx().
104  * Thus provision is made for TCP to allow more packets to be queued
105  * in SRS upto a maximum of mac_tx_srs_max_q_cnt.
106  *
107  * Note that mac_tx_srs_hiwat is always be lesser than
108  * mac_tx_srs_max_q_cnt.
109  */
110 uint32_t mac_tx_srs_max_q_cnt = 100000;
111 uint32_t mac_tx_srs_hiwat = 1000;
112 
113 /*
114  * mac_rx_soft_ring_count, mac_soft_ring_10gig_count:
115  *
116  * Global tunables that determines the number of soft rings to be used for
117  * fanning out incoming traffic on a link. These count will be used only
118  * when no explicit set of CPUs was assigned to the data-links.
119  *
120  * mac_rx_soft_ring_count tunable will come into effect only if
121  * mac_soft_ring_enable is set. mac_soft_ring_enable is turned on by
122  * default only for sun4v platforms.
123  *
124  * mac_rx_soft_ring_10gig_count will come into effect if you are running on a
125  * 10Gbps link and is not dependent upon mac_soft_ring_enable.
126  *
127  * The number of soft rings for fanout for a link or a flow is determined
128  * by mac_compute_soft_ring_count() routine. This routine will take into
129  * account mac_soft_ring_enable, mac_rx_soft_ring_count and
130  * mac_rx_soft_ring_10gig_count to determine the soft ring count for a link.
131  *
132  * If a bandwidth is specified, the determination of the number of soft
133  * rings is based on specified bandwidth, CPU speed and number of CPUs in
134  * the system.
135  */
136 uint_t mac_rx_soft_ring_count = 8;
137 uint_t mac_rx_soft_ring_10gig_count = 8;
138 
139 /*
140  * Every Tx and Rx mac_soft_ring_set_t (mac_srs) created gets added
141  * to mac_srs_g_list and mac_srs_g_lock protects mac_srs_g_list. The
142  * list is used to walk the list of all MAC threads when a CPU is
143  * coming online or going offline.
144  */
145 static mac_soft_ring_set_t *mac_srs_g_list = NULL;
146 static krwlock_t mac_srs_g_lock;
147 
148 /*
149  * Whether the SRS threads should be bound, or not.
150  */
151 boolean_t mac_srs_thread_bind = B_TRUE;
152 
153 /*
154  * Whether Rx/Tx interrupts should be re-targeted. Disabled by default.
155  * dladm command would override this.
156  */
157 boolean_t mac_tx_intr_retarget = B_FALSE;
158 boolean_t mac_rx_intr_retarget = B_FALSE;
159 
160 /*
161  * If cpu bindings are specified by user, then Tx SRS and its soft
162  * rings should also be bound to the CPUs specified by user. The
163  * CPUs for Tx bindings are at the end of the cpu list provided by
164  * the user. If enough CPUs are not available (for Tx and Rx
165  * SRSes), then the CPUs are shared by both Tx and Rx SRSes.
166  */
167 #define	BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp) {			\
168 	processorid_t cpuid;						\
169 	int i;								\
170 	mac_soft_ring_t *softring;					\
171 	mac_cpus_t *srs_cpu;						\
172 									\
173 	srs_cpu = &mac_tx_srs->srs_cpu;					\
174 	cpuid = srs_cpu->mc_tx_fanout_cpus[0];				\
175 	mac_srs_worker_bind(mac_tx_srs, cpuid);				\
176 	if (MAC_TX_SOFT_RINGS(mac_tx_srs)) {				\
177 		for (i = 0; i < mac_tx_srs->srs_tx_ring_count; i++) {	\
178 			cpuid = srs_cpu->mc_tx_fanout_cpus[i];		\
179 			softring = mac_tx_srs->srs_tx_soft_rings[i];	\
180 			if (cpuid != -1) {				\
181 				(void) mac_soft_ring_bind(softring,	\
182 				    cpuid);				\
183 			}						\
184 		}							\
185 	}								\
186 }
187 
188 /*
189  * Re-targeting is allowed only for exclusive group or for primary.
190  */
191 #define	RETARGETABLE_CLIENT(group, mcip)				\
192 	((((group) != NULL) &&						\
193 	    ((group)->mrg_state == MAC_GROUP_STATE_RESERVED)) ||	\
194 	    mac_is_primary_client(mcip))
195 
196 #define	MAC_RING_RETARGETABLE(ring)					\
197 	(((ring) != NULL) &&						\
198 	    ((ring)->mr_info.mri_intr.mi_ddi_handle != NULL) &&		\
199 	    !((ring)->mr_info.mri_intr.mi_ddi_shared))
200 
201 
202 /* INIT and FINI ROUTINES */
203 
204 void
205 mac_soft_ring_init(void)
206 {
207 	mac_soft_ring_cache = kmem_cache_create("mac_soft_ring_cache",
208 	    sizeof (mac_soft_ring_t), 64, NULL, NULL, NULL, NULL, NULL, 0);
209 
210 	mac_srs_cache = kmem_cache_create("mac_srs_cache",
211 	    sizeof (mac_soft_ring_set_t),
212 	    64, NULL, NULL, NULL, NULL, NULL, 0);
213 
214 	rw_init(&mac_srs_g_lock, NULL, RW_DEFAULT, NULL);
215 	mutex_enter(&cpu_lock);
216 	register_cpu_setup_func(mac_srs_cpu_setup, NULL);
217 	mutex_exit(&cpu_lock);
218 }
219 
220 void
221 mac_soft_ring_finish(void)
222 {
223 	mutex_enter(&cpu_lock);
224 	unregister_cpu_setup_func(mac_srs_cpu_setup, NULL);
225 	mutex_exit(&cpu_lock);
226 	rw_destroy(&mac_srs_g_lock);
227 	kmem_cache_destroy(mac_soft_ring_cache);
228 	kmem_cache_destroy(mac_srs_cache);
229 }
230 
231 static void
232 mac_srs_soft_rings_free(mac_soft_ring_set_t *mac_srs)
233 {
234 	mac_soft_ring_t	*softring, *next, *head;
235 
236 	/*
237 	 * Synchronize with mac_walk_srs_bind/unbind which are callbacks from
238 	 * DR. The callbacks from DR are called with cpu_lock held, and hence
239 	 * can't wait to grab the mac perimeter. The soft ring list is hence
240 	 * protected for read access by srs_lock. Changing the soft ring list
241 	 * needs the mac perimeter and the srs_lock.
242 	 */
243 	mutex_enter(&mac_srs->srs_lock);
244 
245 	head = mac_srs->srs_soft_ring_head;
246 	mac_srs->srs_soft_ring_head = NULL;
247 	mac_srs->srs_soft_ring_tail = NULL;
248 	mac_srs->srs_soft_ring_count = 0;
249 
250 	mutex_exit(&mac_srs->srs_lock);
251 
252 	for (softring = head; softring != NULL; softring = next) {
253 		next = softring->s_ring_next;
254 		mac_soft_ring_free(softring);
255 	}
256 }
257 
258 static void
259 mac_srs_add_glist(mac_soft_ring_set_t *mac_srs)
260 {
261 	ASSERT(mac_srs->srs_next == NULL && mac_srs->srs_prev == NULL);
262 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
263 
264 	rw_enter(&mac_srs_g_lock, RW_WRITER);
265 	mutex_enter(&mac_srs->srs_lock);
266 
267 	ASSERT((mac_srs->srs_state & SRS_IN_GLIST) == 0);
268 
269 	if (mac_srs_g_list == NULL) {
270 		mac_srs_g_list = mac_srs;
271 	} else {
272 		mac_srs->srs_next = mac_srs_g_list;
273 		mac_srs_g_list->srs_prev = mac_srs;
274 		mac_srs->srs_prev = NULL;
275 		mac_srs_g_list = mac_srs;
276 	}
277 	mac_srs->srs_state |= SRS_IN_GLIST;
278 
279 	mutex_exit(&mac_srs->srs_lock);
280 	rw_exit(&mac_srs_g_lock);
281 }
282 
283 static void
284 mac_srs_remove_glist(mac_soft_ring_set_t *mac_srs)
285 {
286 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
287 
288 	rw_enter(&mac_srs_g_lock, RW_WRITER);
289 	mutex_enter(&mac_srs->srs_lock);
290 
291 	ASSERT((mac_srs->srs_state & SRS_IN_GLIST) != 0);
292 
293 	if (mac_srs == mac_srs_g_list) {
294 		mac_srs_g_list = mac_srs->srs_next;
295 		if (mac_srs_g_list != NULL)
296 			mac_srs_g_list->srs_prev = NULL;
297 	} else {
298 		mac_srs->srs_prev->srs_next = mac_srs->srs_next;
299 		if (mac_srs->srs_next != NULL)
300 			mac_srs->srs_next->srs_prev = mac_srs->srs_prev;
301 	}
302 	mac_srs->srs_state &= ~SRS_IN_GLIST;
303 
304 	mutex_exit(&mac_srs->srs_lock);
305 	rw_exit(&mac_srs_g_lock);
306 }
307 
308 /* POLLING SETUP AND TEAR DOWN ROUTINES */
309 
310 /*
311  * Quiesce polling on the TCP/IP squeues.
312  */
313 void
314 mac_srs_client_poll_quiesce(mac_client_impl_t *mcip, mac_soft_ring_set_t *srs)
315 {
316 	VERIFY(mac_perim_held((mac_handle_t)mcip->mci_mip));
317 
318 	if (srs->srs_type & SRST_CLIENT_POLL_V4) {
319 		for (uint_t i = 0; i < srs->srs_tcp_ring_count; i++) {
320 			mac_soft_ring_t *sr = srs->srs_tcp_soft_rings[i];
321 
322 			if (sr->s_ring_rx_arg2 != NULL) {
323 				mcip->mci_rcb4.mrc_quiesce(
324 				    mcip->mci_rcb4.mrc_arg, sr->s_ring_rx_arg2);
325 			}
326 		}
327 	}
328 
329 	if (srs->srs_type & SRST_CLIENT_POLL_V6) {
330 		for (uint_t i = 0; i < srs->srs_tcp6_ring_count; i++) {
331 			mac_soft_ring_t *sr = srs->srs_tcp6_soft_rings[i];
332 
333 			if (sr->s_ring_rx_arg2 != NULL) {
334 				mcip->mci_rcb6.mrc_quiesce(
335 				    mcip->mci_rcb6.mrc_arg, sr->s_ring_rx_arg2);
336 			}
337 		}
338 	}
339 }
340 
341 /*
342  * Restart polling on the TCP/IP squeues.
343  */
344 void
345 mac_srs_client_poll_restart(mac_client_impl_t *mcip, mac_soft_ring_set_t *srs)
346 {
347 	VERIFY(mac_perim_held((mac_handle_t)mcip->mci_mip));
348 
349 	if (srs->srs_type & SRST_CLIENT_POLL_V4) {
350 		for (uint_t i = 0; i < srs->srs_tcp_ring_count; i++) {
351 			mac_soft_ring_t *sr = srs->srs_tcp_soft_rings[i];
352 
353 			if (sr->s_ring_rx_arg2 != NULL) {
354 				mcip->mci_rcb4.mrc_restart(
355 				    mcip->mci_rcb4.mrc_arg, sr->s_ring_rx_arg2);
356 			}
357 		}
358 	}
359 
360 	if (srs->srs_type & SRST_CLIENT_POLL_V6) {
361 		for (uint_t i = 0; i < srs->srs_tcp6_ring_count; i++) {
362 			mac_soft_ring_t *sr = srs->srs_tcp6_soft_rings[i];
363 
364 			if (sr->s_ring_rx_arg2 != NULL) {
365 				mcip->mci_rcb6.mrc_restart(
366 				    mcip->mci_rcb6.mrc_arg, sr->s_ring_rx_arg2);
367 			}
368 		}
369 	}
370 }
371 
372 static void
373 mac_srs_client_poll_enable_i(mac_soft_ring_set_t *srs, uint_t sr_cnt,
374     mac_soft_ring_t **udp_rings, mac_soft_ring_t **tcp_rings,
375     mac_direct_rx_t drx, void *drx_arg, mac_resource_cb_t *rcb)
376 {
377 	/*
378 	 * TCP and UDP support DLS bypass. Squeue polling support implies DLS
379 	 * bypass since the squeue poll path does not have DLS processing.
380 	 */
381 	for (uint_t i = 0; i < sr_cnt; i++) {
382 		mac_soft_ring_dls_bypass_enable(udp_rings[i], drx, drx_arg);
383 	}
384 
385 	for (uint_t i = 0; i < sr_cnt; i++) {
386 		mac_soft_ring_t *tcp_sr = tcp_rings[i];
387 		mac_rx_fifo_t mrf;
388 
389 		/*
390 		 * Polling should be configured only once on a given
391 		 * softring.
392 		 */
393 		VERIFY3P(tcp_sr->s_ring_rx_arg2, ==, NULL);
394 
395 		mac_soft_ring_dls_bypass_enable(tcp_sr, drx, drx_arg);
396 
397 		bzero(&mrf, sizeof (mrf));
398 		mrf.mrf_type = MAC_RX_FIFO;
399 		mrf.mrf_receive = (mac_receive_t)mac_soft_ring_poll;
400 		mrf.mrf_intr_enable =
401 		    (mac_intr_enable_t)mac_soft_ring_intr_enable;
402 		mrf.mrf_intr_disable =
403 		    (mac_intr_disable_t)mac_soft_ring_intr_disable;
404 		mrf.mrf_rx_arg = tcp_sr;
405 		mrf.mrf_intr_handle = (mac_intr_handle_t)tcp_sr;
406 		mrf.mrf_cpu_id = tcp_sr->s_ring_cpuid;
407 		mrf.mrf_flow_priority = srs->srs_pri;
408 
409 		tcp_sr->s_ring_rx_arg2 = rcb->mrc_add(rcb->mrc_arg,
410 		    (mac_resource_t *)&mrf);
411 	}
412 }
413 
414 /*
415  * Register the given SRS and associated soft rings with the consumer and
416  * enable the polling interface used by the consumer.(i.e IP) over this
417  * SRS and associated soft rings.
418  */
419 void
420 mac_srs_client_poll_enable(mac_client_impl_t *mcip, mac_soft_ring_set_t *srs,
421     boolean_t is_v6)
422 {
423 	VERIFY3P(srs->srs_mcip, ==, mcip);
424 	VERIFY(mac_perim_held((mac_handle_t)mcip->mci_mip));
425 
426 	if (!(mcip->mci_state_flags & MCIS_CLIENT_POLL_CAPABLE))
427 		return;
428 
429 	/*
430 	 * A SRS is capable of acting as a soft ring for cases
431 	 * where no fanout is needed. This is the case for userland
432 	 * flows.
433 	 */
434 	if (srs->srs_type & SRST_NO_SOFT_RINGS)
435 		return;
436 
437 	if (is_v6) {
438 		mac_srs_client_poll_enable_i(srs, srs->srs_tcp_ring_count,
439 		    srs->srs_udp6_soft_rings, srs->srs_tcp6_soft_rings,
440 		    mcip->mci_direct_rx.mdrx_v6,
441 		    mcip->mci_direct_rx.mdrx_arg_v6, &mcip->mci_rcb6);
442 
443 		mutex_enter(&srs->srs_lock);
444 		srs->srs_type |= (SRST_CLIENT_POLL_V6 | SRST_DLS_BYPASS_V6);
445 		mutex_exit(&srs->srs_lock);
446 	} else {
447 		mac_srs_client_poll_enable_i(srs, srs->srs_tcp_ring_count,
448 		    srs->srs_udp_soft_rings, srs->srs_tcp_soft_rings,
449 		    mcip->mci_direct_rx.mdrx_v4,
450 		    mcip->mci_direct_rx.mdrx_arg_v4, &mcip->mci_rcb4);
451 
452 		mutex_enter(&srs->srs_lock);
453 		srs->srs_type |= (SRST_CLIENT_POLL_V4 | SRST_DLS_BYPASS_V4);
454 		mutex_exit(&srs->srs_lock);
455 	}
456 }
457 
458 static void
459 mac_srs_client_poll_disable_i(mac_client_impl_t *mcip, uint_t sr_cnt,
460     mac_soft_ring_t **udp_rings, mac_soft_ring_t **tcp_rings,
461     mac_resource_cb_t *rcb)
462 {
463 	for (uint_t i = 0; i < sr_cnt; i++) {
464 		mac_soft_ring_t *tcp_sr = tcp_rings[i];
465 
466 		/*
467 		 * Remove the IP ring if there is one associated with this
468 		 * softring. Note that IP rings are a limited resource; and
469 		 * SRST_CLIENT_POLL_V4/V6 being set on the SRS is no
470 		 * guarantee that all TCP softrings have an associated IP
471 		 * ring. This is by design. See ip_squeue_add_ring().
472 		 */
473 		if (tcp_sr->s_ring_rx_arg2 != NULL) {
474 			VERIFY3P(rcb->mrc_arg, !=, NULL);
475 			rcb->mrc_remove(rcb->mrc_arg, tcp_sr->s_ring_rx_arg2);
476 			tcp_sr->s_ring_rx_arg2 = NULL;
477 		}
478 		mac_soft_ring_dls_bypass_disable(tcp_sr, mcip);
479 	}
480 
481 	for (uint_t i = 0; i < sr_cnt; i++) {
482 		mac_soft_ring_t *udp_sr = udp_rings[i];
483 
484 		/* There is no polling on UDP; this should always be NULL. */
485 		VERIFY3P(udp_sr->s_ring_rx_arg2, ==, NULL);
486 		mac_soft_ring_dls_bypass_disable(udp_sr, mcip);
487 	}
488 }
489 
490 /*
491  * Unregister the given SRS and associated soft rings with the consumer and
492  * disable the polling interface used by the consumer (i.e IP) over this
493  * SRS and associated soft rings.
494  */
495 void
496 mac_srs_client_poll_disable(mac_client_impl_t *mcip, mac_soft_ring_set_t *srs,
497     boolean_t is_v6)
498 {
499 	VERIFY(mac_perim_held((mac_handle_t)mcip->mci_mip));
500 
501 	/*
502 	 * A SRS is capable of acting as a soft ring for cases
503 	 * where no protocol fanout is needed. This is the case
504 	 * for userland flows. Nothing to do here.
505 	 */
506 	if (srs->srs_type & SRST_NO_SOFT_RINGS)
507 		return;
508 
509 	mutex_enter(&srs->srs_lock);
510 	if (!is_v6 && !(srs->srs_type & SRST_CLIENT_POLL_V4)) {
511 		VERIFY(!(srs->srs_type & SRST_DLS_BYPASS_V4));
512 		mutex_exit(&srs->srs_lock);
513 		return;
514 	}
515 
516 	if (is_v6 && !(srs->srs_type & SRST_CLIENT_POLL_V6)) {
517 		VERIFY(!(srs->srs_type & SRST_DLS_BYPASS_V6));
518 		mutex_exit(&srs->srs_lock);
519 		return;
520 	}
521 
522 	/*
523 	 * Before modifying TCP/UDP softring state we must first inform the SRS
524 	 * that DLS bypass is no longer to be performed; thereby directing all
525 	 * future traffic to the OTH softring.
526 	 */
527 	if (is_v6) {
528 		srs->srs_type &= ~(SRST_CLIENT_POLL_V6 |
529 		    SRST_DLS_BYPASS_V6);
530 	} else {
531 		srs->srs_type &= ~(SRST_CLIENT_POLL_V4 |
532 		    SRST_DLS_BYPASS_V4);
533 	}
534 
535 	mutex_exit(&srs->srs_lock);
536 
537 	if (is_v6) {
538 		mac_srs_client_poll_disable_i(mcip, srs->srs_tcp_ring_count,
539 		    srs->srs_udp6_soft_rings, srs->srs_tcp6_soft_rings,
540 		    &mcip->mci_rcb6);
541 	} else {
542 		mac_srs_client_poll_disable_i(mcip, srs->srs_tcp_ring_count,
543 		    srs->srs_udp_soft_rings, srs->srs_tcp_soft_rings,
544 		    &mcip->mci_rcb4);
545 	}
546 }
547 
548 /*
549  * Enable or disable poll capability of the SRS on the underlying Rx ring.
550  *
551  * There is a need to enable or disable the poll capability of an SRS over an
552  * Rx ring depending on the number of mac clients sharing the ring and also
553  * whether user flows are configured on it. However the poll state is actively
554  * manipulated by the SRS worker and poll threads and uncoordinated changes by
555  * yet another thread to the underlying capability can surprise them leading
556  * to assert failures. Instead we quiesce the SRS, make the changes and then
557  * restart the SRS.
558  */
559 static void
560 mac_srs_poll_state_change(mac_soft_ring_set_t *mac_srs,
561     boolean_t turn_off_poll_capab, mac_rx_func_t rx_func)
562 {
563 	boolean_t	need_restart = B_FALSE;
564 	mac_srs_rx_t	*srs_rx = &mac_srs->srs_rx;
565 	mac_ring_t	*ring;
566 
567 	if (!SRS_QUIESCED(mac_srs)) {
568 		mac_rx_srs_quiesce(mac_srs, SRS_QUIESCE);
569 		need_restart = B_TRUE;
570 	}
571 
572 	ring = mac_srs->srs_ring;
573 	if ((ring != NULL) &&
574 	    (ring->mr_classify_type == MAC_HW_CLASSIFIER)) {
575 		if (turn_off_poll_capab)
576 			mac_srs->srs_state &= ~SRS_POLLING_CAPAB;
577 		else if (mac_poll_enable)
578 			mac_srs->srs_state |= SRS_POLLING_CAPAB;
579 	}
580 	srs_rx->sr_lower_proc = rx_func;
581 
582 	if (need_restart)
583 		mac_rx_srs_restart(mac_srs);
584 }
585 
586 /* CPU RECONFIGURATION AND FANOUT COMPUTATION ROUTINES */
587 
588 /*
589  * Return the next CPU to be used to bind a MAC kernel thread.
590  * If a cpupart is specified, the cpu chosen must be from that
591  * cpu partition.
592  */
593 static processorid_t
594 mac_next_bind_cpu(cpupart_t *cpupart)
595 {
596 	static cpu_t		*cp = NULL;
597 	cpu_t			*cp_start;
598 
599 	ASSERT(MUTEX_HELD(&cpu_lock));
600 
601 	if (cp == NULL)
602 		cp = cpu_list;
603 
604 	cp = cp->cpu_next_onln;
605 	cp_start = cp;
606 
607 	do {
608 		if ((cpupart == NULL) || (cp->cpu_part == cpupart))
609 			return (cp->cpu_id);
610 
611 	} while ((cp = cp->cpu_next_onln) != cp_start);
612 
613 	return (-1);	/* No matching CPU found online */
614 }
615 
616 /* ARGSUSED */
617 static int
618 mac_srs_cpu_setup(cpu_setup_t what, int id, void *arg)
619 {
620 	ASSERT(MUTEX_HELD(&cpu_lock));
621 	switch (what) {
622 	case CPU_CONFIG:
623 	case CPU_ON:
624 	case CPU_CPUPART_IN:
625 		mac_walk_srs_and_bind(id);
626 		break;
627 
628 	case CPU_UNCONFIG:
629 	case CPU_OFF:
630 	case CPU_CPUPART_OUT:
631 		mac_walk_srs_and_unbind(id);
632 		break;
633 
634 	default:
635 		break;
636 	}
637 	return (0);
638 }
639 
640 /*
641  * mac_compute_soft_ring_count():
642  *
643  * This routine computes the number of soft rings needed to handle incoming
644  * load given a flow_entry.
645  *
646  * The routine does the following:
647  * 1) soft rings will be created if mac_soft_ring_enable is set.
648  * 2) If the underlying link is a 10Gbps link, then soft rings will be
649  * created even if mac_soft_ring_enable is not set. The number of soft
650  * rings, so created,  will equal mac_rx_soft_ring_10gig_count.
651  * 3) On a sun4v platform (i.e., mac_soft_ring_enable is set), 2 times the
652  * mac_rx_soft_ring_10gig_count number of soft rings will be created for a
653  * 10Gbps link.
654  *
655  * If a bandwidth limit is specified, the number that gets computed is
656  * dependent upon CPU speed, the number of Rx rings configured, and
657  * the bandwidth limit.
658  * If more Rx rings are available, less number of soft rings is needed.
659  *
660  * mac_use_bw_heuristic is another "hidden" variable that can be used to
661  * override the default use of soft ring count computation. Depending upon
662  * the usefulness of it, mac_use_bw_heuristic can later be made into a
663  * data-link property or removed altogether.
664  *
665  * TODO: Cleanup and tighten some of the assumptions.
666  */
667 boolean_t mac_check_overlay = B_TRUE;
668 boolean_t mac_use_bw_heuristic = B_TRUE;
669 static int
670 mac_compute_soft_ring_count(flow_entry_t *flent, int rx_srs_cnt, int maxcpus)
671 {
672 	uint64_t cpu_speed, bw = 0;
673 	int srings = 0;
674 	boolean_t bw_enabled = B_FALSE;
675 	mac_client_impl_t *mcip = flent->fe_mcip;
676 
677 	ASSERT(!(flent->fe_type & FLOW_USER));
678 	if (flent->fe_resource_props.mrp_mask & MRP_MAXBW &&
679 	    mac_use_bw_heuristic) {
680 		/* bandwidth enabled */
681 		bw_enabled = B_TRUE;
682 		bw = flent->fe_resource_props.mrp_maxbw;
683 	}
684 	if (!bw_enabled) {
685 		/* No bandwidth enabled */
686 		if (mac_soft_ring_enable)
687 			srings = mac_rx_soft_ring_count;
688 
689 		/* Is this a 10Gig link? */
690 		flent->fe_nic_speed = mac_client_stat_get(flent->fe_mcip,
691 		    MAC_STAT_IFSPEED);
692 		/* convert to Mbps */
693 		if (((flent->fe_nic_speed)/1000000) > 1000 &&
694 		    mac_rx_soft_ring_10gig_count > 0) {
695 			/* This is a 10Gig link */
696 			srings = mac_rx_soft_ring_10gig_count;
697 			/*
698 			 * Use 2 times mac_rx_soft_ring_10gig_count for
699 			 * sun4v systems.
700 			 */
701 			if (mac_soft_ring_enable)
702 				srings = srings * 2;
703 		} else if (mac_check_overlay == B_TRUE &&
704 		    (mcip->mci_state_flags & MCIS_IS_VNIC) != 0) {
705 			/* Is this a VNIC on an overlay? */
706 			mac_handle_t mh = (mac_handle_t)mcip->mci_mip;
707 			if (mac_is_overlay(mh) == B_TRUE) {
708 				srings = mac_rx_soft_ring_10gig_count;
709 			}
710 		}
711 
712 
713 	} else {
714 		/*
715 		 * Soft ring computation using CPU speed and specified
716 		 * bandwidth limit.
717 		 */
718 		/* Assumption: all CPUs have the same frequency */
719 		cpu_speed = (uint64_t)CPU->cpu_type_info.pi_clock;
720 
721 		/* cpu_speed is in MHz; make bw in units of Mbps.  */
722 		bw = bw/1000000;
723 
724 		if (bw >= 1000) {
725 			/*
726 			 * bw is greater than or equal to 1Gbps.
727 			 * The number of soft rings required is a function
728 			 * of bandwidth and CPU speed. To keep this simple,
729 			 * let's use this rule: 1GHz CPU can handle 1Gbps.
730 			 * If bw is less than 1 Gbps, then there is no need
731 			 * for soft rings. Assumption is that CPU speeds
732 			 * (on modern systems) are at least 1GHz.
733 			 */
734 			srings = bw/cpu_speed;
735 			if (srings <= 1 && mac_soft_ring_enable) {
736 				/*
737 				 * Give at least 2 soft rings
738 				 * for sun4v systems
739 				 */
740 				srings = 2;
741 			}
742 		}
743 	}
744 	/*
745 	 * If the flent has multiple Rx SRSs, then each SRS need not
746 	 * have that many soft rings on top of it. The number of
747 	 * soft rings for each Rx SRS is found by dividing srings by
748 	 * rx_srs_cnt.
749 	 */
750 	if (rx_srs_cnt > 1) {
751 		int remainder;
752 
753 		remainder = srings%rx_srs_cnt;
754 		srings = srings/rx_srs_cnt;
755 		if (remainder != 0)
756 			srings++;
757 		/*
758 		 * Fanning out to 1 soft ring is not very useful.
759 		 * Set it as well to 0 and mac_srs_fanout_init()
760 		 * will take care of creating a single soft ring
761 		 * for proto fanout.
762 		 */
763 		if (srings == 1)
764 			srings = 0;
765 	}
766 	/* Do some more massaging */
767 	srings = min(srings, maxcpus);
768 	srings = min(srings, MAX_SR_FANOUT);
769 	return (srings);
770 }
771 
772 /*
773  * mac_tx_cpu_init:
774  * set up CPUs for Tx interrupt re-targeting and Tx worker
775  * thread binding
776  */
777 static void
778 mac_tx_cpu_init(flow_entry_t *flent, mac_resource_props_t *mrp,
779     cpupart_t *cpupart)
780 {
781 	mac_soft_ring_set_t *tx_srs = flent->fe_tx_srs;
782 	mac_srs_tx_t *srs_tx = &tx_srs->srs_tx;
783 	mac_cpus_t *srs_cpu = &tx_srs->srs_cpu;
784 	mac_soft_ring_t *sringp;
785 	mac_ring_t *ring;
786 	processorid_t worker_cpuid;
787 	boolean_t retargetable_client = B_FALSE;
788 	int i, j;
789 
790 	if (RETARGETABLE_CLIENT((mac_group_t *)flent->fe_tx_ring_group,
791 	    flent->fe_mcip)) {
792 		retargetable_client = B_TRUE;
793 	}
794 
795 	if (MAC_TX_SOFT_RINGS(tx_srs)) {
796 		if (mrp != NULL)
797 			j = mrp->mrp_ncpus - 1;
798 		for (i = 0; i < tx_srs->srs_tx_ring_count; i++) {
799 			if (mrp != NULL) {
800 				if (j < 0)
801 					j = mrp->mrp_ncpus - 1;
802 				worker_cpuid = mrp->mrp_cpu[j];
803 			} else {
804 				/*
805 				 * Bind interrupt to the next CPU available
806 				 * and leave the worker unbound.
807 				 */
808 				worker_cpuid = -1;
809 			}
810 			sringp = tx_srs->srs_tx_soft_rings[i];
811 			ring = (mac_ring_t *)sringp->s_ring_tx_arg2;
812 			srs_cpu->mc_tx_fanout_cpus[i] = worker_cpuid;
813 			if (MAC_RING_RETARGETABLE(ring) &&
814 			    retargetable_client) {
815 				mutex_enter(&cpu_lock);
816 				srs_cpu->mc_tx_intr_cpu[i] =
817 				    (mrp != NULL) ? mrp->mrp_cpu[j] :
818 				    (mac_tx_intr_retarget ?
819 				    mac_next_bind_cpu(cpupart) : -1);
820 				mutex_exit(&cpu_lock);
821 			} else {
822 				srs_cpu->mc_tx_intr_cpu[i] = -1;
823 			}
824 			if (mrp != NULL)
825 				j--;
826 		}
827 	} else {
828 		/* Tx mac_ring_handle_t is stored in st_arg2 */
829 		srs_cpu->mc_tx_fanout_cpus[0] =
830 		    (mrp != NULL) ? mrp->mrp_cpu[mrp->mrp_ncpus - 1] : -1;
831 		ring = (mac_ring_t *)srs_tx->st_arg2;
832 		if (MAC_RING_RETARGETABLE(ring) && retargetable_client) {
833 			mutex_enter(&cpu_lock);
834 			srs_cpu->mc_tx_intr_cpu[0] = (mrp != NULL) ?
835 			    mrp->mrp_cpu[mrp->mrp_ncpus - 1] :
836 			    (mac_tx_intr_retarget ?
837 			    mac_next_bind_cpu(cpupart) : -1);
838 			mutex_exit(&cpu_lock);
839 		} else {
840 			srs_cpu->mc_tx_intr_cpu[0] = -1;
841 		}
842 	}
843 }
844 
845 /*
846  * Assignment of user specified CPUs to a link.
847  *
848  * Minimum CPUs required to get an optimal assignmet:
849  * For each Rx SRS, atleast two CPUs are needed if mac_latency_optimize
850  * flag is set -- one for polling, one for fanout soft ring.
851  * If mac_latency_optimize is not set, then 3 CPUs are needed -- one
852  * for polling, one for SRS worker thread and one for fanout soft ring.
853  *
854  * The CPUs needed for Tx side is equal to the number of Tx rings
855  * the link is using.
856  *
857  * mac_flow_user_cpu_init() categorizes the CPU assignment depending
858  * upon the number of CPUs in 3 different buckets.
859  *
860  * In the first bucket, the most optimal case is handled. The user has
861  * passed enough number of CPUs and every thread gets its own CPU.
862  *
863  * The second and third are the sub-optimal cases. Enough CPUs are not
864  * available.
865  *
866  * The second bucket handles the case where atleast one distinct CPU is
867  * is available for each of the Rx rings (Rx SRSes) and Tx rings (Tx
868  * SRS or soft rings).
869  *
870  * In the third case (worst case scenario), specified CPU count is less
871  * than the Rx rings configured for the link. In this case, we round
872  * robin the CPUs among the Rx SRSes and Tx SRS/soft rings.
873  */
874 static void
875 mac_flow_user_cpu_init(flow_entry_t *flent, mac_resource_props_t *mrp)
876 {
877 	mac_soft_ring_set_t *rx_srs, *tx_srs;
878 	int i, srs_cnt;
879 	mac_cpus_t *srs_cpu;
880 	int no_of_cpus, cpu_cnt;
881 	int rx_srs_cnt, reqd_rx_cpu_cnt;
882 	int fanout_cpu_cnt, reqd_tx_cpu_cnt;
883 	int reqd_poll_worker_cnt, fanout_cnt_per_srs;
884 	mac_resource_props_t *emrp = &flent->fe_effective_props;
885 
886 	ASSERT(mrp->mrp_fanout_mode == MCM_CPUS);
887 	/*
888 	 * The check for nbc_ncpus to be within limits for
889 	 * the user specified case was done earlier and if
890 	 * not within limits, an error would have been
891 	 * returned to the user.
892 	 */
893 	ASSERT(mrp->mrp_ncpus > 0);
894 
895 	no_of_cpus = mrp->mrp_ncpus;
896 
897 	if (mrp->mrp_rx_intr_cpu != -1) {
898 		/*
899 		 * interrupt has been re-targetted. Poll
900 		 * thread needs to be bound to interrupt
901 		 * CPU.
902 		 *
903 		 * Find where in the list is the intr
904 		 * CPU and swap it with the first one.
905 		 * We will be using the first CPU in the
906 		 * list for poll.
907 		 */
908 		for (i = 0; i < no_of_cpus; i++) {
909 			if (mrp->mrp_cpu[i] == mrp->mrp_rx_intr_cpu)
910 				break;
911 		}
912 		mrp->mrp_cpu[i] = mrp->mrp_cpu[0];
913 		mrp->mrp_cpu[0] = mrp->mrp_rx_intr_cpu;
914 	}
915 
916 	/*
917 	 * Requirements:
918 	 * The number of CPUs that each Rx ring needs is dependent
919 	 * upon mac_latency_optimize flag.
920 	 * 1) If set, atleast 2 CPUs are needed -- one for
921 	 * polling, one for fanout soft ring.
922 	 * 2) If not set, then atleast 3 CPUs are needed -- one
923 	 * for polling, one for srs worker thread, and one for
924 	 * fanout soft ring.
925 	 */
926 	rx_srs_cnt = (flent->fe_rx_srs_cnt > 1) ?
927 	    (flent->fe_rx_srs_cnt - 1) : flent->fe_rx_srs_cnt;
928 	reqd_rx_cpu_cnt = mac_latency_optimize ?
929 	    (rx_srs_cnt * 2) : (rx_srs_cnt * 3);
930 
931 	/* How many CPUs are needed for Tx side? */
932 	tx_srs = flent->fe_tx_srs;
933 	reqd_tx_cpu_cnt = MAC_TX_SOFT_RINGS(tx_srs) ?
934 	    tx_srs->srs_tx_ring_count : 1;
935 
936 	/* CPUs needed for Rx SRSes poll and worker threads */
937 	reqd_poll_worker_cnt = mac_latency_optimize ?
938 	    rx_srs_cnt : rx_srs_cnt * 2;
939 
940 	/* Has the user provided enough CPUs? */
941 	if (no_of_cpus >= (reqd_rx_cpu_cnt + reqd_tx_cpu_cnt)) {
942 		/*
943 		 * Best case scenario. There is enough CPUs. All
944 		 * Rx rings will get their own set of CPUs plus
945 		 * Tx soft rings will get their own.
946 		 */
947 		/*
948 		 * fanout_cpu_cnt is the number of CPUs available
949 		 * for Rx side fanout soft rings.
950 		 */
951 		fanout_cpu_cnt = no_of_cpus -
952 		    reqd_poll_worker_cnt - reqd_tx_cpu_cnt;
953 
954 		/*
955 		 * Divide fanout_cpu_cnt by rx_srs_cnt to find
956 		 * out how many fanout soft rings each Rx SRS
957 		 * can have.
958 		 */
959 		fanout_cnt_per_srs = fanout_cpu_cnt/rx_srs_cnt;
960 
961 		/* fanout_cnt_per_srs should not be >  MAX_SR_FANOUT */
962 		fanout_cnt_per_srs = min(fanout_cnt_per_srs, MAX_SR_FANOUT);
963 
964 		/* Do the assignment for the default Rx ring */
965 		cpu_cnt = 0;
966 		rx_srs = flent->fe_rx_srs[0];
967 		ASSERT(rx_srs->srs_ring == NULL);
968 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
969 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
970 		srs_cpu = &rx_srs->srs_cpu;
971 		srs_cpu->mc_ncpus = no_of_cpus;
972 		bcopy(mrp->mrp_cpu,
973 		    srs_cpu->mc_cpus, sizeof (srs_cpu->mc_cpus));
974 		srs_cpu->mc_rx_fanout_cnt = fanout_cnt_per_srs;
975 		srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt++];
976 		/* Retarget the interrupt to the same CPU as the poll */
977 		srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
978 		srs_cpu->mc_rx_workerid = (mac_latency_optimize ?
979 		    srs_cpu->mc_rx_pollid : mrp->mrp_cpu[cpu_cnt++]);
980 		for (i = 0; i < fanout_cnt_per_srs; i++)
981 			srs_cpu->mc_rx_fanout_cpus[i] = mrp->mrp_cpu[cpu_cnt++];
982 
983 		/* Do the assignment for h/w Rx SRSes */
984 		if (flent->fe_rx_srs_cnt > 1) {
985 			cpu_cnt = 0;
986 			for (srs_cnt = 1;
987 			    srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
988 				rx_srs = flent->fe_rx_srs[srs_cnt];
989 				ASSERT(rx_srs->srs_ring != NULL);
990 				if (rx_srs->srs_fanout_state ==
991 				    SRS_FANOUT_INIT) {
992 					rx_srs->srs_fanout_state =
993 					    SRS_FANOUT_REINIT;
994 				}
995 				srs_cpu = &rx_srs->srs_cpu;
996 				srs_cpu->mc_ncpus = no_of_cpus;
997 				bcopy(mrp->mrp_cpu, srs_cpu->mc_cpus,
998 				    sizeof (srs_cpu->mc_cpus));
999 				srs_cpu->mc_rx_fanout_cnt = fanout_cnt_per_srs;
1000 				/* The first CPU in the list is the intr CPU */
1001 				srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt++];
1002 				srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
1003 				srs_cpu->mc_rx_workerid =
1004 				    (mac_latency_optimize ?
1005 				    srs_cpu->mc_rx_pollid :
1006 				    mrp->mrp_cpu[cpu_cnt++]);
1007 				for (i = 0; i < fanout_cnt_per_srs; i++) {
1008 					srs_cpu->mc_rx_fanout_cpus[i] =
1009 					    mrp->mrp_cpu[cpu_cnt++];
1010 				}
1011 				ASSERT(cpu_cnt <= no_of_cpus);
1012 			}
1013 		}
1014 		goto tx_cpu_init;
1015 	}
1016 
1017 	/*
1018 	 * Sub-optimal case.
1019 	 * We have the following information:
1020 	 * no_of_cpus - no. of cpus that user passed.
1021 	 * rx_srs_cnt - no. of rx rings.
1022 	 * reqd_rx_cpu_cnt = mac_latency_optimize?rx_srs_cnt*2:rx_srs_cnt*3
1023 	 * reqd_tx_cpu_cnt - no. of cpus reqd. for Tx side.
1024 	 * reqd_poll_worker_cnt = mac_latency_optimize?rx_srs_cnt:rx_srs_cnt*2
1025 	 */
1026 	/*
1027 	 * If we bind the Rx fanout soft rings to the same CPUs
1028 	 * as poll/worker, would that be enough?
1029 	 */
1030 	if (no_of_cpus >= (rx_srs_cnt + reqd_tx_cpu_cnt)) {
1031 		boolean_t worker_assign = B_FALSE;
1032 
1033 		/*
1034 		 * If mac_latency_optimize is not set, are there
1035 		 * enough CPUs to assign a CPU for worker also?
1036 		 */
1037 		if (no_of_cpus >= (reqd_poll_worker_cnt + reqd_tx_cpu_cnt))
1038 			worker_assign = B_TRUE;
1039 		/*
1040 		 * Zero'th Rx SRS is the default Rx ring. It is not
1041 		 * associated with h/w Rx ring.
1042 		 */
1043 		rx_srs = flent->fe_rx_srs[0];
1044 		ASSERT(rx_srs->srs_ring == NULL);
1045 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
1046 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
1047 		cpu_cnt = 0;
1048 		srs_cpu = &rx_srs->srs_cpu;
1049 		srs_cpu->mc_ncpus = no_of_cpus;
1050 		bcopy(mrp->mrp_cpu,
1051 		    srs_cpu->mc_cpus, sizeof (srs_cpu->mc_cpus));
1052 		srs_cpu->mc_rx_fanout_cnt = 1;
1053 		srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt++];
1054 		/* Retarget the interrupt to the same CPU as the poll */
1055 		srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
1056 		srs_cpu->mc_rx_workerid =
1057 		    ((!mac_latency_optimize && worker_assign) ?
1058 		    mrp->mrp_cpu[cpu_cnt++] : srs_cpu->mc_rx_pollid);
1059 
1060 		srs_cpu->mc_rx_fanout_cpus[0] = mrp->mrp_cpu[cpu_cnt];
1061 
1062 		/* Do CPU bindings for SRSes having h/w Rx rings */
1063 		if (flent->fe_rx_srs_cnt > 1) {
1064 			cpu_cnt = 0;
1065 			for (srs_cnt = 1;
1066 			    srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
1067 				rx_srs = flent->fe_rx_srs[srs_cnt];
1068 				ASSERT(rx_srs->srs_ring != NULL);
1069 				if (rx_srs->srs_fanout_state ==
1070 				    SRS_FANOUT_INIT) {
1071 					rx_srs->srs_fanout_state =
1072 					    SRS_FANOUT_REINIT;
1073 				}
1074 				srs_cpu = &rx_srs->srs_cpu;
1075 				srs_cpu->mc_ncpus = no_of_cpus;
1076 				bcopy(mrp->mrp_cpu, srs_cpu->mc_cpus,
1077 				    sizeof (srs_cpu->mc_cpus));
1078 				srs_cpu->mc_rx_pollid =
1079 				    mrp->mrp_cpu[cpu_cnt];
1080 				srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
1081 				srs_cpu->mc_rx_workerid =
1082 				    ((!mac_latency_optimize && worker_assign) ?
1083 				    mrp->mrp_cpu[++cpu_cnt] :
1084 				    srs_cpu->mc_rx_pollid);
1085 				srs_cpu->mc_rx_fanout_cnt = 1;
1086 				srs_cpu->mc_rx_fanout_cpus[0] =
1087 				    mrp->mrp_cpu[cpu_cnt];
1088 				cpu_cnt++;
1089 				ASSERT(cpu_cnt <= no_of_cpus);
1090 			}
1091 		}
1092 		goto tx_cpu_init;
1093 	}
1094 
1095 	/*
1096 	 * Real sub-optimal case. Not enough CPUs for poll and
1097 	 * Tx soft rings. Do a round robin assignment where
1098 	 * each Rx SRS will get the same CPU for poll, worker
1099 	 * and fanout soft ring.
1100 	 */
1101 	cpu_cnt = 0;
1102 	for (srs_cnt = 0; srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
1103 		rx_srs = flent->fe_rx_srs[srs_cnt];
1104 		srs_cpu = &rx_srs->srs_cpu;
1105 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
1106 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
1107 		srs_cpu->mc_ncpus = no_of_cpus;
1108 		bcopy(mrp->mrp_cpu,
1109 		    srs_cpu->mc_cpus, sizeof (srs_cpu->mc_cpus));
1110 		srs_cpu->mc_rx_fanout_cnt = 1;
1111 		srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt];
1112 		/* Retarget the interrupt to the same CPU as the poll */
1113 		srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
1114 		srs_cpu->mc_rx_workerid = mrp->mrp_cpu[cpu_cnt];
1115 		srs_cpu->mc_rx_fanout_cpus[0] = mrp->mrp_cpu[cpu_cnt];
1116 		if (++cpu_cnt >= no_of_cpus)
1117 			cpu_cnt = 0;
1118 	}
1119 
1120 tx_cpu_init:
1121 	mac_tx_cpu_init(flent, mrp, NULL);
1122 
1123 	/*
1124 	 * Copy the user specified CPUs to the effective CPUs
1125 	 */
1126 	for (i = 0; i < mrp->mrp_ncpus; i++) {
1127 		emrp->mrp_cpu[i] = mrp->mrp_cpu[i];
1128 	}
1129 	emrp->mrp_ncpus = mrp->mrp_ncpus;
1130 	emrp->mrp_mask = mrp->mrp_mask;
1131 	bzero(emrp->mrp_pool, MAXPATHLEN);
1132 }
1133 
1134 /*
1135  * mac_flow_cpu_init():
1136  *
1137  * Each SRS has a mac_cpu_t structure, srs_cpu. This routine fills in
1138  * the CPU binding information in srs_cpu for all Rx SRSes associated
1139  * with a flent.
1140  */
1141 static void
1142 mac_flow_cpu_init(flow_entry_t *flent, cpupart_t *cpupart)
1143 {
1144 	mac_soft_ring_set_t *rx_srs;
1145 	processorid_t cpuid;
1146 	int i, j, k, srs_cnt, maxcpus, soft_ring_cnt = 0;
1147 	mac_cpus_t *srs_cpu;
1148 	mac_resource_props_t *emrp = &flent->fe_effective_props;
1149 
1150 	/*
1151 	 * The maximum number of CPUs available can either be
1152 	 * the number of CPUs in the pool or the number of CPUs
1153 	 * in the system.
1154 	 */
1155 	maxcpus = (cpupart != NULL) ? cpupart->cp_ncpus : ncpus;
1156 	/*
1157 	 * We cannot exceed the hard limit imposed by data structures.
1158 	 * Leave space for polling CPU and the SRS worker thread when
1159 	 * "mac_latency_optimize" is not set.
1160 	 */
1161 	maxcpus = MIN(maxcpus, MRP_NCPUS - 2);
1162 
1163 	/*
1164 	 * Compute the number of soft rings needed on top for each Rx
1165 	 * SRS. "rx_srs_cnt-1" indicates the number of Rx SRS
1166 	 * associated with h/w Rx rings. Soft ring count needed for
1167 	 * each h/w Rx SRS is computed and the same is applied to
1168 	 * software classified Rx SRS. The first Rx SRS in fe_rx_srs[]
1169 	 * is the software classified Rx SRS.
1170 	 */
1171 	soft_ring_cnt = mac_compute_soft_ring_count(flent,
1172 	    flent->fe_rx_srs_cnt - 1, maxcpus);
1173 	if (soft_ring_cnt == 0) {
1174 		/*
1175 		 * Even when soft_ring_cnt is 0, we still need
1176 		 * to create a soft ring for TCP, UDP and
1177 		 * OTHER. So set it to 1.
1178 		 */
1179 		soft_ring_cnt = 1;
1180 	}
1181 
1182 	emrp->mrp_ncpus = 0;
1183 	for (srs_cnt = 0; srs_cnt < flent->fe_rx_srs_cnt &&
1184 	    emrp->mrp_ncpus < MRP_NCPUS; srs_cnt++) {
1185 		rx_srs = flent->fe_rx_srs[srs_cnt];
1186 		srs_cpu = &rx_srs->srs_cpu;
1187 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
1188 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
1189 		srs_cpu->mc_ncpus = soft_ring_cnt;
1190 		srs_cpu->mc_rx_fanout_cnt = soft_ring_cnt;
1191 		mutex_enter(&cpu_lock);
1192 		for (j = 0; j < soft_ring_cnt; j++) {
1193 			cpuid = mac_next_bind_cpu(cpupart);
1194 			srs_cpu->mc_cpus[j] = cpuid;
1195 			srs_cpu->mc_rx_fanout_cpus[j] = cpuid;
1196 		}
1197 		cpuid = mac_next_bind_cpu(cpupart);
1198 		srs_cpu->mc_rx_pollid = cpuid;
1199 		srs_cpu->mc_rx_intr_cpu = (mac_rx_intr_retarget ?
1200 		    srs_cpu->mc_rx_pollid : -1);
1201 		/* increment ncpus to account for polling cpu */
1202 		srs_cpu->mc_ncpus++;
1203 		srs_cpu->mc_cpus[j++] = cpuid;
1204 		if (!mac_latency_optimize) {
1205 			cpuid = mac_next_bind_cpu(cpupart);
1206 			srs_cpu->mc_ncpus++;
1207 			srs_cpu->mc_cpus[j++] = cpuid;
1208 		}
1209 		srs_cpu->mc_rx_workerid = cpuid;
1210 		mutex_exit(&cpu_lock);
1211 
1212 		/*
1213 		 * Copy fanout CPUs to fe_effective_props without duplicates.
1214 		 */
1215 		for (i = 0; i < srs_cpu->mc_ncpus &&
1216 		    emrp->mrp_ncpus < MRP_NCPUS; i++) {
1217 			for (j = 0; j < emrp->mrp_ncpus; j++) {
1218 				if (emrp->mrp_cpu[j] == srs_cpu->mc_cpus[i])
1219 					break;
1220 			}
1221 			if (j == emrp->mrp_ncpus) {
1222 				emrp->mrp_cpu[emrp->mrp_ncpus++] =
1223 				    srs_cpu->mc_cpus[i];
1224 			}
1225 		}
1226 	}
1227 
1228 	mac_tx_cpu_init(flent, NULL, cpupart);
1229 }
1230 
1231 /*
1232  * DATAPATH SETUP ROUTINES
1233  * (setup SRS and set/update FANOUT, B/W and PRIORITY)
1234  */
1235 
1236 /*
1237  * mac_srs_fanout_list_alloc:
1238  *
1239  * The underlying device can expose upto MAX_RINGS_PER_GROUP worth of
1240  * rings to a client. In such a case, MAX_RINGS_PER_GROUP worth of
1241  * array space is needed to store Tx soft rings. Thus we allocate so
1242  * much array space for srs_tx_soft_rings.
1243  *
1244  * And when it is an aggr, again we allocate MAX_RINGS_PER_GROUP worth
1245  * of space to st_soft_rings. This array is used for quick access to
1246  * soft ring associated with a pseudo Tx ring based on the pseudo
1247  * ring's index (mr_index).
1248  */
1249 static void
1250 mac_srs_fanout_list_alloc(mac_soft_ring_set_t *mac_srs)
1251 {
1252 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
1253 
1254 	if (mac_srs->srs_type & SRST_TX) {
1255 		mac_srs->srs_tx_soft_rings = (mac_soft_ring_t **)
1256 		    kmem_zalloc(sizeof (mac_soft_ring_t *) *
1257 		    MAX_RINGS_PER_GROUP, KM_SLEEP);
1258 		if (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT) {
1259 			mac_srs_tx_t *tx = &mac_srs->srs_tx;
1260 
1261 			tx->st_soft_rings = (mac_soft_ring_t **)
1262 			    kmem_zalloc(sizeof (mac_soft_ring_t *) *
1263 			    MAX_RINGS_PER_GROUP, KM_SLEEP);
1264 		}
1265 	} else {
1266 		mac_srs->srs_tcp_soft_rings = (mac_soft_ring_t **)
1267 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1268 		    KM_SLEEP);
1269 		mac_srs->srs_tcp6_soft_rings = (mac_soft_ring_t **)
1270 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1271 		    KM_SLEEP);
1272 		mac_srs->srs_udp_soft_rings = (mac_soft_ring_t **)
1273 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1274 		    KM_SLEEP);
1275 		mac_srs->srs_udp6_soft_rings = (mac_soft_ring_t **)
1276 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1277 		    KM_SLEEP);
1278 		mac_srs->srs_oth_soft_rings = (mac_soft_ring_t **)
1279 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1280 		    KM_SLEEP);
1281 	}
1282 }
1283 
1284 static void
1285 mac_srs_worker_bind(mac_soft_ring_set_t *mac_srs, processorid_t cpuid)
1286 {
1287 	cpu_t *cp;
1288 	boolean_t clear = B_FALSE;
1289 
1290 	ASSERT(MUTEX_HELD(&cpu_lock));
1291 
1292 	if (!mac_srs_thread_bind)
1293 		return;
1294 
1295 	cp = cpu_get(cpuid);
1296 	if (cp == NULL || !cpu_is_online(cp))
1297 		return;
1298 
1299 	mutex_enter(&mac_srs->srs_lock);
1300 	mac_srs->srs_state |= SRS_WORKER_BOUND;
1301 	if (mac_srs->srs_worker_cpuid != -1)
1302 		clear = B_TRUE;
1303 	mac_srs->srs_worker_cpuid = cpuid;
1304 	mutex_exit(&mac_srs->srs_lock);
1305 
1306 	if (clear)
1307 		thread_affinity_clear(mac_srs->srs_worker);
1308 
1309 	thread_affinity_set(mac_srs->srs_worker, cpuid);
1310 	DTRACE_PROBE1(worker__CPU, processorid_t, cpuid);
1311 }
1312 
1313 static void
1314 mac_srs_poll_bind(mac_soft_ring_set_t *mac_srs, processorid_t cpuid)
1315 {
1316 	cpu_t *cp;
1317 	boolean_t clear = B_FALSE;
1318 
1319 	ASSERT(MUTEX_HELD(&cpu_lock));
1320 
1321 	if (!mac_srs_thread_bind || mac_srs->srs_poll_thr == NULL)
1322 		return;
1323 
1324 	cp = cpu_get(cpuid);
1325 	if (cp == NULL || !cpu_is_online(cp))
1326 		return;
1327 
1328 	mutex_enter(&mac_srs->srs_lock);
1329 	mac_srs->srs_state |= SRS_POLL_BOUND;
1330 	if (mac_srs->srs_poll_cpuid != -1)
1331 		clear = B_TRUE;
1332 	mac_srs->srs_poll_cpuid = cpuid;
1333 	mutex_exit(&mac_srs->srs_lock);
1334 
1335 	if (clear)
1336 		thread_affinity_clear(mac_srs->srs_poll_thr);
1337 
1338 	thread_affinity_set(mac_srs->srs_poll_thr, cpuid);
1339 	DTRACE_PROBE1(poll__CPU, processorid_t, cpuid);
1340 }
1341 
1342 /*
1343  * Re-target interrupt to the passed CPU. If re-target is successful,
1344  * set mc_rx_intr_cpu to the re-targeted CPU. Otherwise set it to -1.
1345  */
1346 void
1347 mac_rx_srs_retarget_intr(mac_soft_ring_set_t *mac_srs, processorid_t cpuid)
1348 {
1349 	cpu_t *cp;
1350 	mac_ring_t *ring = mac_srs->srs_ring;
1351 	mac_intr_t *mintr = &ring->mr_info.mri_intr;
1352 	flow_entry_t *flent = mac_srs->srs_flent;
1353 	boolean_t primary = mac_is_primary_client(mac_srs->srs_mcip);
1354 
1355 	ASSERT(MUTEX_HELD(&cpu_lock));
1356 
1357 	/*
1358 	 * Don't re-target the interrupt for these cases:
1359 	 * 1) ring is NULL
1360 	 * 2) the interrupt is shared (mi_ddi_shared)
1361 	 * 3) ddi_handle is NULL and !primary
1362 	 * 4) primary, ddi_handle is NULL but fe_rx_srs_cnt > 2
1363 	 * Case 3 & 4 are because of mac_client_intr_cpu() routine.
1364 	 * This routine will re-target fixed interrupt for primary
1365 	 * mac client if the client has only one ring. In that
1366 	 * case, mc_rx_intr_cpu will already have the correct value.
1367 	 */
1368 	if (ring == NULL || mintr->mi_ddi_shared || cpuid == -1 ||
1369 	    (mintr->mi_ddi_handle == NULL && !primary) || (primary &&
1370 	    mintr->mi_ddi_handle == NULL && flent->fe_rx_srs_cnt > 2)) {
1371 		mac_srs->srs_cpu.mc_rx_intr_cpu = -1;
1372 		return;
1373 	}
1374 
1375 	if (mintr->mi_ddi_handle == NULL)
1376 		return;
1377 
1378 	cp = cpu_get(cpuid);
1379 	if (cp == NULL || !cpu_is_online(cp))
1380 		return;
1381 
1382 	/* Drop the cpu_lock as set_intr_affinity() holds it */
1383 	mutex_exit(&cpu_lock);
1384 	if (set_intr_affinity(mintr->mi_ddi_handle, cpuid) == DDI_SUCCESS)
1385 		mac_srs->srs_cpu.mc_rx_intr_cpu = cpuid;
1386 	else
1387 		mac_srs->srs_cpu.mc_rx_intr_cpu = -1;
1388 	mutex_enter(&cpu_lock);
1389 }
1390 
1391 /*
1392  * Re-target Tx interrupts
1393  */
1394 void
1395 mac_tx_srs_retarget_intr(mac_soft_ring_set_t *mac_srs)
1396 {
1397 	cpu_t *cp;
1398 	mac_ring_t *ring;
1399 	mac_intr_t *mintr;
1400 	mac_soft_ring_t *sringp;
1401 	mac_srs_tx_t *srs_tx;
1402 	mac_cpus_t *srs_cpu;
1403 	processorid_t cpuid;
1404 	int i;
1405 
1406 	ASSERT(MUTEX_HELD(&cpu_lock));
1407 
1408 	srs_cpu = &mac_srs->srs_cpu;
1409 	if (MAC_TX_SOFT_RINGS(mac_srs)) {
1410 		for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
1411 			sringp = mac_srs->srs_tx_soft_rings[i];
1412 			ring = (mac_ring_t *)sringp->s_ring_tx_arg2;
1413 			cpuid = srs_cpu->mc_tx_intr_cpu[i];
1414 			cp = cpu_get(cpuid);
1415 			if (cp == NULL || !cpu_is_online(cp) ||
1416 			    !MAC_RING_RETARGETABLE(ring)) {
1417 				srs_cpu->mc_tx_retargeted_cpu[i] = -1;
1418 				continue;
1419 			}
1420 			mintr = &ring->mr_info.mri_intr;
1421 			/*
1422 			 * Drop the cpu_lock as set_intr_affinity()
1423 			 * holds it
1424 			 */
1425 			mutex_exit(&cpu_lock);
1426 			if (set_intr_affinity(mintr->mi_ddi_handle,
1427 			    cpuid) == DDI_SUCCESS) {
1428 				srs_cpu->mc_tx_retargeted_cpu[i] = cpuid;
1429 			} else {
1430 				srs_cpu->mc_tx_retargeted_cpu[i] = -1;
1431 			}
1432 			mutex_enter(&cpu_lock);
1433 		}
1434 	} else {
1435 		cpuid = srs_cpu->mc_tx_intr_cpu[0];
1436 		cp = cpu_get(cpuid);
1437 		if (cp == NULL || !cpu_is_online(cp)) {
1438 			srs_cpu->mc_tx_retargeted_cpu[0] = -1;
1439 			return;
1440 		}
1441 		srs_tx = &mac_srs->srs_tx;
1442 		ring = (mac_ring_t *)srs_tx->st_arg2;
1443 		if (MAC_RING_RETARGETABLE(ring)) {
1444 			mintr = &ring->mr_info.mri_intr;
1445 			mutex_exit(&cpu_lock);
1446 			if ((set_intr_affinity(mintr->mi_ddi_handle,
1447 			    cpuid) == DDI_SUCCESS)) {
1448 				srs_cpu->mc_tx_retargeted_cpu[0] = cpuid;
1449 			} else {
1450 				srs_cpu->mc_tx_retargeted_cpu[0] = -1;
1451 			}
1452 			mutex_enter(&cpu_lock);
1453 		}
1454 	}
1455 }
1456 
1457 /*
1458  * When a CPU comes back online, bind the MAC kernel threads which
1459  * were previously bound to that CPU, and had to be unbound because
1460  * the CPU was going away.
1461  *
1462  * These functions are called with cpu_lock held and hence we can't
1463  * cv_wait to grab the mac perimeter. Since these functions walk the soft
1464  * ring list of an SRS without being in the perimeter, the list itself
1465  * is protected by the SRS lock.
1466  */
1467 static void
1468 mac_walk_srs_and_bind(int cpuid)
1469 {
1470 	mac_soft_ring_set_t *mac_srs;
1471 	mac_soft_ring_t *soft_ring;
1472 
1473 	rw_enter(&mac_srs_g_lock, RW_READER);
1474 
1475 	if ((mac_srs = mac_srs_g_list) == NULL)
1476 		goto done;
1477 
1478 	for (; mac_srs != NULL; mac_srs = mac_srs->srs_next) {
1479 		if (mac_srs->srs_worker_cpuid == -1 &&
1480 		    mac_srs->srs_worker_cpuid_save == cpuid) {
1481 			mac_srs->srs_worker_cpuid_save = -1;
1482 			mac_srs_worker_bind(mac_srs, cpuid);
1483 		}
1484 
1485 		if (!(mac_srs->srs_type & SRST_TX)) {
1486 			if (mac_srs->srs_poll_cpuid == -1 &&
1487 			    mac_srs->srs_poll_cpuid_save == cpuid) {
1488 				mac_srs->srs_poll_cpuid_save = -1;
1489 				mac_srs_poll_bind(mac_srs, cpuid);
1490 			}
1491 		}
1492 
1493 		/* Next tackle the soft rings associated with the srs */
1494 		mutex_enter(&mac_srs->srs_lock);
1495 		for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
1496 		    soft_ring = soft_ring->s_ring_next) {
1497 			if (soft_ring->s_ring_cpuid == -1 &&
1498 			    soft_ring->s_ring_cpuid_save == cpuid) {
1499 				soft_ring->s_ring_cpuid_save = -1;
1500 				(void) mac_soft_ring_bind(soft_ring, cpuid);
1501 			}
1502 		}
1503 		mutex_exit(&mac_srs->srs_lock);
1504 	}
1505 done:
1506 	rw_exit(&mac_srs_g_lock);
1507 }
1508 
1509 /*
1510  * Change the priority of the SRS's poll and worker thread. Additionally,
1511  * update the priority of the worker threads for the SRS's soft rings.
1512  * Need to modify any associated squeue threads.
1513  */
1514 void
1515 mac_update_srs_priority(mac_soft_ring_set_t *mac_srs, pri_t prival)
1516 {
1517 	mac_soft_ring_t		*ringp;
1518 
1519 	mac_srs->srs_pri = prival;
1520 	thread_lock(mac_srs->srs_worker);
1521 	(void) thread_change_pri(mac_srs->srs_worker, mac_srs->srs_pri, 0);
1522 	thread_unlock(mac_srs->srs_worker);
1523 	if (mac_srs->srs_poll_thr != NULL) {
1524 		thread_lock(mac_srs->srs_poll_thr);
1525 		(void) thread_change_pri(mac_srs->srs_poll_thr,
1526 		    mac_srs->srs_pri, 0);
1527 		thread_unlock(mac_srs->srs_poll_thr);
1528 	}
1529 	if ((ringp = mac_srs->srs_soft_ring_head) == NULL)
1530 		return;
1531 	while (ringp != mac_srs->srs_soft_ring_tail) {
1532 		thread_lock(ringp->s_ring_worker);
1533 		(void) thread_change_pri(ringp->s_ring_worker,
1534 		    mac_srs->srs_pri, 0);
1535 		thread_unlock(ringp->s_ring_worker);
1536 		ringp = ringp->s_ring_next;
1537 	}
1538 	ASSERT(ringp == mac_srs->srs_soft_ring_tail);
1539 	thread_lock(ringp->s_ring_worker);
1540 	(void) thread_change_pri(ringp->s_ring_worker, mac_srs->srs_pri, 0);
1541 	thread_unlock(ringp->s_ring_worker);
1542 }
1543 
1544 /*
1545  * Change the receive bandwidth limit.
1546  */
1547 static void
1548 mac_rx_srs_update_bwlimit(mac_soft_ring_set_t *srs, mac_resource_props_t *mrp)
1549 {
1550 	mac_soft_ring_t		*softring;
1551 
1552 	mutex_enter(&srs->srs_lock);
1553 	mutex_enter(&srs->srs_bw->mac_bw_lock);
1554 
1555 	if (mrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
1556 		/* Reset bandwidth limit */
1557 		if (srs->srs_type & SRST_BW_CONTROL) {
1558 			srs->srs_type &= ~SRST_BW_CONTROL;
1559 			srs->srs_drain_func = mac_rx_srs_drain;
1560 		}
1561 	} else {
1562 		/* Set/Modify bandwidth limit */
1563 		srs->srs_bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
1564 		/*
1565 		 * Give twice the queuing capability before
1566 		 * dropping packets. The unit is bytes/tick.
1567 		 */
1568 		srs->srs_bw->mac_bw_drop_threshold =
1569 		    srs->srs_bw->mac_bw_limit << 1;
1570 		if (!(srs->srs_type & SRST_BW_CONTROL)) {
1571 			srs->srs_type |= SRST_BW_CONTROL;
1572 			srs->srs_drain_func = mac_rx_srs_drain_bw;
1573 		}
1574 	}
1575 
1576 	mutex_exit(&srs->srs_bw->mac_bw_lock);
1577 	mutex_exit(&srs->srs_lock);
1578 }
1579 
1580 /* Change the transmit bandwidth limit */
1581 static void
1582 mac_tx_srs_update_bwlimit(mac_soft_ring_set_t *srs, mac_resource_props_t *mrp)
1583 {
1584 	uint32_t		tx_mode, ring_info = 0;
1585 	mac_srs_tx_t		*srs_tx = &srs->srs_tx;
1586 	mac_client_impl_t	*mcip = srs->srs_mcip;
1587 
1588 	/*
1589 	 * We need to quiesce/restart the client here because mac_tx() and
1590 	 * srs->srs_tx->st_func do not hold srs->srs_lock while accessing
1591 	 * st_mode and related fields, which are modified by the code below.
1592 	 */
1593 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
1594 
1595 	mutex_enter(&srs->srs_lock);
1596 	mutex_enter(&srs->srs_bw->mac_bw_lock);
1597 
1598 	tx_mode = srs_tx->st_mode;
1599 	if (mrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
1600 		/* Reset bandwidth limit */
1601 		if (tx_mode == SRS_TX_BW) {
1602 			if (srs_tx->st_arg2 != NULL)
1603 				ring_info = mac_hwring_getinfo(srs_tx->st_arg2);
1604 			if (mac_tx_serialize ||
1605 			    (ring_info & MAC_RING_TX_SERIALIZE)) {
1606 				srs_tx->st_mode = SRS_TX_SERIALIZE;
1607 			} else {
1608 				srs_tx->st_mode = SRS_TX_DEFAULT;
1609 			}
1610 		} else if (tx_mode == SRS_TX_BW_FANOUT) {
1611 			srs_tx->st_mode = SRS_TX_FANOUT;
1612 		} else if (tx_mode == SRS_TX_BW_AGGR) {
1613 			srs_tx->st_mode = SRS_TX_AGGR;
1614 		}
1615 		srs->srs_type &= ~SRST_BW_CONTROL;
1616 	} else {
1617 		/* Set/Modify bandwidth limit */
1618 		srs->srs_bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
1619 		/*
1620 		 * Give twice the queuing capability before
1621 		 * dropping packets. The unit is bytes/tick.
1622 		 */
1623 		srs->srs_bw->mac_bw_drop_threshold =
1624 		    srs->srs_bw->mac_bw_limit << 1;
1625 		srs->srs_type |= SRST_BW_CONTROL;
1626 		if (tx_mode != SRS_TX_BW && tx_mode != SRS_TX_BW_FANOUT &&
1627 		    tx_mode != SRS_TX_BW_AGGR) {
1628 			if (tx_mode == SRS_TX_SERIALIZE ||
1629 			    tx_mode == SRS_TX_DEFAULT) {
1630 				srs_tx->st_mode = SRS_TX_BW;
1631 			} else if (tx_mode == SRS_TX_FANOUT) {
1632 				srs_tx->st_mode = SRS_TX_BW_FANOUT;
1633 			} else if (tx_mode == SRS_TX_AGGR) {
1634 				srs_tx->st_mode = SRS_TX_BW_AGGR;
1635 			} else {
1636 				ASSERT(0);
1637 			}
1638 		}
1639 	}
1640 
1641 	srs_tx->st_func = mac_tx_get_func(srs_tx->st_mode);
1642 	mutex_exit(&srs->srs_bw->mac_bw_lock);
1643 	mutex_exit(&srs->srs_lock);
1644 
1645 	mac_tx_client_restart((mac_client_handle_t)mcip);
1646 }
1647 
1648 /*
1649  * The uber function that deals with any update to bandwidth limits.
1650  */
1651 void
1652 mac_srs_update_bwlimit(flow_entry_t *flent, mac_resource_props_t *mrp)
1653 {
1654 	int			count;
1655 
1656 	for (count = 0; count < flent->fe_rx_srs_cnt; count++)
1657 		mac_rx_srs_update_bwlimit(flent->fe_rx_srs[count], mrp);
1658 	mac_tx_srs_update_bwlimit(flent->fe_tx_srs, mrp);
1659 }
1660 
1661 /*
1662  * When the first sub-flow is added to a link, we disable polling on the
1663  * link and also modify the entry point to mac_rx_srs_subflow_process().
1664  * (polling is disabled because with the subflow added, accounting
1665  * for polling needs additional logic, it is assumed that when a subflow is
1666  * added, we can take some hit as a result of disabling polling rather than
1667  * adding more complexity - if this becomes a perf. issue we need to
1668  * re-rvaluate this logic).  When the last subflow is removed, we turn back
1669  * polling and also reset the entry point to mac_rx_srs_process().
1670  *
1671  * In the future if there are multiple SRS, we can simply
1672  * take one and give it to the flow rather than disabling polling and
1673  * resetting the entry point.
1674  */
1675 void
1676 mac_client_update_classifier(mac_client_impl_t *mcip, boolean_t enable)
1677 {
1678 	flow_entry_t		*flent = mcip->mci_flent;
1679 	int			i;
1680 	mac_impl_t		*mip = mcip->mci_mip;
1681 	mac_rx_func_t		rx_func;
1682 	uint_t			rx_srs_cnt;
1683 	boolean_t		enable_classifier;
1684 
1685 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1686 
1687 	enable_classifier = !FLOW_TAB_EMPTY(mcip->mci_subflow_tab) && enable;
1688 
1689 	rx_func = enable_classifier ? mac_rx_srs_subflow_process :
1690 	    mac_rx_srs_process;
1691 
1692 	/* Tell mac_srs_poll_state_change to disable polling if necessary */
1693 	if (mip->mi_state_flags & MIS_POLL_DISABLE)
1694 		enable_classifier = B_TRUE;
1695 
1696 	/*
1697 	 * If receive function has already been configured correctly for
1698 	 * current subflow configuration, do nothing.
1699 	 */
1700 	if (flent->fe_cb_fn == (flow_fn_t)rx_func)
1701 		return;
1702 
1703 	rx_srs_cnt = flent->fe_rx_srs_cnt;
1704 	for (i = 0; i < rx_srs_cnt; i++) {
1705 		ASSERT(flent->fe_rx_srs[i] != NULL);
1706 		mac_srs_poll_state_change(flent->fe_rx_srs[i],
1707 		    enable_classifier, rx_func);
1708 	}
1709 
1710 	/*
1711 	 * Change the S/W classifier so that we can land in the
1712 	 * correct processing function with correct argument.
1713 	 * If all subflows have been removed we can revert to
1714 	 * mac_rx_srs_process(), else we need mac_rx_srs_subflow_process().
1715 	 */
1716 	mutex_enter(&flent->fe_lock);
1717 	flent->fe_cb_fn = (flow_fn_t)rx_func;
1718 	flent->fe_cb_arg1 = (void *)mip;
1719 	flent->fe_cb_arg2 = flent->fe_rx_srs[0];
1720 	mutex_exit(&flent->fe_lock);
1721 }
1722 
1723 static void
1724 mac_srs_update_fanout_list(mac_soft_ring_set_t *mac_srs)
1725 {
1726 	int tcp_count = 0, tcp6_count = 0, udp_count = 0, udp6_count = 0,
1727 	    oth_count = 0, tx_count = 0;
1728 
1729 	mac_soft_ring_t *softring;
1730 
1731 	softring = mac_srs->srs_soft_ring_head;
1732 	if (softring == NULL) {
1733 		ASSERT(mac_srs->srs_soft_ring_count == 0);
1734 		mac_srs->srs_tcp_ring_count = 0;
1735 		mac_srs->srs_udp_ring_count = 0;
1736 		mac_srs->srs_tcp6_ring_count = 0;
1737 		mac_srs->srs_udp6_ring_count = 0;
1738 		mac_srs->srs_oth_ring_count = 0;
1739 		mac_srs->srs_tx_ring_count = 0;
1740 		return;
1741 	}
1742 
1743 	while (softring != NULL) {
1744 		if (softring->s_ring_type & ST_RING_TCP) {
1745 			mac_srs->srs_tcp_soft_rings[tcp_count++] = softring;
1746 		} else if (softring->s_ring_type & ST_RING_TCP6) {
1747 			mac_srs->srs_tcp6_soft_rings[tcp6_count++] = softring;
1748 		} else if (softring->s_ring_type & ST_RING_UDP) {
1749 			mac_srs->srs_udp_soft_rings[udp_count++] = softring;
1750 		} else if (softring->s_ring_type & ST_RING_UDP6) {
1751 			mac_srs->srs_udp6_soft_rings[udp6_count++] = softring;
1752 		} else if (softring->s_ring_type & ST_RING_OTH) {
1753 			mac_srs->srs_oth_soft_rings[oth_count++] = softring;
1754 		} else {
1755 			ASSERT(softring->s_ring_type & ST_RING_TX);
1756 			mac_srs->srs_tx_soft_rings[tx_count++] = softring;
1757 		}
1758 		softring = softring->s_ring_next;
1759 	}
1760 
1761 	ASSERT(mac_srs->srs_soft_ring_count == (tcp_count + tcp6_count +
1762 	    udp_count + udp6_count + oth_count + tx_count));
1763 	mac_srs->srs_tcp_ring_count = tcp_count;
1764 	mac_srs->srs_tcp6_ring_count = tcp6_count;
1765 	mac_srs->srs_udp_ring_count = udp_count;
1766 	mac_srs->srs_udp6_ring_count = udp6_count;
1767 	mac_srs->srs_oth_ring_count = oth_count;
1768 	mac_srs->srs_tx_ring_count = tx_count;
1769 }
1770 
1771 void
1772 mac_srs_create_proto_softrings(int id, pri_t pri, mac_client_impl_t *mcip,
1773     mac_soft_ring_set_t *mac_srs, processorid_t cpuid, mac_direct_rx_t rx_func,
1774     void *x_arg1, mac_resource_handle_t x_arg2, boolean_t set_bypass)
1775 {
1776 	mac_soft_ring_t	*softring;
1777 	mac_rx_fifo_t	mrf;
1778 
1779 	bzero(&mrf, sizeof (mac_rx_fifo_t));
1780 	mrf.mrf_type = MAC_RX_FIFO;
1781 	mrf.mrf_receive = (mac_receive_t)mac_soft_ring_poll;
1782 	mrf.mrf_intr_enable = (mac_intr_enable_t)mac_soft_ring_intr_enable;
1783 	mrf.mrf_intr_disable = (mac_intr_disable_t)mac_soft_ring_intr_disable;
1784 	mrf.mrf_flow_priority = pri;
1785 
1786 	softring = mac_soft_ring_create(id, mac_soft_ring_worker_wait,
1787 	    ST_RING_TCP, pri, mcip, mac_srs,
1788 	    cpuid, rx_func, x_arg1, x_arg2);
1789 	softring->s_ring_rx_arg2 = NULL;
1790 
1791 	/*
1792 	 * TCP and UDP support DLS bypass. In addition TCP
1793 	 * squeue can also poll their corresponding soft rings.
1794 	 */
1795 	if (set_bypass && mcip->mci_direct_rx.mdrx_v4 != NULL &&
1796 	    (mcip->mci_rcb4.mrc_arg != NULL)) {
1797 		mac_soft_ring_dls_bypass_enable(softring,
1798 		    mcip->mci_direct_rx.mdrx_v4,
1799 		    mcip->mci_direct_rx.mdrx_arg_v4);
1800 
1801 		mrf.mrf_rx_arg = softring;
1802 		mrf.mrf_intr_handle = (mac_intr_handle_t)softring;
1803 
1804 		/*
1805 		 * Make a call in IP to get a TCP squeue assigned to
1806 		 * this softring to maintain full CPU locality through
1807 		 * the stack and allow the squeue to be able to poll
1808 		 * the softring so the flow control can be pushed
1809 		 * all the way to H/W.
1810 		 */
1811 		softring->s_ring_rx_arg2 = mcip->mci_rcb4.mrc_add(
1812 		    mcip->mci_rcb4.mrc_arg, (mac_resource_t *)&mrf);
1813 	}
1814 
1815 	/*
1816 	 * Non-TCP protocols don't support squeues. Hence we
1817 	 * don't make any ring addition callbacks for non-TCP
1818 	 * rings. Now create the UDP softring and allow it to
1819 	 * bypass the DLS layer.
1820 	 */
1821 	softring = mac_soft_ring_create(id, mac_soft_ring_worker_wait,
1822 	    ST_RING_UDP, pri, mcip, mac_srs,
1823 	    cpuid, rx_func, x_arg1, x_arg2);
1824 	softring->s_ring_rx_arg2 = NULL;
1825 
1826 	if (set_bypass && mcip->mci_direct_rx.mdrx_v4 != NULL) {
1827 		mac_soft_ring_dls_bypass_enable(softring,
1828 		    mcip->mci_direct_rx.mdrx_v4,
1829 		    mcip->mci_direct_rx.mdrx_arg_v4);
1830 	}
1831 
1832 	/* TCP for IPv6. */
1833 	softring = mac_soft_ring_create(id, mac_soft_ring_worker_wait,
1834 	    ST_RING_TCP6, pri, mcip, mac_srs,
1835 	    cpuid, rx_func, x_arg1, x_arg2);
1836 	softring->s_ring_rx_arg2 = NULL;
1837 
1838 	if (set_bypass && mcip->mci_direct_rx.mdrx_v6 != NULL &&
1839 	    (mcip->mci_rcb6.mrc_arg != NULL)) {
1840 		mac_soft_ring_dls_bypass_enable(softring,
1841 		    mcip->mci_direct_rx.mdrx_v6,
1842 		    mcip->mci_direct_rx.mdrx_arg_v6);
1843 
1844 		mrf.mrf_rx_arg = softring;
1845 		mrf.mrf_intr_handle = (mac_intr_handle_t)softring;
1846 		softring->s_ring_rx_arg2 = mcip->mci_rcb6.mrc_add(
1847 		    mcip->mci_rcb6.mrc_arg, (mac_resource_t *)&mrf);
1848 	}
1849 
1850 	/* UDP for IPv6. */
1851 	softring = mac_soft_ring_create(id, mac_soft_ring_worker_wait,
1852 	    ST_RING_UDP6, pri, mcip, mac_srs,
1853 	    cpuid, rx_func, x_arg1, x_arg2);
1854 	softring->s_ring_rx_arg2 = NULL;
1855 
1856 	if (set_bypass && mcip->mci_direct_rx.mdrx_v6 != NULL) {
1857 		mac_soft_ring_dls_bypass_enable(softring,
1858 		    mcip->mci_direct_rx.mdrx_v6,
1859 		    mcip->mci_direct_rx.mdrx_arg_v6);
1860 	}
1861 
1862 	/* Create the Oth softrings which has to go through the DLS. */
1863 	softring = mac_soft_ring_create(id, mac_soft_ring_worker_wait,
1864 	    ST_RING_OTH, pri, mcip, mac_srs,
1865 	    cpuid, rx_func, x_arg1, x_arg2);
1866 	softring->s_ring_rx_arg2 = NULL;
1867 }
1868 
1869 /*
1870  * This routine associates a CPU or a set of CPU to process incoming
1871  * traffic from a mac client. If multiple CPUs are specified, then
1872  * so many soft rings are created with each soft ring worker thread
1873  * bound to a CPU in the set. Each soft ring in turn will be
1874  * associated with an squeue and the squeue will be moved to the
1875  * same CPU as that of the soft ring's.
1876  */
1877 static void
1878 mac_srs_fanout_modify(mac_client_impl_t *mcip, mac_direct_rx_t rx_func,
1879     void *x_arg1, mac_resource_handle_t x_arg2,
1880     mac_soft_ring_set_t *mac_rx_srs, mac_soft_ring_set_t *mac_tx_srs)
1881 {
1882 	mac_soft_ring_t *softring;
1883 	processorid_t cpuid = -1;
1884 	int i, srings_present, new_fanout_cnt;
1885 	mac_cpus_t *srs_cpu;
1886 
1887 	/* fanout state is REINIT. Set it back to INIT */
1888 	ASSERT(mac_rx_srs->srs_fanout_state == SRS_FANOUT_REINIT);
1889 	mac_rx_srs->srs_fanout_state = SRS_FANOUT_INIT;
1890 
1891 	/* how many are present right now */
1892 	srings_present = mac_rx_srs->srs_tcp_ring_count;
1893 	/* new request */
1894 	srs_cpu = &mac_rx_srs->srs_cpu;
1895 	new_fanout_cnt = srs_cpu->mc_rx_fanout_cnt;
1896 
1897 	if (new_fanout_cnt > srings_present) {
1898 		/* soft rings increased */
1899 		mutex_enter(&mac_rx_srs->srs_lock);
1900 		mac_rx_srs->srs_type |= SRST_FANOUT_SRC_IP;
1901 		mutex_exit(&mac_rx_srs->srs_lock);
1902 
1903 		for (i = mac_rx_srs->srs_tcp_ring_count;
1904 		    i < new_fanout_cnt; i++) {
1905 			/*
1906 			 * Create the protocol softrings and set the
1907 			 * DLS bypass where possible.
1908 			 */
1909 			mac_srs_create_proto_softrings(i, mac_rx_srs->srs_pri,
1910 			    mcip, mac_rx_srs, cpuid, rx_func, x_arg1, x_arg2,
1911 			    B_TRUE);
1912 		}
1913 		mac_srs_update_fanout_list(mac_rx_srs);
1914 	} else if (new_fanout_cnt < srings_present) {
1915 		/* soft rings decreased */
1916 		if (new_fanout_cnt == 1) {
1917 			mutex_enter(&mac_rx_srs->srs_lock);
1918 			mac_rx_srs->srs_type &= ~SRST_FANOUT_SRC_IP;
1919 			ASSERT(mac_rx_srs->srs_type & SRST_FANOUT_PROTO);
1920 			mutex_exit(&mac_rx_srs->srs_lock);
1921 		}
1922 		/* Get rid of extra soft rings */
1923 		for (i = new_fanout_cnt;
1924 		    i < mac_rx_srs->srs_tcp_ring_count; i++) {
1925 			softring = mac_rx_srs->srs_tcp_soft_rings[i];
1926 			if (softring->s_ring_rx_arg2 != NULL) {
1927 				mcip->mci_rcb4.mrc_remove(
1928 				    mcip->mci_rcb4.mrc_arg,
1929 				    softring->s_ring_rx_arg2);
1930 			}
1931 			softring = mac_rx_srs->srs_tcp6_soft_rings[i];
1932 			if (softring->s_ring_rx_arg2 != NULL) {
1933 				mcip->mci_rcb6.mrc_remove(
1934 				    mcip->mci_rcb6.mrc_arg,
1935 				    softring->s_ring_rx_arg2);
1936 			}
1937 			mac_soft_ring_remove(mac_rx_srs,
1938 			    mac_rx_srs->srs_tcp_soft_rings[i]);
1939 			mac_soft_ring_remove(mac_rx_srs,
1940 			    mac_rx_srs->srs_tcp6_soft_rings[i]);
1941 			mac_soft_ring_remove(mac_rx_srs,
1942 			    mac_rx_srs->srs_udp_soft_rings[i]);
1943 			mac_soft_ring_remove(mac_rx_srs,
1944 			    mac_rx_srs->srs_udp6_soft_rings[i]);
1945 			mac_soft_ring_remove(mac_rx_srs,
1946 			    mac_rx_srs->srs_oth_soft_rings[i]);
1947 		}
1948 		mac_srs_update_fanout_list(mac_rx_srs);
1949 	}
1950 
1951 	ASSERT(new_fanout_cnt == mac_rx_srs->srs_tcp_ring_count);
1952 	mutex_enter(&cpu_lock);
1953 	for (i = 0; i < mac_rx_srs->srs_tcp_ring_count; i++) {
1954 		cpuid = srs_cpu->mc_rx_fanout_cpus[i];
1955 		(void) mac_soft_ring_bind(mac_rx_srs->srs_udp_soft_rings[i],
1956 		    cpuid);
1957 		(void) mac_soft_ring_bind(mac_rx_srs->srs_udp6_soft_rings[i],
1958 		    cpuid);
1959 		(void) mac_soft_ring_bind(mac_rx_srs->srs_oth_soft_rings[i],
1960 		    cpuid);
1961 		(void) mac_soft_ring_bind(mac_rx_srs->srs_tcp_soft_rings[i],
1962 		    cpuid);
1963 		(void) mac_soft_ring_bind(mac_rx_srs->srs_tcp6_soft_rings[i],
1964 		    cpuid);
1965 		softring = mac_rx_srs->srs_tcp_soft_rings[i];
1966 		if (softring->s_ring_rx_arg2 != NULL) {
1967 			mcip->mci_rcb4.mrc_bind(mcip->mci_rcb4.mrc_arg,
1968 			    softring->s_ring_rx_arg2, cpuid);
1969 		}
1970 		softring = mac_rx_srs->srs_tcp6_soft_rings[i];
1971 		if (softring->s_ring_rx_arg2 != NULL) {
1972 			mcip->mci_rcb6.mrc_bind(mcip->mci_rcb6.mrc_arg,
1973 			    softring->s_ring_rx_arg2, cpuid);
1974 		}
1975 	}
1976 
1977 	mac_srs_worker_bind(mac_rx_srs, srs_cpu->mc_rx_workerid);
1978 	mac_srs_poll_bind(mac_rx_srs, srs_cpu->mc_rx_pollid);
1979 	mac_rx_srs_retarget_intr(mac_rx_srs, srs_cpu->mc_rx_intr_cpu);
1980 	/*
1981 	 * Bind Tx srs and soft ring threads too. Let's bind tx
1982 	 * srs to the last cpu in mrp list.
1983 	 */
1984 	if (mac_tx_srs != NULL) {
1985 		BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp);
1986 		mac_tx_srs_retarget_intr(mac_tx_srs);
1987 	}
1988 	mutex_exit(&cpu_lock);
1989 }
1990 
1991 /*
1992  * Bind SRS threads and soft rings to CPUs/create fanout list.
1993  */
1994 void
1995 mac_srs_fanout_init(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
1996     mac_direct_rx_t rx_func, void *x_arg1, mac_resource_handle_t x_arg2,
1997     mac_soft_ring_set_t *mac_rx_srs, mac_soft_ring_set_t *mac_tx_srs,
1998     cpupart_t *cpupart)
1999 {
2000 	int		i;
2001 	processorid_t	cpuid;
2002 	int soft_ring_cnt;
2003 	mac_cpus_t *srs_cpu = &mac_rx_srs->srs_cpu;
2004 
2005 	/*
2006 	 * Remove the no soft ring flag and we will adjust it
2007 	 * appropriately further down.
2008 	 */
2009 	mutex_enter(&mac_rx_srs->srs_lock);
2010 	mac_rx_srs->srs_type &= ~SRST_NO_SOFT_RINGS;
2011 	mutex_exit(&mac_rx_srs->srs_lock);
2012 
2013 	ASSERT(mac_rx_srs->srs_soft_ring_head == NULL);
2014 	ASSERT(mac_rx_srs->srs_fanout_state == SRS_FANOUT_UNINIT);
2015 	mac_rx_srs->srs_fanout_state = SRS_FANOUT_INIT;
2016 	/*
2017 	 * Ring count can be 0 if no fanout is required and no cpu
2018 	 * were specified. Leave the SRS worker and poll thread
2019 	 * unbound
2020 	 */
2021 	ASSERT(mrp != NULL);
2022 	soft_ring_cnt = srs_cpu->mc_rx_fanout_cnt;
2023 
2024 	/* Step 1: bind cpu contains cpu list where threads need to bind */
2025 	if (soft_ring_cnt > 0) {
2026 		mutex_enter(&cpu_lock);
2027 		for (i = 0; i < soft_ring_cnt; i++) {
2028 			cpuid = srs_cpu->mc_rx_fanout_cpus[i];
2029 			/* Create the protocol softrings */
2030 			mac_srs_create_proto_softrings(i, mac_rx_srs->srs_pri,
2031 			    mcip, mac_rx_srs, cpuid, rx_func, x_arg1, x_arg2,
2032 			    B_FALSE);
2033 		}
2034 		mac_srs_worker_bind(mac_rx_srs, srs_cpu->mc_rx_workerid);
2035 		mac_srs_poll_bind(mac_rx_srs, srs_cpu->mc_rx_pollid);
2036 		mac_rx_srs_retarget_intr(mac_rx_srs, srs_cpu->mc_rx_intr_cpu);
2037 		/*
2038 		 * Bind Tx srs and soft ring threads too.
2039 		 * Let's bind tx srs to the last cpu in
2040 		 * mrp list.
2041 		 */
2042 		if (mac_tx_srs == NULL) {
2043 			mutex_exit(&cpu_lock);
2044 			goto alldone;
2045 		}
2046 
2047 		BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp);
2048 		mac_tx_srs_retarget_intr(mac_tx_srs);
2049 		mutex_exit(&cpu_lock);
2050 	} else {
2051 		mutex_enter(&cpu_lock);
2052 		/*
2053 		 * For a subflow, mrp_workerid and mrp_pollid
2054 		 * is not set.
2055 		 */
2056 		mac_srs_worker_bind(mac_rx_srs, mrp->mrp_rx_workerid);
2057 		mac_srs_poll_bind(mac_rx_srs, mrp->mrp_rx_pollid);
2058 		mutex_exit(&cpu_lock);
2059 		goto no_softrings;
2060 	}
2061 
2062 alldone:
2063 	if (soft_ring_cnt > 1)
2064 		mac_rx_srs->srs_type |= SRST_FANOUT_SRC_IP;
2065 	mac_srs_update_fanout_list(mac_rx_srs);
2066 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_FALSE);
2067 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_TRUE);
2068 	return;
2069 
2070 no_softrings:
2071 	if (mac_rx_srs->srs_type & SRST_FANOUT_PROTO) {
2072 		mutex_enter(&cpu_lock);
2073 		cpuid = mac_next_bind_cpu(cpupart);
2074 		/* Create the protocol softrings */
2075 		mac_srs_create_proto_softrings(0, mac_rx_srs->srs_pri, mcip,
2076 		    mac_rx_srs, cpuid, rx_func, x_arg1, x_arg2, B_FALSE);
2077 		mutex_exit(&cpu_lock);
2078 	} else {
2079 		/*
2080 		 * This is the case when there is no fanout which is
2081 		 * true for subflows.
2082 		 */
2083 		mac_rx_srs->srs_type |= SRST_NO_SOFT_RINGS;
2084 	}
2085 	mac_srs_update_fanout_list(mac_rx_srs);
2086 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_FALSE);
2087 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_TRUE);
2088 }
2089 
2090 /*
2091  * Calls mac_srs_fanout_init() or modify() depending upon whether
2092  * the SRS is getting initialized or re-initialized.
2093  */
2094 void
2095 mac_fanout_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2096     mac_resource_props_t *mrp, mac_direct_rx_t rx_func, void *x_arg1,
2097     mac_resource_handle_t x_arg2, cpupart_t *cpupart)
2098 {
2099 	mac_soft_ring_set_t *mac_rx_srs, *mac_tx_srs;
2100 	int i, rx_srs_cnt;
2101 
2102 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2103 
2104 	/*
2105 	 * Aggr ports do not have SRSes. This function should never be
2106 	 * called on an aggr port.
2107 	 */
2108 	ASSERT3U((mcip->mci_state_flags & MCIS_IS_AGGR_PORT), ==, 0);
2109 	mac_rx_srs = flent->fe_rx_srs[0];
2110 
2111 	/*
2112 	 * Set up the fanout on the tx side only once, with the
2113 	 * first rx SRS. The CPU binding, fanout, and bandwidth
2114 	 * criteria are common to both RX and TX, so
2115 	 * initializing them along side avoids redundant code.
2116 	 */
2117 	mac_tx_srs = flent->fe_tx_srs;
2118 	rx_srs_cnt = flent->fe_rx_srs_cnt;
2119 
2120 	/* No fanout for subflows */
2121 	if (flent->fe_type & FLOW_USER) {
2122 		mac_srs_fanout_init(mcip, mrp, rx_func,
2123 		    x_arg1, x_arg2, mac_rx_srs, mac_tx_srs,
2124 		    cpupart);
2125 		return;
2126 	}
2127 
2128 	if (mrp->mrp_mask & MRP_CPUS_USERSPEC)
2129 		mac_flow_user_cpu_init(flent, mrp);
2130 	else
2131 		mac_flow_cpu_init(flent, cpupart);
2132 
2133 	mrp->mrp_rx_fanout_cnt = mac_rx_srs->srs_cpu.mc_rx_fanout_cnt;
2134 
2135 	/*
2136 	 * Set up fanout for both SW (0th SRS) and HW classified
2137 	 * SRS (the rest of Rx SRSs in flent).
2138 	 */
2139 	for (i = 0; i < rx_srs_cnt; i++) {
2140 		mac_rx_srs = flent->fe_rx_srs[i];
2141 		if (i != 0)
2142 			mac_tx_srs = NULL;
2143 		switch (mac_rx_srs->srs_fanout_state) {
2144 		case SRS_FANOUT_UNINIT:
2145 			mac_srs_fanout_init(mcip, mrp, rx_func,
2146 			    x_arg1, x_arg2, mac_rx_srs, mac_tx_srs,
2147 			    cpupart);
2148 			break;
2149 		case SRS_FANOUT_INIT:
2150 			break;
2151 		case SRS_FANOUT_REINIT:
2152 			mac_rx_srs_quiesce(mac_rx_srs, SRS_QUIESCE);
2153 			mac_srs_fanout_modify(mcip, rx_func, x_arg1,
2154 			    x_arg2, mac_rx_srs, mac_tx_srs);
2155 			mac_rx_srs_restart(mac_rx_srs);
2156 			break;
2157 		default:
2158 			VERIFY(mac_rx_srs->srs_fanout_state <=
2159 			    SRS_FANOUT_REINIT);
2160 			break;
2161 		}
2162 	}
2163 }
2164 
2165 /*
2166  * Create a mac_soft_ring_set_t (SRS). If soft_ring_fanout_type is
2167  * SRST_TX, an SRS for Tx side is created. Otherwise an SRS for Rx side
2168  * processing is created.
2169  *
2170  * Details on Rx SRS:
2171  * Create a SRS and also add the necessary soft rings for TCP and
2172  * non-TCP based on fanout type and count specified.
2173  *
2174  * mac_soft_ring_fanout, mac_srs_fanout_modify (?),
2175  * mac_soft_ring_stop_workers, mac_soft_ring_set_destroy, etc need
2176  * to be heavily modified.
2177  *
2178  * mi_soft_ring_list_size, mi_soft_ring_size, etc need to disappear.
2179  */
2180 mac_soft_ring_set_t *
2181 mac_srs_create(mac_client_impl_t *mcip, flow_entry_t *flent, uint32_t srs_type,
2182     mac_direct_rx_t rx_func, void *x_arg1, mac_resource_handle_t x_arg2,
2183     mac_ring_t *ring)
2184 {
2185 	mac_soft_ring_set_t	*mac_srs;
2186 	mac_srs_rx_t		*srs_rx;
2187 	mac_srs_tx_t		*srs_tx;
2188 	mac_bw_ctl_t		*mac_bw;
2189 	mac_resource_props_t	*mrp;
2190 	boolean_t		is_tx_srs = ((srs_type & SRST_TX) != 0);
2191 
2192 	mac_srs = kmem_cache_alloc(mac_srs_cache, KM_SLEEP);
2193 	bzero(mac_srs, sizeof (mac_soft_ring_set_t));
2194 	srs_rx = &mac_srs->srs_rx;
2195 	srs_tx = &mac_srs->srs_tx;
2196 
2197 	mutex_enter(&flent->fe_lock);
2198 
2199 	/*
2200 	 * Get the bandwidth control structure from the flent. Get
2201 	 * rid of any residual values in the control structure for
2202 	 * the tx bw struct and also for the rx, if the rx srs is
2203 	 * the 1st one being brought up (the rx bw ctl struct may
2204 	 * be shared by multiple SRSs)
2205 	 */
2206 	if (is_tx_srs) {
2207 		mac_srs->srs_bw = &flent->fe_tx_bw;
2208 		bzero(mac_srs->srs_bw, sizeof (mac_bw_ctl_t));
2209 		flent->fe_tx_srs = mac_srs;
2210 	} else {
2211 		/*
2212 		 * The bw counter (stored in the flent) is shared
2213 		 * by SRS's within an rx group.
2214 		 */
2215 		mac_srs->srs_bw = &flent->fe_rx_bw;
2216 		/* First rx SRS, clear the bw structure */
2217 		if (flent->fe_rx_srs_cnt == 0)
2218 			bzero(mac_srs->srs_bw, sizeof (mac_bw_ctl_t));
2219 
2220 		/*
2221 		 * It is better to panic here rather than just assert because
2222 		 * on a non-debug kernel we might end up courrupting memory
2223 		 * and making it difficult to debug.
2224 		 */
2225 		if (flent->fe_rx_srs_cnt >= MAX_RINGS_PER_GROUP) {
2226 			panic("Array Overrun detected due to MAC client %p "
2227 			    " having more rings than %d", (void *)mcip,
2228 			    MAX_RINGS_PER_GROUP);
2229 		}
2230 		flent->fe_rx_srs[flent->fe_rx_srs_cnt] = mac_srs;
2231 		flent->fe_rx_srs_cnt++;
2232 	}
2233 	mac_srs->srs_flent = flent;
2234 	mutex_exit(&flent->fe_lock);
2235 
2236 	mac_srs->srs_state = 0;
2237 	mac_srs->srs_type = (srs_type | SRST_NO_SOFT_RINGS);
2238 	mac_srs->srs_worker_cpuid = mac_srs->srs_worker_cpuid_save = -1;
2239 	mac_srs->srs_poll_cpuid = mac_srs->srs_poll_cpuid_save = -1;
2240 	mac_srs->srs_mcip = mcip;
2241 	mac_srs_fanout_list_alloc(mac_srs);
2242 
2243 	/*
2244 	 * For a flow we use the underlying MAC client's priority range with
2245 	 * the priority value to find an absolute priority value. For a MAC
2246 	 * client we use the MAC client's maximum priority as the value.
2247 	 */
2248 	mrp = &flent->fe_effective_props;
2249 	if ((mac_srs->srs_type & SRST_FLOW) != 0) {
2250 		mac_srs->srs_pri = FLOW_PRIORITY(mcip->mci_min_pri,
2251 		    mcip->mci_max_pri, mrp->mrp_priority);
2252 	} else {
2253 		mac_srs->srs_pri = mcip->mci_max_pri;
2254 	}
2255 	/*
2256 	 * We need to insert the SRS in the global list before
2257 	 * binding the SRS and SR threads. Otherwise there is a
2258 	 * is a small window where the cpu reconfig callbacks
2259 	 * may miss the SRS in the list walk and DR could fail
2260 	 * as there are bound threads.
2261 	 */
2262 	mac_srs_add_glist(mac_srs);
2263 
2264 	/* Initialize bw limit */
2265 	if ((mrp->mrp_mask & MRP_MAXBW) != 0) {
2266 		mac_srs->srs_drain_func = mac_rx_srs_drain_bw;
2267 
2268 		mac_bw = mac_srs->srs_bw;
2269 		mutex_enter(&mac_bw->mac_bw_lock);
2270 		mac_bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
2271 
2272 		/*
2273 		 * Give twice the queuing capability before
2274 		 * dropping packets. The unit is bytes/tick.
2275 		 */
2276 		mac_bw->mac_bw_drop_threshold = mac_bw->mac_bw_limit << 1;
2277 		mutex_exit(&mac_bw->mac_bw_lock);
2278 		mac_srs->srs_type |= SRST_BW_CONTROL;
2279 	} else {
2280 		mac_srs->srs_drain_func = mac_rx_srs_drain;
2281 	}
2282 
2283 	/*
2284 	 * We use the following policy to control Receive
2285 	 * Side Dynamic Polling:
2286 	 * 1) We switch to poll mode anytime the processing thread causes
2287 	 *    a backlog to build up in SRS and its associated Soft Rings
2288 	 *    (sr_poll_pkt_cnt > 0).
2289 	 * 2) As long as the backlog stays under the low water mark
2290 	 *    (sr_lowat), we poll the H/W for more packets.
2291 	 * 3) If the backlog (sr_poll_pkt_cnt) exceeds low water mark, we
2292 	 *    stay in poll mode but don't poll the H/W for more packets.
2293 	 * 4) Anytime in polling mode, if we poll the H/W for packets and
2294 	 *    find nothing plus we have an existing backlog
2295 	 *    (sr_poll_pkt_cnt > 0), we stay in polling mode but don't poll
2296 	 *    the H/W for packets anymore (let the polling thread go to sleep).
2297 	 * 5) Once the backlog is relieved (packets are processed) we reenable
2298 	 *    polling (by signalling the poll thread) only when the backlog
2299 	 *    dips below sr_poll_thres.
2300 	 * 6) sr_hiwat is used exclusively when we are not polling capable
2301 	 *    and is used to decide when to drop packets so the SRS queue
2302 	 *    length doesn't grow infinitely.
2303 	 */
2304 	if (!is_tx_srs) {
2305 		srs_rx->sr_hiwat = mac_soft_ring_max_q_cnt;
2306 		/* Low water mark needs to be less than high water mark */
2307 		srs_rx->sr_lowat = mac_soft_ring_min_q_cnt <=
2308 		    mac_soft_ring_max_q_cnt ? mac_soft_ring_min_q_cnt :
2309 		    (mac_soft_ring_max_q_cnt >> 2);
2310 		/* Poll threshold need to be half of low water mark or less */
2311 		srs_rx->sr_poll_thres = mac_soft_ring_poll_thres <=
2312 		    (srs_rx->sr_lowat >> 1) ? mac_soft_ring_poll_thres :
2313 		    (srs_rx->sr_lowat >> 1);
2314 		if (mac_latency_optimize)
2315 			mac_srs->srs_state |= SRS_LATENCY_OPT;
2316 		else
2317 			mac_srs->srs_state |= SRS_SOFTRING_QUEUE;
2318 	}
2319 
2320 	/*
2321 	 * Create the srs_worker with twice the stack of a normal kernel thread
2322 	 * to reduce the likelihood of stack overflows in receive-side
2323 	 * processing.  (The larger stacks are not the only precaution taken
2324 	 * against stack overflows; see the use of mac_rx_srs_stack_needed
2325 	 * in mac_sched.c).
2326 	 */
2327 	mac_srs->srs_worker = thread_create(NULL, default_stksize << 1,
2328 	    mac_srs_worker, mac_srs, 0, &p0, TS_RUN, mac_srs->srs_pri);
2329 
2330 	if (is_tx_srs) {
2331 		/* Handle everything about Tx SRS and return */
2332 		mac_srs->srs_drain_func = mac_tx_srs_drain;
2333 		srs_tx->st_max_q_cnt = mac_tx_srs_max_q_cnt;
2334 		srs_tx->st_hiwat =
2335 		    (mac_tx_srs_hiwat > mac_tx_srs_max_q_cnt) ?
2336 		    mac_tx_srs_max_q_cnt : mac_tx_srs_hiwat;
2337 		srs_tx->st_arg1 = x_arg1;
2338 		srs_tx->st_arg2 = x_arg2;
2339 		goto done;
2340 	}
2341 
2342 	if ((srs_type & SRST_FLOW) != 0 ||
2343 	    FLOW_TAB_EMPTY(mcip->mci_subflow_tab))
2344 		srs_rx->sr_lower_proc = mac_rx_srs_process;
2345 	else
2346 		srs_rx->sr_lower_proc = mac_rx_srs_subflow_process;
2347 
2348 	srs_rx->sr_func = rx_func;
2349 	srs_rx->sr_arg1 = x_arg1;
2350 	srs_rx->sr_arg2 = x_arg2;
2351 
2352 	if (ring != NULL) {
2353 		uint_t ring_info;
2354 
2355 		/* Is the mac_srs created over the RX default group? */
2356 		if (ring->mr_gh == (mac_group_handle_t)
2357 		    MAC_DEFAULT_RX_GROUP(mcip->mci_mip)) {
2358 			mac_srs->srs_type |= SRST_DEFAULT_GRP;
2359 		}
2360 		mac_srs->srs_ring = ring;
2361 		ring->mr_srs = mac_srs;
2362 		ring->mr_classify_type = MAC_HW_CLASSIFIER;
2363 		ring->mr_flag |= MR_INCIPIENT;
2364 
2365 		if (!(mcip->mci_mip->mi_state_flags & MIS_POLL_DISABLE) &&
2366 		    FLOW_TAB_EMPTY(mcip->mci_subflow_tab) && mac_poll_enable)
2367 			mac_srs->srs_state |= SRS_POLLING_CAPAB;
2368 
2369 		mac_srs->srs_poll_thr = thread_create(NULL, 0,
2370 		    mac_rx_srs_poll_ring, mac_srs, 0, &p0, TS_RUN,
2371 		    mac_srs->srs_pri);
2372 		/*
2373 		 * Some drivers require serialization and don't send
2374 		 * packet chains in interrupt context. For such
2375 		 * drivers, we should always queue in the soft ring
2376 		 * so that we get a chance to switch into polling
2377 		 * mode under backlog.
2378 		 */
2379 		ring_info = mac_hwring_getinfo((mac_ring_handle_t)ring);
2380 		if (ring_info & MAC_RING_RX_ENQUEUE)
2381 			mac_srs->srs_state |= SRS_SOFTRING_QUEUE;
2382 	}
2383 done:
2384 	mac_srs_stat_create(mac_srs);
2385 	return (mac_srs);
2386 }
2387 
2388 /*
2389  * Figure out the number of soft rings required. Its dependant on
2390  * if protocol fanout is required (for LINKs), global settings
2391  * require us to do fanout for performance (based on mac_soft_ring_enable),
2392  * or user has specifically requested fanout.
2393  */
2394 static uint32_t
2395 mac_find_fanout(flow_entry_t *flent, uint32_t link_type)
2396 {
2397 	uint32_t			fanout_type;
2398 	mac_resource_props_t		*mrp = &flent->fe_effective_props;
2399 
2400 	/* no fanout for subflows */
2401 	switch (link_type) {
2402 	case SRST_FLOW:
2403 		fanout_type = SRST_NO_SOFT_RINGS;
2404 		break;
2405 	case SRST_LINK:
2406 		fanout_type = SRST_FANOUT_PROTO;
2407 		break;
2408 	}
2409 
2410 	/* A primary NIC/link is being plumbed */
2411 	if (flent->fe_type & FLOW_PRIMARY_MAC) {
2412 		if (mac_soft_ring_enable && mac_rx_soft_ring_count > 1) {
2413 			fanout_type |= SRST_FANOUT_SRC_IP;
2414 		}
2415 	} else if (flent->fe_type & FLOW_VNIC) {
2416 		/* A VNIC is being created */
2417 		if (mrp != NULL && mrp->mrp_ncpus > 0) {
2418 			fanout_type |= SRST_FANOUT_SRC_IP;
2419 		}
2420 	}
2421 
2422 	return (fanout_type);
2423 }
2424 
2425 /*
2426  * Change a group from h/w to s/w classification.
2427  */
2428 void
2429 mac_rx_switch_grp_to_sw(mac_group_t *group)
2430 {
2431 	mac_ring_t		*ring;
2432 	mac_soft_ring_set_t	*mac_srs;
2433 
2434 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) {
2435 		if (ring->mr_classify_type == MAC_HW_CLASSIFIER) {
2436 			/*
2437 			 * Remove the SRS associated with the HW ring.
2438 			 * As a result, polling will be disabled.
2439 			 */
2440 			mac_srs = ring->mr_srs;
2441 			ASSERT(mac_srs != NULL);
2442 			mac_rx_srs_remove(mac_srs);
2443 			ring->mr_srs = NULL;
2444 		}
2445 
2446 		if (ring->mr_state != MR_INUSE)
2447 			(void) mac_start_ring(ring);
2448 
2449 		/*
2450 		 * We need to perform SW classification
2451 		 * for packets landing in these rings
2452 		 */
2453 		ring->mr_flag = 0;
2454 		ring->mr_classify_type = MAC_SW_CLASSIFIER;
2455 	}
2456 }
2457 
2458 /*
2459  * Create the Rx SRS for S/W classifier and for each ring in the
2460  * group (if exclusive group). Also create the Tx SRS.
2461  */
2462 void
2463 mac_srs_group_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2464     uint32_t link_type)
2465 {
2466 	cpupart_t		*cpupart;
2467 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
2468 	mac_resource_props_t	*emrp = MCIP_EFFECTIVE_PROPS(mcip);
2469 	boolean_t		use_default = B_FALSE;
2470 
2471 	mac_rx_srs_group_setup(mcip, flent, link_type);
2472 	mac_tx_srs_group_setup(mcip, flent, link_type);
2473 
2474 	/* Aggr ports don't have SRSes; thus there is no soft ring fanout. */
2475 	if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) != 0)
2476 		return;
2477 
2478 	pool_lock();
2479 	cpupart = mac_pset_find(mrp, &use_default);
2480 	mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip),
2481 	    mac_rx_deliver, mcip, NULL, cpupart);
2482 	mac_set_pool_effective(use_default, cpupart, mrp, emrp);
2483 	pool_unlock();
2484 }
2485 
2486 /*
2487  * Set up the Rx SRSes. If there is no group associated with the
2488  * client, then only setup SW classification. If the client has
2489  * exlusive (MAC_GROUP_STATE_RESERVED) use of the group, then create an
2490  * SRS for each HW ring. If the client is sharing a group, then make
2491  * sure to teardown the HW SRSes.
2492  */
2493 void
2494 mac_rx_srs_group_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2495     uint32_t link_type)
2496 {
2497 	mac_impl_t		*mip = mcip->mci_mip;
2498 	mac_soft_ring_set_t	*mac_srs;
2499 	mac_ring_t		*ring;
2500 	uint32_t		fanout_type;
2501 	mac_group_t		*rx_group = flent->fe_rx_ring_group;
2502 	boolean_t		no_unicast;
2503 
2504 	/*
2505 	 * If this is an an aggr port, then don't setup Rx SRS and Rx
2506 	 * soft rings as they won't be used. However, we still need to
2507 	 * start the rings to receive data on them.
2508 	 */
2509 	if (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) {
2510 		if (rx_group == NULL)
2511 			return;
2512 
2513 		for (ring = rx_group->mrg_rings; ring != NULL;
2514 		    ring = ring->mr_next) {
2515 			if (ring->mr_state != MR_INUSE)
2516 				(void) mac_start_ring(ring);
2517 		}
2518 
2519 		return;
2520 	}
2521 
2522 	/*
2523 	 * Aggr ports should never have SRSes.
2524 	 */
2525 	ASSERT3U((mcip->mci_state_flags & MCIS_IS_AGGR_PORT), ==, 0);
2526 
2527 	fanout_type = mac_find_fanout(flent, link_type);
2528 	no_unicast = (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) != 0;
2529 
2530 	/* Create the SRS for SW classification if none exists */
2531 	if (flent->fe_rx_srs[0] == NULL) {
2532 		ASSERT(flent->fe_rx_srs_cnt == 0);
2533 		mac_srs = mac_srs_create(mcip, flent, fanout_type | link_type,
2534 		    mac_rx_deliver, mcip, NULL, NULL);
2535 		mutex_enter(&flent->fe_lock);
2536 		flent->fe_cb_fn = (flow_fn_t)mac_srs->srs_rx.sr_lower_proc;
2537 		flent->fe_cb_arg1 = (void *)mip;
2538 		flent->fe_cb_arg2 = (void *)mac_srs;
2539 		mutex_exit(&flent->fe_lock);
2540 	}
2541 
2542 	if (rx_group == NULL)
2543 		return;
2544 
2545 	/*
2546 	 * If the group is marked RESERVED then setup an SRS and
2547 	 * fanout for each HW ring.
2548 	 */
2549 	switch (rx_group->mrg_state) {
2550 	case MAC_GROUP_STATE_RESERVED:
2551 		for (ring = rx_group->mrg_rings; ring != NULL;
2552 		    ring = ring->mr_next) {
2553 			uint16_t vid = i_mac_flow_vid(mcip->mci_flent);
2554 
2555 			switch (ring->mr_state) {
2556 			case MR_INUSE:
2557 			case MR_FREE:
2558 				if (ring->mr_srs != NULL)
2559 					break;
2560 				if (ring->mr_state != MR_INUSE)
2561 					(void) mac_start_ring(ring);
2562 
2563 				/*
2564 				 * If a client requires SW VLAN
2565 				 * filtering or has no unicast address
2566 				 * then we don't create any HW ring
2567 				 * SRSes.
2568 				 */
2569 				if ((!MAC_GROUP_HW_VLAN(rx_group) &&
2570 				    vid != VLAN_ID_NONE) || no_unicast)
2571 					break;
2572 
2573 				/*
2574 				 * When a client has exclusive use of
2575 				 * a group, and that group's traffic
2576 				 * is fully HW classified, we create
2577 				 * an SRS for each HW ring in order to
2578 				 * make use of dynamic polling of said
2579 				 * HW rings.
2580 				 */
2581 				mac_srs = mac_srs_create(mcip, flent,
2582 				    fanout_type | link_type,
2583 				    mac_rx_deliver, mcip, NULL, ring);
2584 				break;
2585 			default:
2586 				cmn_err(CE_PANIC,
2587 				    "srs_setup: mcip = %p "
2588 				    "trying to add UNKNOWN ring = %p\n",
2589 				    (void *)mcip, (void *)ring);
2590 				break;
2591 			}
2592 		}
2593 		break;
2594 	case MAC_GROUP_STATE_SHARED:
2595 		/*
2596 		 * When a group is shared by multiple clients, we must
2597 		 * use SW classifiction to ensure packets are
2598 		 * delivered to the correct client.
2599 		 */
2600 		mac_rx_switch_grp_to_sw(rx_group);
2601 		break;
2602 	default:
2603 		ASSERT(B_FALSE);
2604 		break;
2605 	}
2606 }
2607 
2608 /*
2609  * Set up the TX SRS.
2610  */
2611 void
2612 mac_tx_srs_group_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2613     uint32_t link_type)
2614 {
2615 	/*
2616 	 * If this is an exclusive client (e.g. an aggr port), then
2617 	 * don't setup Tx SRS and Tx soft rings as they won't be used.
2618 	 * However, we still need to start the rings to send data
2619 	 * across them.
2620 	 */
2621 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2622 		mac_ring_t		*ring;
2623 		mac_group_t		*grp;
2624 
2625 		grp = (mac_group_t *)flent->fe_tx_ring_group;
2626 
2627 		if (grp == NULL)
2628 			return;
2629 
2630 		for (ring = grp->mrg_rings; ring != NULL;
2631 		    ring = ring->mr_next) {
2632 			if (ring->mr_state != MR_INUSE)
2633 				(void) mac_start_ring(ring);
2634 		}
2635 
2636 		return;
2637 	}
2638 
2639 	/*
2640 	 * Aggr ports should never have SRSes.
2641 	 */
2642 	ASSERT3U((mcip->mci_state_flags & MCIS_IS_AGGR_PORT), ==, 0);
2643 
2644 	if (flent->fe_tx_srs == NULL) {
2645 		(void) mac_srs_create(mcip, flent, SRST_TX | link_type,
2646 		    NULL, mcip, NULL, NULL);
2647 	}
2648 
2649 	mac_tx_srs_setup(mcip, flent);
2650 }
2651 
2652 /*
2653  * Teardown all the Rx SRSes. Unless hwonly is set, then only teardown
2654  * the Rx HW SRSes and leave the SW SRS alone. The hwonly flag is set
2655  * when we wish to move a MAC client from one group to another. In
2656  * that case, we need to release the current HW SRSes but keep the SW
2657  * SRS for continued traffic classifiction.
2658  */
2659 void
2660 mac_rx_srs_group_teardown(flow_entry_t *flent, boolean_t hwonly)
2661 {
2662 	mac_soft_ring_set_t	*mac_srs;
2663 	int			i;
2664 	int			count = flent->fe_rx_srs_cnt;
2665 
2666 	for (i = 0; i < count; i++) {
2667 		if (i == 0 && hwonly)
2668 			continue;
2669 		mac_srs = flent->fe_rx_srs[i];
2670 		mac_rx_srs_quiesce(mac_srs, SRS_CONDEMNED);
2671 		mac_srs_free(mac_srs);
2672 		flent->fe_rx_srs[i] = NULL;
2673 		flent->fe_rx_srs_cnt--;
2674 	}
2675 
2676 	/*
2677 	 * If we are only tearing down the HW SRSes then there must be
2678 	 * one SRS left for SW classification. Otherwise we are tearing
2679 	 * down both HW and SW and there should be no SRSes left.
2680 	 */
2681 	if (hwonly)
2682 		VERIFY3S(flent->fe_rx_srs_cnt, ==, 1);
2683 	else
2684 		VERIFY3S(flent->fe_rx_srs_cnt, ==, 0);
2685 }
2686 
2687 /*
2688  * Remove the TX SRS.
2689  */
2690 void
2691 mac_tx_srs_group_teardown(mac_client_impl_t *mcip, flow_entry_t *flent,
2692     uint32_t link_type)
2693 {
2694 	mac_soft_ring_set_t	*tx_srs;
2695 	mac_srs_tx_t		*tx;
2696 
2697 	if ((tx_srs = flent->fe_tx_srs) == NULL)
2698 		return;
2699 
2700 	tx = &tx_srs->srs_tx;
2701 	switch (link_type) {
2702 	case SRST_FLOW:
2703 		/*
2704 		 * For flows, we need to work with passed
2705 		 * flent to find the Rx/Tx SRS.
2706 		 */
2707 		mac_tx_srs_quiesce(tx_srs, SRS_CONDEMNED);
2708 		break;
2709 	case SRST_LINK:
2710 		mac_tx_client_condemn((mac_client_handle_t)mcip);
2711 		if (tx->st_arg2 != NULL) {
2712 			ASSERT(tx_srs->srs_type & SRST_TX);
2713 			/*
2714 			 * The ring itself will be stopped when
2715 			 * we release the group or in the
2716 			 * mac_datapath_teardown (for the default
2717 			 * group)
2718 			 */
2719 			tx->st_arg2 = NULL;
2720 		}
2721 		break;
2722 	default:
2723 		ASSERT(B_FALSE);
2724 		break;
2725 	}
2726 	mac_srs_free(tx_srs);
2727 	flent->fe_tx_srs = NULL;
2728 }
2729 
2730 /*
2731  * This is the group state machine.
2732  *
2733  * The state of an Rx group is given by
2734  * the following table. The default group and its rings are started in
2735  * mac_start itself and the default group stays in SHARED state until
2736  * mac_stop at which time the group and rings are stopped and and it
2737  * reverts to the Registered state.
2738  *
2739  * Typically this function is called on a group after adding or removing a
2740  * client from it, to find out what should be the new state of the group.
2741  * If the new state is RESERVED, then the client that owns this group
2742  * exclusively is also returned. Note that adding or removing a client from
2743  * a group could also impact the default group and the caller needs to
2744  * evaluate the effect on the default group.
2745  *
2746  * Group type		# of clients	mi_nactiveclients	Group State
2747  *			in the group
2748  *
2749  * Non-default		0		N.A.			REGISTERED
2750  * Non-default		1		N.A.			RESERVED
2751  *
2752  * Default		0		N.A.			SHARED
2753  * Default		1		1			RESERVED
2754  * Default		1		> 1			SHARED
2755  * Default		> 1		N.A.			SHARED
2756  *
2757  * For a TX group, the following is the state table.
2758  *
2759  * Group type		# of clients	Group State
2760  *			in the group
2761  *
2762  * Non-default		0		REGISTERED
2763  * Non-default		1		RESERVED
2764  *
2765  * Default		0		REGISTERED
2766  * Default		1		RESERVED
2767  * Default		> 1		SHARED
2768  */
2769 mac_group_state_t
2770 mac_group_next_state(mac_group_t *grp, mac_client_impl_t **group_only_mcip,
2771     mac_group_t *defgrp, boolean_t rx_group)
2772 {
2773 	mac_impl_t		*mip = (mac_impl_t *)grp->mrg_mh;
2774 
2775 	*group_only_mcip = NULL;
2776 
2777 	/* Non-default group */
2778 
2779 	if (grp != defgrp) {
2780 		if (MAC_GROUP_NO_CLIENT(grp))
2781 			return (MAC_GROUP_STATE_REGISTERED);
2782 
2783 		*group_only_mcip = MAC_GROUP_ONLY_CLIENT(grp);
2784 		if (*group_only_mcip != NULL)
2785 			return (MAC_GROUP_STATE_RESERVED);
2786 
2787 		return (MAC_GROUP_STATE_SHARED);
2788 	}
2789 
2790 	/* Default group */
2791 
2792 	if (MAC_GROUP_NO_CLIENT(grp)) {
2793 		if (rx_group)
2794 			return (MAC_GROUP_STATE_SHARED);
2795 		else
2796 			return (MAC_GROUP_STATE_REGISTERED);
2797 	}
2798 	*group_only_mcip = MAC_GROUP_ONLY_CLIENT(grp);
2799 	if (*group_only_mcip == NULL)
2800 		return (MAC_GROUP_STATE_SHARED);
2801 
2802 	if (rx_group && mip->mi_nactiveclients != 1)
2803 		return (MAC_GROUP_STATE_SHARED);
2804 
2805 	ASSERT(*group_only_mcip != NULL);
2806 	return (MAC_GROUP_STATE_RESERVED);
2807 }
2808 
2809 /*
2810  * OVERVIEW NOTES FOR DATAPATH
2811  * ===========================
2812  *
2813  * Create an SRS and setup the corresponding flow function and args.
2814  * Add a classification rule for the flow specified by 'flent' and program
2815  * the hardware classifier when applicable.
2816  *
2817  * Rx ring assignment, SRS, polling and B/W enforcement
2818  * ----------------------------------------------------
2819  *
2820  * We try to use H/W classification on NIC and assign traffic to a
2821  * MAC address to a particular Rx ring. There is a 1-1 mapping
2822  * between a SRS and a Rx ring. The SRS (short for soft ring set)
2823  * dynamically switches the underlying Rx ring between interrupt
2824  * and polling mode and enforces any specified B/W control.
2825  *
2826  * There is always a SRS created and tied to each H/W and S/W rule.
2827  * Whenever we create a H/W rule, we always add the the same rule to
2828  * S/W classifier and tie a SRS to it.
2829  *
2830  * In case a B/W control is specified, its broken into bytes
2831  * per ticks and as soon as the quota for a tick is exhausted,
2832  * the underlying Rx ring is forced into poll mode for remianing
2833  * tick. The SRS poll thread only polls for bytes that are
2834  * allowed to come in the SRS. We typically let 4x the configured
2835  * B/W worth of packets to come in the SRS (to prevent unnecessary
2836  * drops due to bursts) but only process the specified amount.
2837  *
2838  * A Link (primary NIC, VNIC, VLAN or aggr) can have 1 or more
2839  * Rx rings (and corresponding SRSs) assigned to it. The SRS
2840  * in turn can have softrings to do protocol level fanout or
2841  * softrings to do S/W based fanout or both. In case the NIC
2842  * has no Rx rings, we do S/W classification to respective SRS.
2843  * The S/W classification rule is always setup and ready. This
2844  * allows the MAC layer to reassign Rx rings whenever needed
2845  * but packets still continue to flow via the default path and
2846  * getting S/W classified to correct SRS.
2847  *
2848  * In other cases where a NIC or VNIC is plumbed, our goal is use
2849  * H/W classifier and get two Rx ring assigned for the Link. One
2850  * for TCP and one for UDP|SCTP. The respective SRS still do the
2851  * polling on the Rx ring. For Link that is plumbed for IP, there
2852  * is a TCP squeue which also does polling and can control the
2853  * the Rx ring directly (where SRS is just pass through). For
2854  * the following cases, the SRS does the polling underneath.
2855  * 1) non IP based Links (Links which are not plumbed via ifconfig)
2856  *    and paths which have no IP squeues (UDP & SCTP)
2857  * 2) If B/W control is specified on the Link
2858  * 3) If S/W fanout is secified
2859  *
2860  * Note1: As of current implementation, we try to assign only 1 Rx
2861  * ring per Link and more than 1 Rx ring for primary Link for
2862  * H/W based fanout. We always create following softrings per SRS:
2863  * 1) TCP softring which is polled by TCP squeue where possible
2864  *    (and also bypasses DLS)
2865  * 2) UDP/SCTP based which bypasses DLS
2866  * 3) OTH softring which goes via DLS (currently deal with IPv6
2867  *    and non TCP/UDP/SCTP for IPv4 packets).
2868  *
2869  * It is necessary to create 3 softrings since SRS has to poll
2870  * the single Rx ring underneath and enforce any link level B/W
2871  * control (we can't switch the Rx ring in poll mode just based
2872  * on TCP squeue if the same Rx ring is sharing UDP and other
2873  * traffic as well). Once polling is done and any Link level B/W
2874  * control is specified, the packets are assigned to respective
2875  * softring based on protocol. Since TCP has IP based squeue
2876  * which benefits by polling, we separate TCP packets into
2877  * its own softring which can be polled by IP squeue. We need
2878  * to separate out UDP/SCTP to UDP softring since it can bypass
2879  * the DLS layer which has heavy performance advanatges and we
2880  * need a softring (OTH) for rest.
2881  *
2882  * ToDo: The 3 softrings for protocol are needed only till we can
2883  * get rid of DLS from datapath, make IPv4 and IPv6 paths
2884  * symmetric (deal with mac_header_info for v6 and polling for
2885  * IPv4 TCP - ip_accept_tcp is IPv4 specific although squeues
2886  * are generic), and bring SAP based classification to MAC layer
2887  *
2888  * H/W and S/W based fanout and multiple Rx rings per Link
2889  * -------------------------------------------------------
2890  *
2891  * In case, fanout is requested (or determined automatically based
2892  * on Link speed and processor speed), we try to assign multiple
2893  * Rx rings per Link with their respective SRS. In this case
2894  * the NIC should be capable of fanning out incoming packets between
2895  * the assigned Rx rings (H/W based fanout). All the SRS
2896  * individually switch their Rx ring between interrupt and polling
2897  * mode but share a common B/W control counter in case of Link
2898  * level B/W is specified.
2899  *
2900  * If S/W based fanout is specified in lieu of H/W based fanout,
2901  * the Link SRS creates the specified number of softrings for
2902  * each protocol (TCP, UDP, OTH). Incoming packets are fanned
2903  * out to the correct softring based on their protocol and
2904  * protocol specific hash function.
2905  *
2906  * Primary and non primary MAC clients
2907  * -----------------------------------
2908  *
2909  * The NICs, VNICs, Vlans, and Aggrs are typically termed as Links
2910  * and are a Layer 2 construct.
2911  *
2912  * Primary NIC:
2913  *	The Link that owns the primary MAC address and typically
2914  *	is used as the data NIC in non virtualized cases. As such
2915  *	H/W resources are preferntially given to primary NIC. As
2916  *	far as code is concerned, there is no difference in the
2917  *	primary NIC vs VNICs. They are all treated as Links.
2918  *	At the very first call to mac_unicast_add() we program the S/W
2919  *	classifier for the primary MAC address, get a soft ring set
2920  *	(and soft rings based on 'ip_soft_ring_cnt')
2921  *	and a Rx ring assigned for polling to get enabled.
2922  *	When IP get plumbed and negotiates polling, we can
2923  *	let squeue do the polling on TCP softring.
2924  *
2925  * VNICs:
2926  *	Same as any other Link. As long as the H/W resource assignments
2927  *	are equal, the data path and setup for all Links is same.
2928  *
2929  * Flows:
2930  *	Can be configured on Links. They have their own SRS and the
2931  *	S/W classifier is programmed appropriately based on the flow.
2932  *	The flows typically deal with layer 3 and above and
2933  *	creates a soft ring set specific to the flow. The receive
2934  *	side function is switched from mac_rx_srs_process to
2935  *	mac_rx_srs_subflow_process which first tries to assign the
2936  *	packet to appropriate flow SRS and failing which assigns it
2937  *	to link SRS. This allows us to avoid the layered approach
2938  *	which gets complex.
2939  *
2940  * By the time mac_datapath_setup() completes, we already have the
2941  * soft rings set, Rx rings, soft rings, etc figured out and both H/W
2942  * and S/W classifiers programmed. IP is not plumbed yet (and might
2943  * never be for Virtual Machines guest OS path). When IP is plumbed
2944  * (for both NIC and VNIC), we do a capability negotiation for polling
2945  * and upcall functions etc.
2946  *
2947  * Rx ring Assignement NOTES
2948  * -------------------------
2949  *
2950  * For NICs which have only 1 Rx ring (we treat  NICs with no Rx rings
2951  * as NIC with a single default ring), we assign the only ring to
2952  * primary Link. The primary Link SRS can do polling on it as long as
2953  * it is the only link in use and we compare the MAC address for unicast
2954  * packets before accepting an incoming packet (there is no need for S/W
2955  * classification in this case). We disable polling on the only ring the
2956  * moment 2nd link gets created (the polling remains enabled even though
2957  * there are broadcast and * multicast flows created).
2958  *
2959  * If the NIC has more than 1 Rx ring, we assign the default ring (the
2960  * 1st ring) to deal with broadcast, multicast and traffic for other
2961  * NICs which needs S/W classification. We assign the primary mac
2962  * addresses to another ring by specifiying a classification rule for
2963  * primary unicast MAC address to the selected ring. The primary Link
2964  * (and its SRS) can continue to poll the assigned Rx ring at all times
2965  * independantly.
2966  *
2967  * Note: In future, if no fanout is specified, we try to assign 2 Rx
2968  * rings for the primary Link with the primary MAC address + TCP going
2969  * to one ring and primary MAC address + UDP|SCTP going to other ring.
2970  * Any remaining traffic for primary MAC address can go to the default
2971  * Rx ring and get S/W classified. This way the respective SRSs don't
2972  * need to do proto fanout and don't need to have softrings at all and
2973  * can poll their respective Rx rings.
2974  *
2975  * As an optimization, when a new NIC or VNIC is created, we can get
2976  * only one Rx ring and make it a TCP specific Rx ring and use the
2977  * H/W default Rx ring for the rest (this Rx ring is never polled).
2978  *
2979  * For clients that don't have MAC address, but want to receive and
2980  * transmit packets (e.g, bpf, gvrp etc.), we need to setup the datapath.
2981  * For such clients (identified by the MCIS_NO_UNICAST_ADDR flag) we
2982  * always give the default group and use software classification (i.e.
2983  * even if this is the only client in the default group, we will
2984  * leave group as shared).
2985  */
2986 
2987 int
2988 mac_datapath_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2989     uint32_t link_type)
2990 {
2991 	mac_impl_t		*mip = mcip->mci_mip;
2992 	mac_group_t		*rgroup = NULL;
2993 	mac_group_t		*tgroup = NULL;
2994 	mac_group_t		*default_rgroup;
2995 	mac_group_t		*default_tgroup;
2996 	int			err;
2997 	uint16_t		vid;
2998 	uint8_t			*mac_addr;
2999 	mac_group_state_t	next_state;
3000 	mac_client_impl_t	*group_only_mcip;
3001 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
3002 	mac_resource_props_t	*emrp = MCIP_EFFECTIVE_PROPS(mcip);
3003 	boolean_t		rxhw;
3004 	boolean_t		txhw;
3005 	boolean_t		use_default = B_FALSE;
3006 	cpupart_t		*cpupart;
3007 	boolean_t		no_unicast;
3008 	boolean_t		isprimary = flent->fe_type & FLOW_PRIMARY_MAC;
3009 	mac_client_impl_t	*reloc_pmcip = NULL;
3010 	boolean_t		use_hw;
3011 
3012 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3013 
3014 	switch (link_type) {
3015 	case SRST_FLOW:
3016 		mac_srs_group_setup(mcip, flent, link_type);
3017 		return (0);
3018 
3019 	case SRST_LINK:
3020 		no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
3021 		mac_addr = flent->fe_flow_desc.fd_dst_mac;
3022 
3023 		/* Default RX group */
3024 		default_rgroup = MAC_DEFAULT_RX_GROUP(mip);
3025 
3026 		/* Default TX group */
3027 		default_tgroup = MAC_DEFAULT_TX_GROUP(mip);
3028 
3029 		if (no_unicast) {
3030 			rgroup = default_rgroup;
3031 			tgroup = default_tgroup;
3032 			goto grp_found;
3033 		}
3034 		rxhw = (mrp->mrp_mask & MRP_RX_RINGS) &&
3035 		    (mrp->mrp_nrxrings > 0 ||
3036 		    (mrp->mrp_mask & MRP_RXRINGS_UNSPEC));
3037 		txhw = (mrp->mrp_mask & MRP_TX_RINGS) &&
3038 		    (mrp->mrp_ntxrings > 0 ||
3039 		    (mrp->mrp_mask & MRP_TXRINGS_UNSPEC));
3040 
3041 		/*
3042 		 * All the rings initially belong to the default group
3043 		 * under dynamic grouping. The primary client uses the
3044 		 * default group when it is the only client. The
3045 		 * default group is also used as the destination for
3046 		 * all multicast and broadcast traffic of all clients.
3047 		 * Therefore, the primary client loses its ability to
3048 		 * poll the softrings on addition of a second client.
3049 		 * To avoid a performance penalty, MAC will move the
3050 		 * primary client to a dedicated group when it can.
3051 		 *
3052 		 * When using static grouping, the primary client
3053 		 * begins life on a non-default group. There is
3054 		 * no moving needed upon addition of a second client.
3055 		 */
3056 		if (!isprimary && mip->mi_nactiveclients == 2 &&
3057 		    (group_only_mcip = mac_primary_client_handle(mip)) !=
3058 		    NULL && mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
3059 			reloc_pmcip = mac_check_primary_relocation(
3060 			    group_only_mcip, rxhw);
3061 		}
3062 
3063 		/*
3064 		 * Check to see if we can get an exclusive group for
3065 		 * this mac address or if there already exists a
3066 		 * group that has this mac address (case of VLANs).
3067 		 * If no groups are available, use the default group.
3068 		 */
3069 		rgroup = mac_reserve_rx_group(mcip, mac_addr, B_FALSE);
3070 		if (rgroup == NULL && rxhw) {
3071 			err = ENOSPC;
3072 			goto setup_failed;
3073 		} else if (rgroup == NULL) {
3074 			rgroup = default_rgroup;
3075 		}
3076 
3077 		/*
3078 		 * If we are adding a second client to a
3079 		 * non-default group then we need to move the
3080 		 * existing client to the default group and
3081 		 * add the new client to the default group as
3082 		 * well.
3083 		 */
3084 		if (rgroup != default_rgroup &&
3085 		    rgroup->mrg_state == MAC_GROUP_STATE_RESERVED) {
3086 			group_only_mcip = MAC_GROUP_ONLY_CLIENT(rgroup);
3087 			err = mac_rx_switch_group(group_only_mcip, rgroup,
3088 			    default_rgroup);
3089 
3090 			if (err != 0)
3091 				goto setup_failed;
3092 
3093 			rgroup = default_rgroup;
3094 		}
3095 
3096 		/*
3097 		 * Check to see if we can get an exclusive group for
3098 		 * this mac client. If no groups are available, use
3099 		 * the default group.
3100 		 */
3101 		tgroup = mac_reserve_tx_group(mcip, B_FALSE);
3102 		if (tgroup == NULL && txhw) {
3103 			if (rgroup != NULL && rgroup != default_rgroup)
3104 				mac_release_rx_group(mcip, rgroup);
3105 			err = ENOSPC;
3106 			goto setup_failed;
3107 		} else if (tgroup == NULL) {
3108 			tgroup = default_tgroup;
3109 		}
3110 
3111 		/*
3112 		 * Some NICs don't support any Rx rings, so there may not
3113 		 * even be a default group.
3114 		 */
3115 	grp_found:
3116 		if (rgroup != NULL) {
3117 			if (rgroup != default_rgroup &&
3118 			    MAC_GROUP_NO_CLIENT(rgroup) &&
3119 			    (rxhw || mcip->mci_share != 0)) {
3120 				MAC_RX_GRP_RESERVED(mip);
3121 				if (mip->mi_rx_group_type ==
3122 				    MAC_GROUP_TYPE_DYNAMIC) {
3123 					MAC_RX_RING_RESERVED(mip,
3124 					    rgroup->mrg_cur_count);
3125 				}
3126 			}
3127 
3128 			flent->fe_rx_ring_group = rgroup;
3129 			/*
3130 			 * Add the client to the group and update the
3131 			 * group's state. If rgroup != default_group
3132 			 * then the rgroup should only ever have one
3133 			 * client and be in the RESERVED state. But no
3134 			 * matter what, the default_rgroup will enter
3135 			 * the SHARED state since it has to receive
3136 			 * all broadcast and multicast traffic. This
3137 			 * case is handled later in the function.
3138 			 */
3139 			mac_group_add_client(rgroup, mcip);
3140 			next_state = mac_group_next_state(rgroup,
3141 			    &group_only_mcip, default_rgroup, B_TRUE);
3142 			mac_set_group_state(rgroup, next_state);
3143 		}
3144 
3145 		if (tgroup != NULL) {
3146 			if (tgroup != default_tgroup &&
3147 			    MAC_GROUP_NO_CLIENT(tgroup) &&
3148 			    (txhw || mcip->mci_share != 0)) {
3149 				MAC_TX_GRP_RESERVED(mip);
3150 				if (mip->mi_tx_group_type ==
3151 				    MAC_GROUP_TYPE_DYNAMIC) {
3152 					MAC_TX_RING_RESERVED(mip,
3153 					    tgroup->mrg_cur_count);
3154 				}
3155 			}
3156 			flent->fe_tx_ring_group = tgroup;
3157 			mac_group_add_client(tgroup, mcip);
3158 			next_state = mac_group_next_state(tgroup,
3159 			    &group_only_mcip, default_tgroup, B_FALSE);
3160 			tgroup->mrg_state = next_state;
3161 		}
3162 
3163 		/* We are setting up minimal datapath only */
3164 		if (no_unicast) {
3165 			mac_srs_group_setup(mcip, flent, link_type);
3166 			break;
3167 		}
3168 
3169 		/* Program software classification. */
3170 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0)
3171 			goto setup_failed;
3172 
3173 		/* Program hardware classification. */
3174 		vid = i_mac_flow_vid(flent);
3175 		use_hw = (mcip->mci_state_flags & MCIS_UNICAST_HW) != 0;
3176 		err = mac_add_macaddr_vlan(mip, rgroup, mac_addr, vid, use_hw);
3177 
3178 		if (err != 0)
3179 			goto setup_failed;
3180 
3181 		mcip->mci_unicast = mac_find_macaddr(mip, mac_addr);
3182 		VERIFY3P(mcip->mci_unicast, !=, NULL);
3183 
3184 		/*
3185 		 * Setup the Rx and Tx SRSes. If the client has a
3186 		 * reserved group, then mac_srs_group_setup() creates
3187 		 * the required SRSes for the HW rings. If we have a
3188 		 * shared group, mac_srs_group_setup() dismantles the
3189 		 * HW SRSes of the previously exclusive group.
3190 		 */
3191 		mac_srs_group_setup(mcip, flent, link_type);
3192 
3193 		/* (Re)init the v6 token & local addr used by link protection */
3194 		mac_protect_update_mac_token(mcip);
3195 		break;
3196 
3197 	default:
3198 		ASSERT(B_FALSE);
3199 		break;
3200 	}
3201 
3202 	/*
3203 	 * All broadcast and multicast traffic is received only on the default
3204 	 * group. If we have setup the datapath for a non-default group above
3205 	 * then move the default group to shared state to allow distribution of
3206 	 * incoming broadcast traffic to the other groups and dismantle the
3207 	 * SRSes over the default group.
3208 	 */
3209 	if (rgroup != NULL) {
3210 		if (rgroup != default_rgroup) {
3211 			if (default_rgroup->mrg_state ==
3212 			    MAC_GROUP_STATE_RESERVED) {
3213 				group_only_mcip = MAC_GROUP_ONLY_CLIENT(
3214 				    default_rgroup);
3215 				ASSERT(group_only_mcip != NULL &&
3216 				    mip->mi_nactiveclients > 1);
3217 
3218 				mac_set_group_state(default_rgroup,
3219 				    MAC_GROUP_STATE_SHARED);
3220 				mac_rx_srs_group_setup(group_only_mcip,
3221 				    group_only_mcip->mci_flent, SRST_LINK);
3222 				pool_lock();
3223 				cpupart = mac_pset_find(mrp, &use_default);
3224 				mac_fanout_setup(group_only_mcip,
3225 				    group_only_mcip->mci_flent,
3226 				    MCIP_RESOURCE_PROPS(group_only_mcip),
3227 				    mac_rx_deliver, group_only_mcip, NULL,
3228 				    cpupart);
3229 				mac_set_pool_effective(use_default, cpupart,
3230 				    mrp, emrp);
3231 				pool_unlock();
3232 			}
3233 			ASSERT(default_rgroup->mrg_state ==
3234 			    MAC_GROUP_STATE_SHARED);
3235 		}
3236 
3237 		/*
3238 		 * A VLAN MAC client on a reserved group still
3239 		 * requires SW classification if the MAC doesn't
3240 		 * provide VLAN HW filtering.
3241 		 *
3242 		 * Clients with no unicast address also require SW
3243 		 * classification.
3244 		 */
3245 		if (rgroup->mrg_state == MAC_GROUP_STATE_RESERVED &&
3246 		    ((!MAC_GROUP_HW_VLAN(rgroup) && vid != VLAN_ID_NONE) ||
3247 		    no_unicast)) {
3248 			mac_rx_switch_grp_to_sw(rgroup);
3249 		}
3250 
3251 	}
3252 
3253 	mac_set_rings_effective(mcip);
3254 	return (0);
3255 
3256 setup_failed:
3257 	/* Switch the primary back to default group */
3258 	if (reloc_pmcip != NULL) {
3259 		(void) mac_rx_switch_group(reloc_pmcip,
3260 		    reloc_pmcip->mci_flent->fe_rx_ring_group, default_rgroup);
3261 	}
3262 	mac_datapath_teardown(mcip, flent, link_type);
3263 	return (err);
3264 }
3265 
3266 void
3267 mac_datapath_teardown(mac_client_impl_t *mcip, flow_entry_t *flent,
3268     uint32_t link_type)
3269 {
3270 	mac_impl_t		*mip = mcip->mci_mip;
3271 	mac_group_t		*group = NULL;
3272 	mac_client_impl_t	*grp_only_mcip;
3273 	flow_entry_t		*group_only_flent;
3274 	mac_group_t		*default_group;
3275 	boolean_t		check_default_group = B_FALSE;
3276 	mac_group_state_t	next_state;
3277 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
3278 	uint16_t		vid;
3279 
3280 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3281 
3282 	switch (link_type) {
3283 	case SRST_FLOW:
3284 		mac_rx_srs_group_teardown(flent, B_FALSE);
3285 		mac_tx_srs_group_teardown(mcip, flent, SRST_FLOW);
3286 		return;
3287 
3288 	case SRST_LINK:
3289 		/* Stop sending packets */
3290 		mac_tx_client_block(mcip);
3291 		group = flent->fe_rx_ring_group;
3292 		vid = i_mac_flow_vid(flent);
3293 
3294 		/*
3295 		 * Stop the packet flow from the hardware by disabling
3296 		 * any hardware filters assigned to this client.
3297 		 */
3298 		if (mcip->mci_unicast != NULL) {
3299 			int err;
3300 
3301 			err = mac_remove_macaddr_vlan(mcip->mci_unicast, vid);
3302 
3303 			if (err != 0) {
3304 				cmn_err(CE_WARN, "%s: failed to remove a MAC HW"
3305 				    " filters because of error 0x%x",
3306 				    mip->mi_name, err);
3307 			}
3308 
3309 			mcip->mci_unicast = NULL;
3310 		}
3311 
3312 		/* Stop the packets coming from the S/W classifier */
3313 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
3314 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
3315 
3316 		/* Quiesce and destroy all the SRSes. */
3317 		mac_rx_srs_group_teardown(flent, B_FALSE);
3318 		mac_tx_srs_group_teardown(mcip, flent, SRST_LINK);
3319 
3320 		ASSERT3P(mcip->mci_flent, ==, flent);
3321 		ASSERT3P(flent->fe_next, ==, NULL);
3322 
3323 		/*
3324 		 * Release our hold on the group as well. We need
3325 		 * to check if the shared group has only one client
3326 		 * left who can use it exclusively. Also, if we
3327 		 * were the last client, release the group.
3328 		 */
3329 		default_group = MAC_DEFAULT_RX_GROUP(mip);
3330 		if (group != NULL) {
3331 			mac_group_remove_client(group, mcip);
3332 			next_state = mac_group_next_state(group,
3333 			    &grp_only_mcip, default_group, B_TRUE);
3334 
3335 			if (next_state == MAC_GROUP_STATE_RESERVED) {
3336 				/*
3337 				 * Only one client left on this RX group.
3338 				 */
3339 				VERIFY3P(grp_only_mcip, !=, NULL);
3340 				mac_set_group_state(group,
3341 				    MAC_GROUP_STATE_RESERVED);
3342 				group_only_flent = grp_only_mcip->mci_flent;
3343 
3344 				/*
3345 				 * The only remaining client has exclusive
3346 				 * access on the group. Allow it to
3347 				 * dynamically poll the H/W rings etc.
3348 				 */
3349 				mac_rx_srs_group_setup(grp_only_mcip,
3350 				    group_only_flent, SRST_LINK);
3351 				mac_fanout_setup(grp_only_mcip,
3352 				    group_only_flent,
3353 				    MCIP_RESOURCE_PROPS(grp_only_mcip),
3354 				    mac_rx_deliver, grp_only_mcip, NULL, NULL);
3355 				mac_rx_group_unmark(group, MR_INCIPIENT);
3356 				mac_set_rings_effective(grp_only_mcip);
3357 			} else if (next_state == MAC_GROUP_STATE_REGISTERED) {
3358 				/*
3359 				 * This is a non-default group being freed up.
3360 				 * We need to reevaluate the default group
3361 				 * to see if the primary client can get
3362 				 * exclusive access to the default group.
3363 				 */
3364 				VERIFY3P(group, !=, MAC_DEFAULT_RX_GROUP(mip));
3365 				if (mrp->mrp_mask & MRP_RX_RINGS) {
3366 					MAC_RX_GRP_RELEASED(mip);
3367 					if (mip->mi_rx_group_type ==
3368 					    MAC_GROUP_TYPE_DYNAMIC) {
3369 						MAC_RX_RING_RELEASED(mip,
3370 						    group->mrg_cur_count);
3371 					}
3372 				}
3373 				mac_release_rx_group(mcip, group);
3374 				mac_set_group_state(group,
3375 				    MAC_GROUP_STATE_REGISTERED);
3376 				check_default_group = B_TRUE;
3377 			} else {
3378 				VERIFY3S(next_state, ==,
3379 				    MAC_GROUP_STATE_SHARED);
3380 				mac_set_group_state(group,
3381 				    MAC_GROUP_STATE_SHARED);
3382 				mac_rx_group_unmark(group, MR_CONDEMNED);
3383 			}
3384 			flent->fe_rx_ring_group = NULL;
3385 		}
3386 		/*
3387 		 * Remove the client from the TX group. Additionally, if
3388 		 * this a non-default group, then we also need to release
3389 		 * the group.
3390 		 */
3391 		group = flent->fe_tx_ring_group;
3392 		default_group = MAC_DEFAULT_TX_GROUP(mip);
3393 		if (group != NULL) {
3394 			mac_group_remove_client(group, mcip);
3395 			next_state = mac_group_next_state(group,
3396 			    &grp_only_mcip, default_group, B_FALSE);
3397 			if (next_state == MAC_GROUP_STATE_REGISTERED) {
3398 				if (group != default_group) {
3399 					if (mrp->mrp_mask & MRP_TX_RINGS) {
3400 						MAC_TX_GRP_RELEASED(mip);
3401 						if (mip->mi_tx_group_type ==
3402 						    MAC_GROUP_TYPE_DYNAMIC) {
3403 							MAC_TX_RING_RELEASED(
3404 							    mip, group->
3405 							    mrg_cur_count);
3406 						}
3407 					}
3408 					mac_release_tx_group(mcip, group);
3409 					/*
3410 					 * If the default group is reserved,
3411 					 * then we need to set the effective
3412 					 * rings as we would have given
3413 					 * back some rings when the group
3414 					 * was released
3415 					 */
3416 					if (mip->mi_tx_group_type ==
3417 					    MAC_GROUP_TYPE_DYNAMIC &&
3418 					    default_group->mrg_state ==
3419 					    MAC_GROUP_STATE_RESERVED) {
3420 						grp_only_mcip =
3421 						    MAC_GROUP_ONLY_CLIENT
3422 						    (default_group);
3423 						mac_set_rings_effective(
3424 						    grp_only_mcip);
3425 					}
3426 				} else {
3427 					mac_ring_t	*ring;
3428 					int		cnt;
3429 					int		ringcnt;
3430 
3431 					/*
3432 					 * Stop all the rings except the
3433 					 * default ring.
3434 					 */
3435 					ringcnt = group->mrg_cur_count;
3436 					ring = group->mrg_rings;
3437 					for (cnt = 0; cnt < ringcnt; cnt++) {
3438 						if (ring->mr_state ==
3439 						    MR_INUSE && ring !=
3440 						    (mac_ring_t *)
3441 						    mip->mi_default_tx_ring) {
3442 							mac_stop_ring(ring);
3443 							ring->mr_flag = 0;
3444 						}
3445 						ring = ring->mr_next;
3446 					}
3447 				}
3448 			} else if (next_state == MAC_GROUP_STATE_RESERVED) {
3449 				mac_set_rings_effective(grp_only_mcip);
3450 			}
3451 			flent->fe_tx_ring_group = NULL;
3452 			group->mrg_state = next_state;
3453 		}
3454 		break;
3455 	default:
3456 		ASSERT(B_FALSE);
3457 		break;
3458 	}
3459 
3460 	/*
3461 	 * The mac client using the default group gets exclusive access to the
3462 	 * default group if and only if it is the sole client on the entire
3463 	 * mip. If so set the group state to reserved, and set up the SRSes
3464 	 * over the default group.
3465 	 */
3466 	if (check_default_group) {
3467 		default_group = MAC_DEFAULT_RX_GROUP(mip);
3468 		VERIFY3S(default_group->mrg_state, ==, MAC_GROUP_STATE_SHARED);
3469 		next_state = mac_group_next_state(default_group,
3470 		    &grp_only_mcip, default_group, B_TRUE);
3471 		if (next_state == MAC_GROUP_STATE_RESERVED) {
3472 			VERIFY3P(grp_only_mcip, !=, NULL);
3473 			VERIFY3U(mip->mi_nactiveclients, ==, 1);
3474 			mac_set_group_state(default_group,
3475 			    MAC_GROUP_STATE_RESERVED);
3476 			mac_rx_srs_group_setup(grp_only_mcip,
3477 			    grp_only_mcip->mci_flent, SRST_LINK);
3478 			mac_fanout_setup(grp_only_mcip,
3479 			    grp_only_mcip->mci_flent,
3480 			    MCIP_RESOURCE_PROPS(grp_only_mcip), mac_rx_deliver,
3481 			    grp_only_mcip, NULL, NULL);
3482 			mac_rx_group_unmark(default_group, MR_INCIPIENT);
3483 			mac_set_rings_effective(grp_only_mcip);
3484 		}
3485 	}
3486 
3487 	/*
3488 	 * If the primary is the only one left and the MAC supports
3489 	 * dynamic grouping, we need to see if the primary needs to
3490 	 * be moved to the default group so that it can use all the
3491 	 * H/W rings.
3492 	 */
3493 	if (!(flent->fe_type & FLOW_PRIMARY_MAC) &&
3494 	    mip->mi_nactiveclients == 1 &&
3495 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
3496 		default_group = MAC_DEFAULT_RX_GROUP(mip);
3497 		grp_only_mcip = mac_primary_client_handle(mip);
3498 		if (grp_only_mcip == NULL)
3499 			return;
3500 		group_only_flent = grp_only_mcip->mci_flent;
3501 		mrp = MCIP_RESOURCE_PROPS(grp_only_mcip);
3502 		/*
3503 		 * If the primary has an explicit property set, leave it
3504 		 * alone.
3505 		 */
3506 		if (mrp->mrp_mask & MRP_RX_RINGS)
3507 			return;
3508 		/*
3509 		 * Switch the primary to the default group.
3510 		 */
3511 		(void) mac_rx_switch_group(grp_only_mcip,
3512 		    group_only_flent->fe_rx_ring_group, default_group);
3513 	}
3514 }
3515 
3516 /* DATAPATH TEAR DOWN ROUTINES (SRS and FANOUT teardown) */
3517 
3518 static void
3519 mac_srs_fanout_list_free(mac_soft_ring_set_t *mac_srs)
3520 {
3521 	if (mac_srs->srs_type & SRST_TX) {
3522 		mac_srs_tx_t *tx;
3523 
3524 		ASSERT(mac_srs->srs_tcp_soft_rings == NULL);
3525 		ASSERT(mac_srs->srs_udp_soft_rings == NULL);
3526 		ASSERT(mac_srs->srs_tcp6_soft_rings == NULL);
3527 		ASSERT(mac_srs->srs_udp6_soft_rings == NULL);
3528 		ASSERT(mac_srs->srs_oth_soft_rings == NULL);
3529 		ASSERT(mac_srs->srs_tx_soft_rings != NULL);
3530 		kmem_free(mac_srs->srs_tx_soft_rings,
3531 		    sizeof (mac_soft_ring_t *) * MAX_RINGS_PER_GROUP);
3532 		mac_srs->srs_tx_soft_rings = NULL;
3533 		tx = &mac_srs->srs_tx;
3534 		if (tx->st_soft_rings != NULL) {
3535 			kmem_free(tx->st_soft_rings,
3536 			    sizeof (mac_soft_ring_t *) * MAX_RINGS_PER_GROUP);
3537 		}
3538 	} else {
3539 		ASSERT(mac_srs->srs_tx_soft_rings == NULL);
3540 
3541 		ASSERT(mac_srs->srs_tcp_soft_rings != NULL);
3542 		kmem_free(mac_srs->srs_tcp_soft_rings,
3543 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3544 		mac_srs->srs_tcp_soft_rings = NULL;
3545 
3546 		ASSERT(mac_srs->srs_udp_soft_rings != NULL);
3547 		kmem_free(mac_srs->srs_udp_soft_rings,
3548 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3549 		mac_srs->srs_udp_soft_rings = NULL;
3550 
3551 		ASSERT(mac_srs->srs_tcp6_soft_rings != NULL);
3552 		kmem_free(mac_srs->srs_tcp6_soft_rings,
3553 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3554 		mac_srs->srs_tcp6_soft_rings = NULL;
3555 
3556 		ASSERT(mac_srs->srs_udp6_soft_rings != NULL);
3557 		kmem_free(mac_srs->srs_udp6_soft_rings,
3558 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3559 		mac_srs->srs_udp6_soft_rings = NULL;
3560 
3561 		ASSERT(mac_srs->srs_oth_soft_rings != NULL);
3562 		kmem_free(mac_srs->srs_oth_soft_rings,
3563 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3564 		mac_srs->srs_oth_soft_rings = NULL;
3565 	}
3566 }
3567 
3568 /*
3569  * An RX SRS is attached to at most one mac_ring.
3570  * A TX SRS  has no  rings.
3571  */
3572 static void
3573 mac_srs_ring_free(mac_soft_ring_set_t *mac_srs)
3574 {
3575 	mac_client_impl_t	*mcip;
3576 	mac_ring_t		*ring;
3577 	flow_entry_t		*flent;
3578 
3579 	ring = mac_srs->srs_ring;
3580 	if (mac_srs->srs_type & SRST_TX) {
3581 		ASSERT(ring == NULL);
3582 		return;
3583 	}
3584 
3585 	if (ring == NULL)
3586 		return;
3587 
3588 	/*
3589 	 * Broadcast flows don't have a client impl association, but they
3590 	 * use only soft rings.
3591 	 */
3592 	flent = mac_srs->srs_flent;
3593 	mcip = flent->fe_mcip;
3594 	ASSERT(mcip != NULL);
3595 
3596 	ring->mr_classify_type = MAC_NO_CLASSIFIER;
3597 	ring->mr_srs = NULL;
3598 }
3599 
3600 /*
3601  * Physical unlink and free of the data structures happen below. This is
3602  * driven from mac_flow_destroy(), on the last refrele of a flow.
3603  *
3604  * Assumes Rx srs is 1-1 mapped with an ring.
3605  */
3606 void
3607 mac_srs_free(mac_soft_ring_set_t *mac_srs)
3608 {
3609 	ASSERT(mac_srs->srs_mcip == NULL ||
3610 	    MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
3611 	ASSERT((mac_srs->srs_state & (SRS_CONDEMNED | SRS_CONDEMNED_DONE |
3612 	    SRS_PROC | SRS_PROC_FAST)) == (SRS_CONDEMNED | SRS_CONDEMNED_DONE));
3613 
3614 	mac_drop_chain(mac_srs->srs_first, "SRS free");
3615 	mac_srs_ring_free(mac_srs);
3616 	mac_srs_soft_rings_free(mac_srs);
3617 	mac_srs_fanout_list_free(mac_srs);
3618 
3619 	mac_srs->srs_bw = NULL;
3620 	mac_srs_stat_delete(mac_srs);
3621 	kmem_cache_free(mac_srs_cache, mac_srs);
3622 }
3623 
3624 static void
3625 mac_srs_soft_rings_quiesce(mac_soft_ring_set_t *mac_srs, uint_t s_ring_flag)
3626 {
3627 	mac_soft_ring_t	*softring;
3628 
3629 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
3630 
3631 	mac_srs_soft_rings_signal(mac_srs, s_ring_flag);
3632 	if (s_ring_flag == S_RING_CONDEMNED) {
3633 		while (mac_srs->srs_soft_ring_condemned_count !=
3634 		    mac_srs->srs_soft_ring_count)
3635 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3636 	} else {
3637 		while (mac_srs->srs_soft_ring_quiesced_count !=
3638 		    mac_srs->srs_soft_ring_count)
3639 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3640 	}
3641 	mutex_exit(&mac_srs->srs_lock);
3642 
3643 	for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
3644 	    softring = softring->s_ring_next) {
3645 		(void) untimeout(softring->s_ring_tid);
3646 		softring->s_ring_tid = NULL;
3647 	}
3648 
3649 	(void) untimeout(mac_srs->srs_tid);
3650 	mac_srs->srs_tid = NULL;
3651 
3652 	mutex_enter(&mac_srs->srs_lock);
3653 }
3654 
3655 /*
3656  * The block comment above mac_rx_classify_flow_state_change explains the
3657  * background. At this point upcalls from the driver (both hardware classified
3658  * and software classified) have been cut off. We now need to quiesce the
3659  * SRS worker, poll, and softring threads. The SRS worker thread serves as
3660  * the master controller. The steps involved are described below in the function
3661  */
3662 void
3663 mac_srs_worker_quiesce(mac_soft_ring_set_t *mac_srs)
3664 {
3665 	uint_t			s_ring_flag;
3666 	uint_t			srs_poll_wait_flag;
3667 
3668 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
3669 	ASSERT(mac_srs->srs_state & (SRS_CONDEMNED | SRS_QUIESCE));
3670 
3671 	if (mac_srs->srs_state & SRS_CONDEMNED) {
3672 		s_ring_flag = S_RING_CONDEMNED;
3673 		srs_poll_wait_flag = SRS_POLL_THR_EXITED;
3674 	} else {
3675 		s_ring_flag = S_RING_QUIESCE;
3676 		srs_poll_wait_flag = SRS_POLL_THR_QUIESCED;
3677 	}
3678 
3679 	/*
3680 	 * In the case of Rx SRS wait till the poll thread is done.
3681 	 */
3682 	if ((mac_srs->srs_type & SRST_TX) == 0 &&
3683 	    mac_srs->srs_poll_thr != NULL) {
3684 		while (!(mac_srs->srs_state & srs_poll_wait_flag))
3685 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3686 
3687 		/*
3688 		 * Turn off polling as part of the quiesce operation.
3689 		 */
3690 		MAC_SRS_POLLING_OFF(mac_srs);
3691 		mac_srs->srs_state &= ~(SRS_POLLING | SRS_GET_PKTS);
3692 	}
3693 
3694 	/*
3695 	 * Then signal the soft ring worker threads to quiesce or quit
3696 	 * as needed and then wait till that happens.
3697 	 */
3698 	mac_srs_soft_rings_quiesce(mac_srs, s_ring_flag);
3699 
3700 	if (mac_srs->srs_state & SRS_CONDEMNED)
3701 		mac_srs->srs_state |= (SRS_QUIESCE_DONE | SRS_CONDEMNED_DONE);
3702 	else
3703 		mac_srs->srs_state |= SRS_QUIESCE_DONE;
3704 	cv_signal(&mac_srs->srs_quiesce_done_cv);
3705 }
3706 
3707 /*
3708  * Signal an SRS to start a temporary quiesce, or permanent removal, or restart
3709  * a quiesced SRS by setting the appropriate flags and signaling the SRS worker
3710  * or poll thread. This function is internal to the quiescing logic and is
3711  * called internally from the SRS quiesce or flow quiesce or client quiesce
3712  * higher level functions.
3713  */
3714 void
3715 mac_srs_signal(mac_soft_ring_set_t *mac_srs, uint_t srs_flag)
3716 {
3717 	mac_ring_t	*ring;
3718 
3719 	ring = mac_srs->srs_ring;
3720 	ASSERT(ring == NULL || ring->mr_refcnt == 0);
3721 
3722 	if (srs_flag == SRS_CONDEMNED) {
3723 		/*
3724 		 * The SRS is going away. We need to unbind the SRS and SR
3725 		 * threads before removing from the global SRS list. Otherwise
3726 		 * there is a small window where the cpu reconfig callbacks
3727 		 * may miss the SRS in the list walk and DR could fail since
3728 		 * there are still bound threads.
3729 		 */
3730 		mac_srs_threads_unbind(mac_srs);
3731 		mac_srs_remove_glist(mac_srs);
3732 	}
3733 	/*
3734 	 * Wakeup the SRS worker and poll threads.
3735 	 */
3736 	mutex_enter(&mac_srs->srs_lock);
3737 	mac_srs->srs_state |= srs_flag;
3738 	cv_signal(&mac_srs->srs_async);
3739 	cv_signal(&mac_srs->srs_cv);
3740 	mutex_exit(&mac_srs->srs_lock);
3741 }
3742 
3743 /*
3744  * In the Rx side, the quiescing is done bottom up. After the Rx upcalls
3745  * from the driver are done, then the Rx SRS is quiesced and only then can
3746  * we signal the soft rings. Thus this function can't be called arbitrarily
3747  * without satisfying the prerequisites. On the Tx side, the threads from
3748  * top need to quiesced, then the Tx SRS and only then can we signal the
3749  * Tx soft rings.
3750  */
3751 static void
3752 mac_srs_soft_rings_signal(mac_soft_ring_set_t *mac_srs, uint_t sr_flag)
3753 {
3754 	mac_soft_ring_t		*softring;
3755 
3756 	for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
3757 	    softring = softring->s_ring_next)
3758 		mac_soft_ring_signal(softring, sr_flag);
3759 }
3760 
3761 /*
3762  * The block comment above mac_rx_classify_flow_state_change explains the
3763  * background. At this point the SRS is quiesced and we need to restart the
3764  * SRS worker, poll, and softring threads. The SRS worker thread serves as
3765  * the master controller. The steps involved are described below in the function
3766  */
3767 void
3768 mac_srs_worker_restart(mac_soft_ring_set_t *mac_srs)
3769 {
3770 	boolean_t	iam_rx_srs;
3771 	mac_soft_ring_t	*softring;
3772 
3773 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
3774 	if ((mac_srs->srs_type & SRST_TX) != 0) {
3775 		iam_rx_srs = B_FALSE;
3776 		ASSERT((mac_srs->srs_state &
3777 		    (SRS_POLL_THR_QUIESCED | SRS_QUIESCE_DONE | SRS_QUIESCE)) ==
3778 		    (SRS_QUIESCE_DONE | SRS_QUIESCE));
3779 	} else {
3780 		iam_rx_srs = B_TRUE;
3781 		ASSERT((mac_srs->srs_state &
3782 		    (SRS_QUIESCE_DONE | SRS_QUIESCE)) ==
3783 		    (SRS_QUIESCE_DONE | SRS_QUIESCE));
3784 		if (mac_srs->srs_poll_thr != NULL) {
3785 			ASSERT((mac_srs->srs_state & SRS_POLL_THR_QUIESCED) ==
3786 			    SRS_POLL_THR_QUIESCED);
3787 		}
3788 	}
3789 
3790 	/*
3791 	 * Signal any quiesced soft ring workers to restart and wait for the
3792 	 * soft ring down count to come down to zero.
3793 	 */
3794 	if (mac_srs->srs_soft_ring_quiesced_count != 0) {
3795 		for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
3796 		    softring = softring->s_ring_next) {
3797 			if (!(softring->s_ring_state & S_RING_QUIESCE))
3798 				continue;
3799 			mac_soft_ring_signal(softring, S_RING_RESTART);
3800 		}
3801 		while (mac_srs->srs_soft_ring_quiesced_count != 0)
3802 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3803 	}
3804 
3805 	mac_srs->srs_state &= ~(SRS_QUIESCE_DONE | SRS_QUIESCE | SRS_RESTART);
3806 	if (iam_rx_srs && mac_srs->srs_poll_thr != NULL) {
3807 		/*
3808 		 * Signal the poll thread and ask it to restart. Wait till it
3809 		 * actually restarts and the SRS_POLL_THR_QUIESCED flag gets
3810 		 * cleared.
3811 		 */
3812 		mac_srs->srs_state |= SRS_POLL_THR_RESTART;
3813 		cv_signal(&mac_srs->srs_cv);
3814 		while (mac_srs->srs_state & SRS_POLL_THR_QUIESCED)
3815 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3816 		ASSERT(!(mac_srs->srs_state & SRS_POLL_THR_RESTART));
3817 	}
3818 	/* Wake up any waiter waiting for the restart to complete */
3819 	mac_srs->srs_state |= SRS_RESTART_DONE;
3820 	cv_signal(&mac_srs->srs_quiesce_done_cv);
3821 }
3822 
3823 static void
3824 mac_srs_worker_unbind(mac_soft_ring_set_t *mac_srs)
3825 {
3826 	mutex_enter(&mac_srs->srs_lock);
3827 	if (!(mac_srs->srs_state & SRS_WORKER_BOUND)) {
3828 		ASSERT(mac_srs->srs_worker_cpuid == -1);
3829 		mutex_exit(&mac_srs->srs_lock);
3830 		return;
3831 	}
3832 
3833 	mac_srs->srs_worker_cpuid = -1;
3834 	mac_srs->srs_state &= ~SRS_WORKER_BOUND;
3835 	thread_affinity_clear(mac_srs->srs_worker);
3836 	mutex_exit(&mac_srs->srs_lock);
3837 }
3838 
3839 static void
3840 mac_srs_poll_unbind(mac_soft_ring_set_t *mac_srs)
3841 {
3842 	mutex_enter(&mac_srs->srs_lock);
3843 	if (mac_srs->srs_poll_thr == NULL ||
3844 	    (mac_srs->srs_state & SRS_POLL_BOUND) == 0) {
3845 		ASSERT(mac_srs->srs_poll_cpuid == -1);
3846 		mutex_exit(&mac_srs->srs_lock);
3847 		return;
3848 	}
3849 
3850 	mac_srs->srs_poll_cpuid = -1;
3851 	mac_srs->srs_state &= ~SRS_POLL_BOUND;
3852 	thread_affinity_clear(mac_srs->srs_poll_thr);
3853 	mutex_exit(&mac_srs->srs_lock);
3854 }
3855 
3856 static void
3857 mac_srs_threads_unbind(mac_soft_ring_set_t *mac_srs)
3858 {
3859 	mac_soft_ring_t	*soft_ring;
3860 
3861 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
3862 
3863 	mutex_enter(&cpu_lock);
3864 	mac_srs_worker_unbind(mac_srs);
3865 	if (!(mac_srs->srs_type & SRST_TX))
3866 		mac_srs_poll_unbind(mac_srs);
3867 
3868 	for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
3869 	    soft_ring = soft_ring->s_ring_next) {
3870 		mac_soft_ring_unbind(soft_ring);
3871 	}
3872 	mutex_exit(&cpu_lock);
3873 }
3874 
3875 /*
3876  * When a CPU is going away, unbind all MAC threads which are bound
3877  * to that CPU. The affinity of the thread to the CPU is saved to allow
3878  * the thread to be rebound to the CPU if it comes back online.
3879  */
3880 static void
3881 mac_walk_srs_and_unbind(int cpuid)
3882 {
3883 	mac_soft_ring_set_t *mac_srs;
3884 	mac_soft_ring_t *soft_ring;
3885 
3886 	rw_enter(&mac_srs_g_lock, RW_READER);
3887 
3888 	if ((mac_srs = mac_srs_g_list) == NULL)
3889 		goto done;
3890 
3891 	for (; mac_srs != NULL; mac_srs = mac_srs->srs_next) {
3892 		if (mac_srs->srs_worker_cpuid == cpuid) {
3893 			mac_srs->srs_worker_cpuid_save = cpuid;
3894 			mac_srs_worker_unbind(mac_srs);
3895 		}
3896 
3897 		if (!(mac_srs->srs_type & SRST_TX)) {
3898 			if (mac_srs->srs_poll_cpuid == cpuid) {
3899 				mac_srs->srs_poll_cpuid_save = cpuid;
3900 				mac_srs_poll_unbind(mac_srs);
3901 			}
3902 		}
3903 
3904 		/* Next tackle the soft rings associated with the srs */
3905 		mutex_enter(&mac_srs->srs_lock);
3906 		for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
3907 		    soft_ring = soft_ring->s_ring_next) {
3908 			if (soft_ring->s_ring_cpuid == cpuid) {
3909 				soft_ring->s_ring_cpuid_save = cpuid;
3910 				mac_soft_ring_unbind(soft_ring);
3911 			}
3912 		}
3913 		mutex_exit(&mac_srs->srs_lock);
3914 	}
3915 done:
3916 	rw_exit(&mac_srs_g_lock);
3917 }
3918 
3919 /* TX SETUP and TEARDOWN ROUTINES */
3920 
3921 /*
3922  * XXXHIO need to make sure the two mac_tx_srs_{add,del}_ring()
3923  * handle the case where the number of rings is one. I.e. there is
3924  * a ring pointed to by mac_srs->srs_tx_arg2.
3925  */
3926 void
3927 mac_tx_srs_add_ring(mac_soft_ring_set_t *mac_srs, mac_ring_t *tx_ring)
3928 {
3929 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
3930 	mac_soft_ring_t *soft_ring;
3931 	int count = mac_srs->srs_tx_ring_count;
3932 	uint32_t soft_ring_type = ST_RING_TX;
3933 	uint_t ring_info;
3934 
3935 	ASSERT(mac_srs->srs_state & SRS_QUIESCE);
3936 	ring_info = mac_hwring_getinfo((mac_ring_handle_t)tx_ring);
3937 	if (mac_tx_serialize || (ring_info & MAC_RING_TX_SERIALIZE))
3938 		soft_ring_type |= ST_RING_WORKER_ONLY;
3939 	soft_ring = mac_soft_ring_create(count, 0,
3940 	    soft_ring_type, maxclsyspri, mcip, mac_srs, -1,
3941 	    NULL, mcip, (mac_resource_handle_t)tx_ring);
3942 	mac_srs->srs_tx_ring_count++;
3943 	mac_srs_update_fanout_list(mac_srs);
3944 	/*
3945 	 * put this soft ring in quiesce mode too so when we restart
3946 	 * all soft rings in the srs are in the same state.
3947 	 */
3948 	mac_soft_ring_signal(soft_ring, S_RING_QUIESCE);
3949 }
3950 
3951 static void
3952 mac_soft_ring_remove(mac_soft_ring_set_t *mac_srs, mac_soft_ring_t *softring)
3953 {
3954 	int sringcnt;
3955 
3956 	mutex_enter(&mac_srs->srs_lock);
3957 	sringcnt = mac_srs->srs_soft_ring_count;
3958 	ASSERT(sringcnt > 0);
3959 	mac_soft_ring_signal(softring, S_RING_CONDEMNED);
3960 
3961 	ASSERT(mac_srs->srs_soft_ring_condemned_count == 0);
3962 	while (mac_srs->srs_soft_ring_condemned_count != 1)
3963 		cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3964 
3965 	if (softring == mac_srs->srs_soft_ring_head) {
3966 		mac_srs->srs_soft_ring_head = softring->s_ring_next;
3967 		if (mac_srs->srs_soft_ring_head != NULL) {
3968 			mac_srs->srs_soft_ring_head->s_ring_prev = NULL;
3969 		} else {
3970 			mac_srs->srs_soft_ring_tail = NULL;
3971 		}
3972 	} else {
3973 		softring->s_ring_prev->s_ring_next =
3974 		    softring->s_ring_next;
3975 		if (softring->s_ring_next != NULL) {
3976 			softring->s_ring_next->s_ring_prev =
3977 			    softring->s_ring_prev;
3978 		} else {
3979 			mac_srs->srs_soft_ring_tail =
3980 			    softring->s_ring_prev;
3981 		}
3982 	}
3983 	mac_srs->srs_soft_ring_count--;
3984 
3985 	mac_srs->srs_soft_ring_condemned_count--;
3986 	mutex_exit(&mac_srs->srs_lock);
3987 
3988 	mac_soft_ring_free(softring);
3989 }
3990 
3991 void
3992 mac_tx_srs_del_ring(mac_soft_ring_set_t *mac_srs, mac_ring_t *tx_ring)
3993 {
3994 	int i;
3995 	mac_soft_ring_t *soft_ring, *remove_sring;
3996 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
3997 
3998 	mutex_enter(&mac_srs->srs_lock);
3999 	for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
4000 		soft_ring =  mac_srs->srs_tx_soft_rings[i];
4001 		if (soft_ring->s_ring_tx_arg2 == tx_ring)
4002 			break;
4003 	}
4004 	mutex_exit(&mac_srs->srs_lock);
4005 	ASSERT(i < mac_srs->srs_tx_ring_count);
4006 	remove_sring = soft_ring;
4007 	/*
4008 	 * In the case of aggr, the soft ring associated with a Tx ring
4009 	 * is also stored in st_soft_rings[] array. That entry should
4010 	 * be removed.
4011 	 */
4012 	if (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT) {
4013 		mac_srs_tx_t *tx = &mac_srs->srs_tx;
4014 
4015 		ASSERT(tx->st_soft_rings[tx_ring->mr_index] == remove_sring);
4016 		tx->st_soft_rings[tx_ring->mr_index] = NULL;
4017 	}
4018 	mac_soft_ring_remove(mac_srs, remove_sring);
4019 	mac_srs_update_fanout_list(mac_srs);
4020 }
4021 
4022 /*
4023  * mac_tx_srs_setup():
4024  * Used to setup Tx rings. If no free Tx ring is available, then default
4025  * Tx ring is used.
4026  */
4027 void
4028 mac_tx_srs_setup(mac_client_impl_t *mcip, flow_entry_t *flent)
4029 {
4030 	mac_impl_t		*mip = mcip->mci_mip;
4031 	mac_soft_ring_set_t	*tx_srs = flent->fe_tx_srs;
4032 	int			i;
4033 	int			tx_ring_count = 0;
4034 	uint32_t		soft_ring_type;
4035 	mac_group_t		*grp = NULL;
4036 	mac_ring_t		*ring;
4037 	mac_srs_tx_t		*tx = &tx_srs->srs_tx;
4038 	boolean_t		is_aggr;
4039 	uint_t			ring_info = 0;
4040 
4041 	is_aggr = (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT) != 0;
4042 	grp = flent->fe_tx_ring_group;
4043 	if (grp == NULL) {
4044 		ring = (mac_ring_t *)mip->mi_default_tx_ring;
4045 		goto no_group;
4046 	}
4047 	tx_ring_count = grp->mrg_cur_count;
4048 	ring = grp->mrg_rings;
4049 	/*
4050 	 * An attempt is made to reserve 'tx_ring_count' number
4051 	 * of Tx rings. If tx_ring_count is 0, default Tx ring
4052 	 * is used. If it is 1, an attempt is made to reserve one
4053 	 * Tx ring. In both the cases, the ring information is
4054 	 * stored in Tx SRS. If multiple Tx rings are specified,
4055 	 * then each Tx ring will have a Tx-side soft ring. All
4056 	 * these soft rings will be hang off Tx SRS.
4057 	 */
4058 	switch (grp->mrg_state) {
4059 		case MAC_GROUP_STATE_SHARED:
4060 		case MAC_GROUP_STATE_RESERVED:
4061 			if (tx_ring_count <= 1 && !is_aggr) {
4062 no_group:
4063 				if (ring != NULL &&
4064 				    ring->mr_state != MR_INUSE) {
4065 					(void) mac_start_ring(ring);
4066 					ring_info = mac_hwring_getinfo(
4067 					    (mac_ring_handle_t)ring);
4068 				}
4069 				tx->st_arg2 = (void *)ring;
4070 				mac_tx_srs_stat_recreate(tx_srs, B_FALSE);
4071 				if (tx_srs->srs_type & SRST_BW_CONTROL) {
4072 					tx->st_mode = SRS_TX_BW;
4073 				} else if (mac_tx_serialize ||
4074 				    (ring_info & MAC_RING_TX_SERIALIZE)) {
4075 					tx->st_mode = SRS_TX_SERIALIZE;
4076 				} else {
4077 					tx->st_mode = SRS_TX_DEFAULT;
4078 				}
4079 				break;
4080 			}
4081 			soft_ring_type = ST_RING_TX;
4082 			if (tx_srs->srs_type & SRST_BW_CONTROL) {
4083 				tx->st_mode = is_aggr ?
4084 				    SRS_TX_BW_AGGR : SRS_TX_BW_FANOUT;
4085 			} else {
4086 				tx->st_mode = is_aggr ? SRS_TX_AGGR :
4087 				    SRS_TX_FANOUT;
4088 			}
4089 			for (i = 0; i < tx_ring_count; i++) {
4090 				ASSERT(ring != NULL);
4091 				switch (ring->mr_state) {
4092 				case MR_INUSE:
4093 				case MR_FREE:
4094 					ASSERT(ring->mr_srs == NULL);
4095 
4096 					if (ring->mr_state != MR_INUSE)
4097 						(void) mac_start_ring(ring);
4098 					ring_info = mac_hwring_getinfo(
4099 					    (mac_ring_handle_t)ring);
4100 					if (mac_tx_serialize || (ring_info &
4101 					    MAC_RING_TX_SERIALIZE)) {
4102 						soft_ring_type |=
4103 						    ST_RING_WORKER_ONLY;
4104 					}
4105 					(void) mac_soft_ring_create(i, 0,
4106 					    soft_ring_type, maxclsyspri,
4107 					    mcip, tx_srs, -1, NULL, mcip,
4108 					    (mac_resource_handle_t)ring);
4109 					break;
4110 				default:
4111 					cmn_err(CE_PANIC,
4112 					    "srs_setup: mcip = %p "
4113 					    "trying to add UNKNOWN ring = %p\n",
4114 					    (void *)mcip, (void *)ring);
4115 					break;
4116 				}
4117 				ring = ring->mr_next;
4118 			}
4119 			mac_srs_update_fanout_list(tx_srs);
4120 			break;
4121 		default:
4122 			ASSERT(B_FALSE);
4123 			break;
4124 	}
4125 	tx->st_func = mac_tx_get_func(tx->st_mode);
4126 	if (is_aggr) {
4127 		VERIFY(i_mac_capab_get((mac_handle_t)mip,
4128 		    MAC_CAPAB_AGGR, &tx->st_capab_aggr));
4129 	}
4130 	DTRACE_PROBE3(tx__srs___setup__return, mac_soft_ring_set_t *, tx_srs,
4131 	    int, tx->st_mode, int, tx_srs->srs_tx_ring_count);
4132 }
4133 
4134 /*
4135  * Update the fanout of a client if its recorded link speed doesn't match
4136  * its current link speed.
4137  */
4138 void
4139 mac_fanout_recompute_client(mac_client_impl_t *mcip, cpupart_t *cpupart)
4140 {
4141 	uint64_t link_speed;
4142 	mac_resource_props_t *mcip_mrp;
4143 	flow_entry_t *flent = mcip->mci_flent;
4144 	mac_soft_ring_set_t *rx_srs;
4145 	mac_cpus_t *srs_cpu;
4146 	int soft_ring_count, maxcpus;
4147 
4148 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4149 
4150 	link_speed = mac_client_stat_get(mcip->mci_flent->fe_mcip,
4151 	    MAC_STAT_IFSPEED);
4152 
4153 	if ((link_speed != 0) &&
4154 	    (link_speed != mcip->mci_flent->fe_nic_speed)) {
4155 		mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
4156 		/*
4157 		 * Before calling mac_fanout_setup(), check to see if
4158 		 * the SRSes already have the right number of soft
4159 		 * rings. mac_fanout_setup() is a heavy duty operation
4160 		 * where new cpu bindings are done for SRS and soft
4161 		 * ring threads and interrupts re-targeted.
4162 		 */
4163 		maxcpus = (cpupart != NULL) ? cpupart->cp_ncpus : ncpus;
4164 		soft_ring_count = mac_compute_soft_ring_count(flent,
4165 		    flent->fe_rx_srs_cnt - 1, maxcpus);
4166 		/*
4167 		 * If soft_ring_count returned by
4168 		 * mac_compute_soft_ring_count() is 0, bump it
4169 		 * up by 1 because we always have atleast one
4170 		 * TCP, UDP, and OTH soft ring associated with
4171 		 * an SRS.
4172 		 */
4173 		soft_ring_count = (soft_ring_count == 0) ?
4174 		    1 : soft_ring_count;
4175 		rx_srs = flent->fe_rx_srs[0];
4176 		srs_cpu = &rx_srs->srs_cpu;
4177 		if (soft_ring_count != srs_cpu->mc_rx_fanout_cnt) {
4178 			mac_fanout_setup(mcip, flent, mcip_mrp,
4179 			    mac_rx_deliver, mcip, NULL, cpupart);
4180 		}
4181 	}
4182 }
4183 
4184 /*
4185  * Walk through the list of MAC clients for the MAC.
4186  * For each active MAC client, recompute the number of soft rings
4187  * associated with every client, only if current speed is different
4188  * from the speed that was previously used for soft ring computation.
4189  * If the cable is disconnected whlie the NIC is started, we would get
4190  * notification with speed set to 0. We do not recompute in that case.
4191  */
4192 void
4193 mac_fanout_recompute(mac_impl_t *mip)
4194 {
4195 	mac_client_impl_t	*mcip;
4196 	cpupart_t		*cpupart;
4197 	boolean_t		use_default;
4198 	mac_resource_props_t	*mrp, *emrp;
4199 
4200 	i_mac_perim_enter(mip);
4201 	if ((mip->mi_state_flags & MIS_IS_VNIC) != 0 ||
4202 	    mip->mi_linkstate != LINK_STATE_UP) {
4203 		i_mac_perim_exit(mip);
4204 		return;
4205 	}
4206 
4207 	for (mcip = mip->mi_clients_list; mcip != NULL;
4208 	    mcip = mcip->mci_client_next) {
4209 		/* Aggr port clients don't have SRSes. */
4210 		if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) != 0)
4211 			continue;
4212 
4213 		if ((mcip->mci_state_flags & MCIS_SHARE_BOUND) != 0 ||
4214 		    !MCIP_DATAPATH_SETUP(mcip))
4215 			continue;
4216 		mrp = MCIP_RESOURCE_PROPS(mcip);
4217 		emrp = MCIP_EFFECTIVE_PROPS(mcip);
4218 		use_default = B_FALSE;
4219 		pool_lock();
4220 		cpupart = mac_pset_find(mrp, &use_default);
4221 		mac_fanout_recompute_client(mcip, cpupart);
4222 		mac_set_pool_effective(use_default, cpupart, mrp, emrp);
4223 		pool_unlock();
4224 	}
4225 
4226 	i_mac_perim_exit(mip);
4227 }
4228 
4229 /*
4230  * Given a MAC, change the polling state for all its MAC clients.  'enable' is
4231  * B_TRUE to enable polling or B_FALSE to disable.  Polling is enabled by
4232  * default.
4233  */
4234 void
4235 mac_poll_state_change(mac_handle_t mh, boolean_t enable)
4236 {
4237 	mac_impl_t *mip = (mac_impl_t *)mh;
4238 	mac_client_impl_t *mcip;
4239 
4240 	i_mac_perim_enter(mip);
4241 	if (enable)
4242 		mip->mi_state_flags &= ~MIS_POLL_DISABLE;
4243 	else
4244 		mip->mi_state_flags |= MIS_POLL_DISABLE;
4245 	for (mcip = mip->mi_clients_list; mcip != NULL;
4246 	    mcip = mcip->mci_client_next)
4247 		mac_client_update_classifier(mcip, B_TRUE);
4248 	i_mac_perim_exit(mip);
4249 }
4250