xref: /illumos-gate/usr/src/uts/common/io/mac/mac_datapath_setup.c (revision 2aa8db5932a99c01d32f2aea7dbbf15b4898169b)
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 *,
51     const mac_soft_ring_state_t);
52 static void mac_srs_update_fanout_list(mac_soft_ring_set_t *);
53 static void mac_srs_poll_unbind(mac_soft_ring_set_t *);
54 static void mac_srs_worker_unbind(mac_soft_ring_set_t *);
55 static void mac_srs_soft_rings_quiesce(mac_soft_ring_set_t *,
56     const mac_soft_ring_state_t);
57 
58 static int mac_srs_cpu_setup(cpu_setup_t, int, void *);
59 static void mac_srs_worker_bind(mac_soft_ring_set_t *, processorid_t);
60 static void mac_srs_poll_bind(mac_soft_ring_set_t *, processorid_t);
61 static void mac_srs_threads_unbind(mac_soft_ring_set_t *);
62 static void mac_srs_add_glist(mac_soft_ring_set_t *);
63 static void mac_srs_remove_glist(mac_soft_ring_set_t *);
64 static void mac_srs_fanout_list_free(mac_soft_ring_set_t *);
65 static void mac_soft_ring_remove(mac_soft_ring_set_t *, mac_soft_ring_t *);
66 
67 static int mac_compute_soft_ring_count(flow_entry_t *, int, int);
68 static void mac_walk_srs_and_bind(int);
69 static void mac_walk_srs_and_unbind(int);
70 
71 extern boolean_t mac_latency_optimize;
72 
73 static kmem_cache_t *mac_srs_cache;
74 kmem_cache_t *mac_soft_ring_cache;
75 
76 /*
77  * The duration in msec we wait before signalling the soft ring
78  * worker thread in case packets get queued.
79  */
80 uint32_t mac_soft_ring_worker_wait = 0;
81 
82 /*
83  * A global tunable for turning polling on/off. By default, dynamic
84  * polling is always on and is always very beneficial. It should be
85  * turned off with absolute care and for the rare workload (very
86  * low latency sensitive traffic).
87  */
88 int mac_poll_enable = B_TRUE;
89 
90 /*
91  * Need to set mac_soft_ring_max_q_cnt based on bandwidth and perhaps latency.
92  * Large values could end up in consuming lot of system memory and cause
93  * system hang.
94  */
95 int mac_soft_ring_max_q_cnt = 1024;
96 int mac_soft_ring_min_q_cnt = 256;
97 int mac_soft_ring_poll_thres = 16;
98 
99 boolean_t mac_tx_serialize = B_FALSE;
100 
101 /*
102  * mac_tx_srs_hiwat is the queue depth threshold at which callers of
103  * mac_tx() will be notified of flow control condition.
104  *
105  * TCP does not honour flow control condition sent up by mac_tx().
106  * Thus provision is made for TCP to allow more packets to be queued
107  * in SRS upto a maximum of mac_tx_srs_max_q_cnt.
108  *
109  * Note that mac_tx_srs_hiwat is always be lesser than
110  * mac_tx_srs_max_q_cnt.
111  */
112 uint32_t mac_tx_srs_max_q_cnt = 100000;
113 uint32_t mac_tx_srs_hiwat = 1000;
114 
115 /*
116  * mac_rx_soft_ring_count, mac_soft_ring_10gig_count:
117  *
118  * Global tunables that determines the number of soft rings to be used for
119  * fanning out incoming traffic on a link. These count will be used only
120  * when no explicit set of CPUs was assigned to the data-links.
121  *
122  * mac_rx_soft_ring_count tunable will come into effect only if
123  * mac_soft_ring_enable is set. mac_soft_ring_enable is turned on by
124  * default only for sun4v platforms.
125  *
126  * mac_rx_soft_ring_10gig_count will come into effect if you are running on a
127  * 10Gbps link and is not dependent upon mac_soft_ring_enable.
128  *
129  * The number of soft rings for fanout for a link or a flow is determined
130  * by mac_compute_soft_ring_count() routine. This routine will take into
131  * account mac_soft_ring_enable, mac_rx_soft_ring_count and
132  * mac_rx_soft_ring_10gig_count to determine the soft ring count for a link.
133  *
134  * If a bandwidth is specified, the determination of the number of soft
135  * rings is based on specified bandwidth, CPU speed and number of CPUs in
136  * the system.
137  */
138 uint_t mac_rx_soft_ring_count = 8;
139 uint_t mac_rx_soft_ring_10gig_count = 8;
140 
141 /*
142  * Every Tx and Rx mac_soft_ring_set_t (mac_srs) created gets added
143  * to mac_srs_g_list and mac_srs_g_lock protects mac_srs_g_list. The
144  * list is used to walk the list of all MAC threads when a CPU is
145  * coming online or going offline.
146  */
147 static mac_soft_ring_set_t *mac_srs_g_list = NULL;
148 static krwlock_t mac_srs_g_lock;
149 
150 /*
151  * Whether the SRS threads should be bound, or not.
152  */
153 boolean_t mac_srs_thread_bind = B_TRUE;
154 
155 /*
156  * Whether Rx/Tx interrupts should be re-targeted. Disabled by default.
157  * dladm command would override this.
158  */
159 boolean_t mac_tx_intr_retarget = B_FALSE;
160 boolean_t mac_rx_intr_retarget = B_FALSE;
161 
162 /*
163  * If cpu bindings are specified by user, then Tx SRS and its soft
164  * rings should also be bound to the CPUs specified by user. The
165  * CPUs for Tx bindings are at the end of the cpu list provided by
166  * the user. If enough CPUs are not available (for Tx and Rx
167  * SRSes), then the CPUs are shared by both Tx and Rx SRSes.
168  */
169 #define	BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp) {			\
170 	processorid_t cpuid;						\
171 	int i;								\
172 	mac_soft_ring_t *softring;					\
173 	mac_cpus_t *srs_cpu;						\
174 									\
175 	srs_cpu = &mac_tx_srs->srs_cpu;					\
176 	cpuid = srs_cpu->mc_tx_fanout_cpus[0];				\
177 	mac_srs_worker_bind(mac_tx_srs, cpuid);				\
178 	if (MAC_TX_SOFT_RINGS(mac_tx_srs)) {				\
179 		for (i = 0; i < mac_tx_srs->srs_tx_ring_count; i++) {	\
180 			cpuid = srs_cpu->mc_tx_fanout_cpus[i];		\
181 			softring = mac_tx_srs->srs_tx_soft_rings[i];	\
182 			if (cpuid != -1) {				\
183 				(void) mac_soft_ring_bind(softring,	\
184 				    cpuid);				\
185 			}						\
186 		}							\
187 	}								\
188 }
189 
190 /*
191  * Re-targeting is allowed only for exclusive group or for primary.
192  */
193 #define	RETARGETABLE_CLIENT(group, mcip)				\
194 	((((group) != NULL) &&						\
195 	    ((group)->mrg_state == MAC_GROUP_STATE_RESERVED)) ||	\
196 	    mac_is_primary_client(mcip))
197 
198 #define	MAC_RING_RETARGETABLE(ring)					\
199 	(((ring) != NULL) &&						\
200 	    ((ring)->mr_info.mri_intr.mi_ddi_handle != NULL) &&		\
201 	    !((ring)->mr_info.mri_intr.mi_ddi_shared))
202 
203 
204 /* INIT and FINI ROUTINES */
205 
206 void
207 mac_soft_ring_init(void)
208 {
209 	mac_soft_ring_cache = kmem_cache_create("mac_soft_ring_cache",
210 	    sizeof (mac_soft_ring_t), 64, NULL, NULL, NULL, NULL, NULL, 0);
211 
212 	mac_srs_cache = kmem_cache_create("mac_srs_cache",
213 	    sizeof (mac_soft_ring_set_t),
214 	    64, NULL, NULL, NULL, NULL, NULL, 0);
215 
216 	rw_init(&mac_srs_g_lock, NULL, RW_DEFAULT, NULL);
217 	mutex_enter(&cpu_lock);
218 	register_cpu_setup_func(mac_srs_cpu_setup, NULL);
219 	mutex_exit(&cpu_lock);
220 }
221 
222 void
223 mac_soft_ring_finish(void)
224 {
225 	mutex_enter(&cpu_lock);
226 	unregister_cpu_setup_func(mac_srs_cpu_setup, NULL);
227 	mutex_exit(&cpu_lock);
228 	rw_destroy(&mac_srs_g_lock);
229 	kmem_cache_destroy(mac_soft_ring_cache);
230 	kmem_cache_destroy(mac_srs_cache);
231 }
232 
233 static void
234 mac_srs_soft_rings_free(mac_soft_ring_set_t *mac_srs)
235 {
236 	mac_soft_ring_t	*softring, *next, *head;
237 
238 	/*
239 	 * Synchronize with mac_walk_srs_bind/unbind which are callbacks from
240 	 * DR. The callbacks from DR are called with cpu_lock held, and hence
241 	 * can't wait to grab the mac perimeter. The soft ring list is hence
242 	 * protected for read access by srs_lock. Changing the soft ring list
243 	 * needs the mac perimeter and the srs_lock.
244 	 */
245 	mutex_enter(&mac_srs->srs_lock);
246 
247 	head = mac_srs->srs_soft_ring_head;
248 	mac_srs->srs_soft_ring_head = NULL;
249 	mac_srs->srs_soft_ring_tail = NULL;
250 	mac_srs->srs_soft_ring_count = 0;
251 
252 	mutex_exit(&mac_srs->srs_lock);
253 
254 	for (softring = head; softring != NULL; softring = next) {
255 		next = softring->s_ring_next;
256 		mac_soft_ring_free(softring);
257 	}
258 }
259 
260 static void
261 mac_srs_add_glist(mac_soft_ring_set_t *mac_srs)
262 {
263 	ASSERT(mac_srs->srs_next == NULL && mac_srs->srs_prev == NULL);
264 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
265 
266 	rw_enter(&mac_srs_g_lock, RW_WRITER);
267 	mutex_enter(&mac_srs->srs_lock);
268 
269 	ASSERT((mac_srs->srs_state & SRS_IN_GLIST) == 0);
270 
271 	if (mac_srs_g_list == NULL) {
272 		mac_srs_g_list = mac_srs;
273 	} else {
274 		mac_srs->srs_next = mac_srs_g_list;
275 		mac_srs_g_list->srs_prev = mac_srs;
276 		mac_srs->srs_prev = NULL;
277 		mac_srs_g_list = mac_srs;
278 	}
279 	mac_srs->srs_state |= SRS_IN_GLIST;
280 
281 	mutex_exit(&mac_srs->srs_lock);
282 	rw_exit(&mac_srs_g_lock);
283 }
284 
285 static void
286 mac_srs_remove_glist(mac_soft_ring_set_t *mac_srs)
287 {
288 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
289 
290 	rw_enter(&mac_srs_g_lock, RW_WRITER);
291 	mutex_enter(&mac_srs->srs_lock);
292 
293 	ASSERT((mac_srs->srs_state & SRS_IN_GLIST) != 0);
294 
295 	if (mac_srs == mac_srs_g_list) {
296 		mac_srs_g_list = mac_srs->srs_next;
297 		if (mac_srs_g_list != NULL)
298 			mac_srs_g_list->srs_prev = NULL;
299 	} else {
300 		mac_srs->srs_prev->srs_next = mac_srs->srs_next;
301 		if (mac_srs->srs_next != NULL)
302 			mac_srs->srs_next->srs_prev = mac_srs->srs_prev;
303 	}
304 	mac_srs->srs_state &= ~SRS_IN_GLIST;
305 
306 	mutex_exit(&mac_srs->srs_lock);
307 	rw_exit(&mac_srs_g_lock);
308 }
309 
310 /* POLLING SETUP AND TEAR DOWN ROUTINES */
311 
312 /*
313  * Quiesce polling on the TCP/IP squeues.
314  */
315 void
316 mac_srs_client_poll_quiesce(mac_client_impl_t *mcip, mac_soft_ring_set_t *srs)
317 {
318 	VERIFY(mac_perim_held((mac_handle_t)mcip->mci_mip));
319 
320 	if (srs->srs_type & SRST_CLIENT_POLL_V4) {
321 		for (uint_t i = 0; i < srs->srs_tcp_ring_count; i++) {
322 			mac_soft_ring_t *sr = srs->srs_tcp_soft_rings[i];
323 
324 			if (sr->s_ring_rx_arg2 != NULL) {
325 				mcip->mci_rcb4.mrc_quiesce(
326 				    mcip->mci_rcb4.mrc_arg, sr->s_ring_rx_arg2);
327 			}
328 		}
329 	}
330 
331 	if (srs->srs_type & SRST_CLIENT_POLL_V6) {
332 		for (uint_t i = 0; i < srs->srs_tcp6_ring_count; i++) {
333 			mac_soft_ring_t *sr = srs->srs_tcp6_soft_rings[i];
334 
335 			if (sr->s_ring_rx_arg2 != NULL) {
336 				mcip->mci_rcb6.mrc_quiesce(
337 				    mcip->mci_rcb6.mrc_arg, sr->s_ring_rx_arg2);
338 			}
339 		}
340 	}
341 }
342 
343 /*
344  * Restart polling on the TCP/IP squeues.
345  */
346 void
347 mac_srs_client_poll_restart(mac_client_impl_t *mcip, mac_soft_ring_set_t *srs)
348 {
349 	VERIFY(mac_perim_held((mac_handle_t)mcip->mci_mip));
350 
351 	if (srs->srs_type & SRST_CLIENT_POLL_V4) {
352 		for (uint_t i = 0; i < srs->srs_tcp_ring_count; i++) {
353 			mac_soft_ring_t *sr = srs->srs_tcp_soft_rings[i];
354 
355 			if (sr->s_ring_rx_arg2 != NULL) {
356 				mcip->mci_rcb4.mrc_restart(
357 				    mcip->mci_rcb4.mrc_arg, sr->s_ring_rx_arg2);
358 			}
359 		}
360 	}
361 
362 	if (srs->srs_type & SRST_CLIENT_POLL_V6) {
363 		for (uint_t i = 0; i < srs->srs_tcp6_ring_count; i++) {
364 			mac_soft_ring_t *sr = srs->srs_tcp6_soft_rings[i];
365 
366 			if (sr->s_ring_rx_arg2 != NULL) {
367 				mcip->mci_rcb6.mrc_restart(
368 				    mcip->mci_rcb6.mrc_arg, sr->s_ring_rx_arg2);
369 			}
370 		}
371 	}
372 }
373 
374 static void
375 mac_srs_client_poll_enable_i(mac_soft_ring_set_t *srs, uint_t sr_cnt,
376     mac_soft_ring_t **udp_rings, mac_soft_ring_t **tcp_rings,
377     mac_direct_rx_t drx, void *drx_arg, mac_resource_cb_t *rcb)
378 {
379 	/*
380 	 * TCP and UDP support DLS bypass. Squeue polling support implies DLS
381 	 * bypass since the squeue poll path does not have DLS processing.
382 	 */
383 	for (uint_t i = 0; i < sr_cnt; i++) {
384 		mac_soft_ring_dls_bypass_enable(udp_rings[i], drx, drx_arg);
385 	}
386 
387 	for (uint_t i = 0; i < sr_cnt; i++) {
388 		mac_soft_ring_poll_enable(tcp_rings[i], drx, drx_arg, rcb,
389 		    srs->srs_pri);
390 	}
391 }
392 
393 /*
394  * Register the given SRS and associated soft rings with the consumer and
395  * enable the polling interface used by the consumer.(i.e IP) over this
396  * SRS and associated soft rings.
397  */
398 void
399 mac_srs_client_poll_enable(mac_client_impl_t *mcip, mac_soft_ring_set_t *srs,
400     boolean_t is_v6)
401 {
402 	VERIFY3P(srs->srs_mcip, ==, mcip);
403 	VERIFY(mac_perim_held((mac_handle_t)mcip->mci_mip));
404 
405 	if (!(mcip->mci_state_flags & MCIS_CLIENT_POLL_CAPABLE))
406 		return;
407 
408 	/*
409 	 * A SRS is capable of acting as a soft ring for cases
410 	 * where no fanout is needed. This is the case for userland
411 	 * flows.
412 	 */
413 	if (srs->srs_type & SRST_NO_SOFT_RINGS)
414 		return;
415 
416 	/*
417 	 * Once mci_direct_rx is set for a given protocol (IPv4/IPv6) it is not
418 	 * cleared. We probably should clear it when there is no longer a
419 	 * client, but we don't. The resource callbacks in mci_rcb4/6, however,
420 	 * are cleared when polling is disabled. So, even though DLS and polling
421 	 * currently come as a pair, we make sure to check both mci_direct_rx
422 	 * and mci_rcb4/6 before attemping to enable polling.
423 	 */
424 	if (is_v6 && mcip->mci_direct_rx.mdrx_v6 != NULL &&
425 	    mcip->mci_rcb6.mrc_arg != NULL) {
426 		mac_srs_client_poll_enable_i(srs, srs->srs_tcp_ring_count,
427 		    srs->srs_udp6_soft_rings, srs->srs_tcp6_soft_rings,
428 		    mcip->mci_direct_rx.mdrx_v6,
429 		    mcip->mci_direct_rx.mdrx_arg_v6, &mcip->mci_rcb6);
430 
431 		mutex_enter(&srs->srs_lock);
432 		srs->srs_type |= (SRST_CLIENT_POLL_V6 | SRST_DLS_BYPASS_V6);
433 		mutex_exit(&srs->srs_lock);
434 	} else if (!is_v6 && mcip->mci_direct_rx.mdrx_v4 != NULL &&
435 	    mcip->mci_rcb4.mrc_arg != NULL) {
436 		mac_srs_client_poll_enable_i(srs, srs->srs_tcp_ring_count,
437 		    srs->srs_udp_soft_rings, srs->srs_tcp_soft_rings,
438 		    mcip->mci_direct_rx.mdrx_v4,
439 		    mcip->mci_direct_rx.mdrx_arg_v4, &mcip->mci_rcb4);
440 
441 		mutex_enter(&srs->srs_lock);
442 		srs->srs_type |= (SRST_CLIENT_POLL_V4 | SRST_DLS_BYPASS_V4);
443 		mutex_exit(&srs->srs_lock);
444 	}
445 }
446 
447 static void
448 mac_srs_client_poll_disable_i(mac_client_impl_t *mcip, uint_t sr_cnt,
449     mac_soft_ring_t **udp_rings, mac_soft_ring_t **tcp_rings,
450     mac_resource_cb_t *rcb)
451 {
452 	for (uint_t i = 0; i < sr_cnt; i++) {
453 		mac_soft_ring_poll_disable(tcp_rings[i], rcb, mcip);
454 	}
455 
456 	for (uint_t i = 0; i < sr_cnt; i++) {
457 		mac_soft_ring_t *udp_sr = udp_rings[i];
458 
459 		/* There is no polling on UDP; this should always be NULL. */
460 		VERIFY3P(udp_sr->s_ring_rx_arg2, ==, NULL);
461 		mac_soft_ring_dls_bypass_disable(udp_sr, mcip);
462 	}
463 }
464 
465 /*
466  * Unregister the given SRS and associated soft rings with the consumer and
467  * disable the polling interface used by the consumer (i.e IP) over this
468  * SRS and associated soft rings.
469  */
470 void
471 mac_srs_client_poll_disable(mac_client_impl_t *mcip, mac_soft_ring_set_t *srs,
472     boolean_t is_v6)
473 {
474 	VERIFY(mac_perim_held((mac_handle_t)mcip->mci_mip));
475 
476 	/*
477 	 * A SRS is capable of acting as a soft ring for cases
478 	 * where no protocol fanout is needed. This is the case
479 	 * for userland flows. Nothing to do here.
480 	 */
481 	if (srs->srs_type & SRST_NO_SOFT_RINGS)
482 		return;
483 
484 	mutex_enter(&srs->srs_lock);
485 	if (!is_v6 && !(srs->srs_type & SRST_CLIENT_POLL_V4)) {
486 		VERIFY(!(srs->srs_type & SRST_DLS_BYPASS_V4));
487 		mutex_exit(&srs->srs_lock);
488 		return;
489 	}
490 
491 	if (is_v6 && !(srs->srs_type & SRST_CLIENT_POLL_V6)) {
492 		VERIFY(!(srs->srs_type & SRST_DLS_BYPASS_V6));
493 		mutex_exit(&srs->srs_lock);
494 		return;
495 	}
496 
497 	/*
498 	 * Before modifying TCP/UDP softring state we must first inform the SRS
499 	 * that DLS bypass is no longer to be performed; thereby directing all
500 	 * future traffic to the OTH softring.
501 	 */
502 	if (is_v6) {
503 		srs->srs_type &= ~(SRST_CLIENT_POLL_V6 |
504 		    SRST_DLS_BYPASS_V6);
505 	} else {
506 		srs->srs_type &= ~(SRST_CLIENT_POLL_V4 |
507 		    SRST_DLS_BYPASS_V4);
508 	}
509 
510 	mutex_exit(&srs->srs_lock);
511 
512 	if (is_v6) {
513 		mac_srs_client_poll_disable_i(mcip, srs->srs_tcp_ring_count,
514 		    srs->srs_udp6_soft_rings, srs->srs_tcp6_soft_rings,
515 		    &mcip->mci_rcb6);
516 	} else {
517 		mac_srs_client_poll_disable_i(mcip, srs->srs_tcp_ring_count,
518 		    srs->srs_udp_soft_rings, srs->srs_tcp_soft_rings,
519 		    &mcip->mci_rcb4);
520 	}
521 }
522 
523 /*
524  * Enable or disable poll capability of the SRS on the underlying Rx ring.
525  *
526  * There is a need to enable or disable the poll capability of an SRS over an
527  * Rx ring depending on the number of mac clients sharing the ring and also
528  * whether user flows are configured on it. However the poll state is actively
529  * manipulated by the SRS worker and poll threads and uncoordinated changes by
530  * yet another thread to the underlying capability can surprise them leading
531  * to assert failures. Instead we quiesce the SRS, make the changes and then
532  * restart the SRS.
533  */
534 static void
535 mac_srs_poll_state_change(mac_soft_ring_set_t *mac_srs,
536     boolean_t turn_off_poll_capab, mac_rx_func_t rx_func)
537 {
538 	boolean_t	need_restart = B_FALSE;
539 	mac_srs_rx_t	*srs_rx = &mac_srs->srs_rx;
540 	mac_ring_t	*ring;
541 
542 	if (!SRS_QUIESCED(mac_srs)) {
543 		mac_rx_srs_quiesce(mac_srs, SRS_QUIESCE);
544 		need_restart = B_TRUE;
545 	}
546 
547 	ring = mac_srs->srs_ring;
548 	if ((ring != NULL) &&
549 	    (ring->mr_classify_type == MAC_HW_CLASSIFIER)) {
550 		if (turn_off_poll_capab)
551 			mac_srs->srs_state &= ~SRS_POLLING_CAPAB;
552 		else if (mac_poll_enable)
553 			mac_srs->srs_state |= SRS_POLLING_CAPAB;
554 	}
555 	srs_rx->sr_lower_proc = rx_func;
556 
557 	if (need_restart)
558 		mac_rx_srs_restart(mac_srs);
559 }
560 
561 /* CPU RECONFIGURATION AND FANOUT COMPUTATION ROUTINES */
562 
563 /*
564  * Return the next CPU to be used to bind a MAC kernel thread.
565  * If a cpupart is specified, the cpu chosen must be from that
566  * cpu partition.
567  */
568 static processorid_t
569 mac_next_bind_cpu(cpupart_t *cpupart)
570 {
571 	static cpu_t		*cp = NULL;
572 	cpu_t			*cp_start;
573 
574 	ASSERT(MUTEX_HELD(&cpu_lock));
575 
576 	if (cp == NULL)
577 		cp = cpu_list;
578 
579 	cp = cp->cpu_next_onln;
580 	cp_start = cp;
581 
582 	do {
583 		if ((cpupart == NULL) || (cp->cpu_part == cpupart))
584 			return (cp->cpu_id);
585 
586 	} while ((cp = cp->cpu_next_onln) != cp_start);
587 
588 	return (-1);	/* No matching CPU found online */
589 }
590 
591 /* ARGSUSED */
592 static int
593 mac_srs_cpu_setup(cpu_setup_t what, int id, void *arg)
594 {
595 	ASSERT(MUTEX_HELD(&cpu_lock));
596 	switch (what) {
597 	case CPU_CONFIG:
598 	case CPU_ON:
599 	case CPU_CPUPART_IN:
600 		mac_walk_srs_and_bind(id);
601 		break;
602 
603 	case CPU_UNCONFIG:
604 	case CPU_OFF:
605 	case CPU_CPUPART_OUT:
606 		mac_walk_srs_and_unbind(id);
607 		break;
608 
609 	default:
610 		break;
611 	}
612 	return (0);
613 }
614 
615 /*
616  * mac_compute_soft_ring_count():
617  *
618  * This routine computes the number of soft rings needed to handle incoming
619  * load given a flow_entry.
620  *
621  * The routine does the following:
622  * 1) soft rings will be created if mac_soft_ring_enable is set.
623  * 2) If the underlying link is a 10Gbps link, then soft rings will be
624  * created even if mac_soft_ring_enable is not set. The number of soft
625  * rings, so created,  will equal mac_rx_soft_ring_10gig_count.
626  * 3) On a sun4v platform (i.e., mac_soft_ring_enable is set), 2 times the
627  * mac_rx_soft_ring_10gig_count number of soft rings will be created for a
628  * 10Gbps link.
629  *
630  * If a bandwidth limit is specified, the number that gets computed is
631  * dependent upon CPU speed, the number of Rx rings configured, and
632  * the bandwidth limit.
633  * If more Rx rings are available, less number of soft rings is needed.
634  *
635  * mac_use_bw_heuristic is another "hidden" variable that can be used to
636  * override the default use of soft ring count computation. Depending upon
637  * the usefulness of it, mac_use_bw_heuristic can later be made into a
638  * data-link property or removed altogether.
639  *
640  * TODO: Cleanup and tighten some of the assumptions.
641  */
642 boolean_t mac_check_overlay = B_TRUE;
643 boolean_t mac_use_bw_heuristic = B_TRUE;
644 static int
645 mac_compute_soft_ring_count(flow_entry_t *flent, int rx_srs_cnt, int maxcpus)
646 {
647 	uint64_t cpu_speed, bw = 0;
648 	int srings = 0;
649 	boolean_t bw_enabled = B_FALSE;
650 	mac_client_impl_t *mcip = flent->fe_mcip;
651 
652 	ASSERT(!(flent->fe_type & FLOW_USER));
653 	if (flent->fe_resource_props.mrp_mask & MRP_MAXBW &&
654 	    mac_use_bw_heuristic) {
655 		/* bandwidth enabled */
656 		bw_enabled = B_TRUE;
657 		bw = flent->fe_resource_props.mrp_maxbw;
658 	}
659 	if (!bw_enabled) {
660 		/* No bandwidth enabled */
661 		if (mac_soft_ring_enable)
662 			srings = mac_rx_soft_ring_count;
663 
664 		/* Is this a 10Gig link? */
665 		flent->fe_nic_speed = mac_client_stat_get(
666 		    (mac_client_handle_t)flent->fe_mcip, MAC_STAT_IFSPEED);
667 		/* convert to Mbps */
668 		if (((flent->fe_nic_speed)/1000000) > 1000 &&
669 		    mac_rx_soft_ring_10gig_count > 0) {
670 			/* This is a 10Gig link */
671 			srings = mac_rx_soft_ring_10gig_count;
672 			/*
673 			 * Use 2 times mac_rx_soft_ring_10gig_count for
674 			 * sun4v systems.
675 			 */
676 			if (mac_soft_ring_enable)
677 				srings = srings * 2;
678 		} else if (mac_check_overlay == B_TRUE &&
679 		    (mcip->mci_state_flags & MCIS_IS_VNIC) != 0) {
680 			/* Is this a VNIC on an overlay? */
681 			mac_handle_t mh = (mac_handle_t)mcip->mci_mip;
682 			if (mac_is_overlay(mh) == B_TRUE) {
683 				srings = mac_rx_soft_ring_10gig_count;
684 			}
685 		}
686 
687 
688 	} else {
689 		/*
690 		 * Soft ring computation using CPU speed and specified
691 		 * bandwidth limit.
692 		 */
693 		/* Assumption: all CPUs have the same frequency */
694 		cpu_speed = (uint64_t)CPU->cpu_type_info.pi_clock;
695 
696 		/* cpu_speed is in MHz; make bw in units of Mbps.  */
697 		bw = bw/1000000;
698 
699 		if (bw >= 1000) {
700 			/*
701 			 * bw is greater than or equal to 1Gbps.
702 			 * The number of soft rings required is a function
703 			 * of bandwidth and CPU speed. To keep this simple,
704 			 * let's use this rule: 1GHz CPU can handle 1Gbps.
705 			 * If bw is less than 1 Gbps, then there is no need
706 			 * for soft rings. Assumption is that CPU speeds
707 			 * (on modern systems) are at least 1GHz.
708 			 */
709 			srings = bw/cpu_speed;
710 			if (srings <= 1 && mac_soft_ring_enable) {
711 				/*
712 				 * Give at least 2 soft rings
713 				 * for sun4v systems
714 				 */
715 				srings = 2;
716 			}
717 		}
718 	}
719 	/*
720 	 * If the flent has multiple Rx SRSs, then each SRS need not
721 	 * have that many soft rings on top of it. The number of
722 	 * soft rings for each Rx SRS is found by dividing srings by
723 	 * rx_srs_cnt.
724 	 */
725 	if (rx_srs_cnt > 1) {
726 		int remainder;
727 
728 		remainder = srings%rx_srs_cnt;
729 		srings = srings/rx_srs_cnt;
730 		if (remainder != 0)
731 			srings++;
732 		/*
733 		 * Fanning out to 1 soft ring is not very useful.
734 		 * Set it as well to 0 and mac_srs_fanout_init()
735 		 * will take care of creating a single soft ring
736 		 * for proto fanout.
737 		 */
738 		if (srings == 1)
739 			srings = 0;
740 	}
741 	/* Do some more massaging */
742 	srings = min(srings, maxcpus);
743 	srings = min(srings, MAX_SR_FANOUT);
744 	return (srings);
745 }
746 
747 /*
748  * mac_tx_cpu_init:
749  * set up CPUs for Tx interrupt re-targeting and Tx worker
750  * thread binding
751  */
752 static void
753 mac_tx_cpu_init(flow_entry_t *flent, mac_resource_props_t *mrp,
754     cpupart_t *cpupart)
755 {
756 	mac_soft_ring_set_t *tx_srs = flent->fe_tx_srs;
757 	mac_srs_tx_t *srs_tx = &tx_srs->srs_tx;
758 	mac_cpus_t *srs_cpu = &tx_srs->srs_cpu;
759 	mac_soft_ring_t *sringp;
760 	mac_ring_t *ring;
761 	processorid_t worker_cpuid;
762 	boolean_t retargetable_client = B_FALSE;
763 	int i, j;
764 
765 	if (RETARGETABLE_CLIENT((mac_group_t *)flent->fe_tx_ring_group,
766 	    flent->fe_mcip)) {
767 		retargetable_client = B_TRUE;
768 	}
769 
770 	if (MAC_TX_SOFT_RINGS(tx_srs)) {
771 		if (mrp != NULL)
772 			j = mrp->mrp_ncpus - 1;
773 		for (i = 0; i < tx_srs->srs_tx_ring_count; i++) {
774 			if (mrp != NULL) {
775 				if (j < 0)
776 					j = mrp->mrp_ncpus - 1;
777 				worker_cpuid = mrp->mrp_cpu[j];
778 			} else {
779 				/*
780 				 * Bind interrupt to the next CPU available
781 				 * and leave the worker unbound.
782 				 */
783 				worker_cpuid = -1;
784 			}
785 			sringp = tx_srs->srs_tx_soft_rings[i];
786 			ring = (mac_ring_t *)sringp->s_ring_tx_arg2;
787 			srs_cpu->mc_tx_fanout_cpus[i] = worker_cpuid;
788 			if (MAC_RING_RETARGETABLE(ring) &&
789 			    retargetable_client) {
790 				mutex_enter(&cpu_lock);
791 				srs_cpu->mc_tx_intr_cpu[i] =
792 				    (mrp != NULL) ? mrp->mrp_cpu[j] :
793 				    (mac_tx_intr_retarget ?
794 				    mac_next_bind_cpu(cpupart) : -1);
795 				mutex_exit(&cpu_lock);
796 			} else {
797 				srs_cpu->mc_tx_intr_cpu[i] = -1;
798 			}
799 			if (mrp != NULL)
800 				j--;
801 		}
802 	} else {
803 		/* Tx mac_ring_handle_t is stored in st_arg2 */
804 		srs_cpu->mc_tx_fanout_cpus[0] =
805 		    (mrp != NULL) ? mrp->mrp_cpu[mrp->mrp_ncpus - 1] : -1;
806 		ring = (mac_ring_t *)srs_tx->st_arg2;
807 		if (MAC_RING_RETARGETABLE(ring) && retargetable_client) {
808 			mutex_enter(&cpu_lock);
809 			srs_cpu->mc_tx_intr_cpu[0] = (mrp != NULL) ?
810 			    mrp->mrp_cpu[mrp->mrp_ncpus - 1] :
811 			    (mac_tx_intr_retarget ?
812 			    mac_next_bind_cpu(cpupart) : -1);
813 			mutex_exit(&cpu_lock);
814 		} else {
815 			srs_cpu->mc_tx_intr_cpu[0] = -1;
816 		}
817 	}
818 }
819 
820 /*
821  * Assignment of user specified CPUs to a link.
822  *
823  * Minimum CPUs required to get an optimal assignmet:
824  * For each Rx SRS, atleast two CPUs are needed if mac_latency_optimize
825  * flag is set -- one for polling, one for fanout soft ring.
826  * If mac_latency_optimize is not set, then 3 CPUs are needed -- one
827  * for polling, one for SRS worker thread and one for fanout soft ring.
828  *
829  * The CPUs needed for Tx side is equal to the number of Tx rings
830  * the link is using.
831  *
832  * mac_flow_user_cpu_init() categorizes the CPU assignment depending
833  * upon the number of CPUs in 3 different buckets.
834  *
835  * In the first bucket, the most optimal case is handled. The user has
836  * passed enough number of CPUs and every thread gets its own CPU.
837  *
838  * The second and third are the sub-optimal cases. Enough CPUs are not
839  * available.
840  *
841  * The second bucket handles the case where atleast one distinct CPU is
842  * is available for each of the Rx rings (Rx SRSes) and Tx rings (Tx
843  * SRS or soft rings).
844  *
845  * In the third case (worst case scenario), specified CPU count is less
846  * than the Rx rings configured for the link. In this case, we round
847  * robin the CPUs among the Rx SRSes and Tx SRS/soft rings.
848  */
849 static void
850 mac_flow_user_cpu_init(flow_entry_t *flent, mac_resource_props_t *mrp)
851 {
852 	mac_soft_ring_set_t *rx_srs, *tx_srs;
853 	int i, srs_cnt;
854 	mac_cpus_t *srs_cpu;
855 	int no_of_cpus, cpu_cnt;
856 	int rx_srs_cnt, reqd_rx_cpu_cnt;
857 	int fanout_cpu_cnt, reqd_tx_cpu_cnt;
858 	int reqd_poll_worker_cnt, fanout_cnt_per_srs;
859 	mac_resource_props_t *emrp = &flent->fe_effective_props;
860 
861 	ASSERT(mrp->mrp_fanout_mode == MCM_CPUS);
862 	/*
863 	 * The check for nbc_ncpus to be within limits for
864 	 * the user specified case was done earlier and if
865 	 * not within limits, an error would have been
866 	 * returned to the user.
867 	 */
868 	ASSERT(mrp->mrp_ncpus > 0);
869 
870 	no_of_cpus = mrp->mrp_ncpus;
871 
872 	if (mrp->mrp_rx_intr_cpu != -1) {
873 		/*
874 		 * interrupt has been re-targetted. Poll
875 		 * thread needs to be bound to interrupt
876 		 * CPU.
877 		 *
878 		 * Find where in the list is the intr
879 		 * CPU and swap it with the first one.
880 		 * We will be using the first CPU in the
881 		 * list for poll.
882 		 */
883 		for (i = 0; i < no_of_cpus; i++) {
884 			if (mrp->mrp_cpu[i] == mrp->mrp_rx_intr_cpu)
885 				break;
886 		}
887 		mrp->mrp_cpu[i] = mrp->mrp_cpu[0];
888 		mrp->mrp_cpu[0] = mrp->mrp_rx_intr_cpu;
889 	}
890 
891 	/*
892 	 * Requirements:
893 	 * The number of CPUs that each Rx ring needs is dependent
894 	 * upon mac_latency_optimize flag.
895 	 * 1) If set, atleast 2 CPUs are needed -- one for
896 	 * polling, one for fanout soft ring.
897 	 * 2) If not set, then atleast 3 CPUs are needed -- one
898 	 * for polling, one for srs worker thread, and one for
899 	 * fanout soft ring.
900 	 */
901 	rx_srs_cnt = (flent->fe_rx_srs_cnt > 1) ?
902 	    (flent->fe_rx_srs_cnt - 1) : flent->fe_rx_srs_cnt;
903 	reqd_rx_cpu_cnt = mac_latency_optimize ?
904 	    (rx_srs_cnt * 2) : (rx_srs_cnt * 3);
905 
906 	/* How many CPUs are needed for Tx side? */
907 	tx_srs = flent->fe_tx_srs;
908 	reqd_tx_cpu_cnt = MAC_TX_SOFT_RINGS(tx_srs) ?
909 	    tx_srs->srs_tx_ring_count : 1;
910 
911 	/* CPUs needed for Rx SRSes poll and worker threads */
912 	reqd_poll_worker_cnt = mac_latency_optimize ?
913 	    rx_srs_cnt : rx_srs_cnt * 2;
914 
915 	/* Has the user provided enough CPUs? */
916 	if (no_of_cpus >= (reqd_rx_cpu_cnt + reqd_tx_cpu_cnt)) {
917 		/*
918 		 * Best case scenario. There is enough CPUs. All
919 		 * Rx rings will get their own set of CPUs plus
920 		 * Tx soft rings will get their own.
921 		 */
922 		/*
923 		 * fanout_cpu_cnt is the number of CPUs available
924 		 * for Rx side fanout soft rings.
925 		 */
926 		fanout_cpu_cnt = no_of_cpus -
927 		    reqd_poll_worker_cnt - reqd_tx_cpu_cnt;
928 
929 		/*
930 		 * Divide fanout_cpu_cnt by rx_srs_cnt to find
931 		 * out how many fanout soft rings each Rx SRS
932 		 * can have.
933 		 */
934 		fanout_cnt_per_srs = fanout_cpu_cnt/rx_srs_cnt;
935 
936 		/* fanout_cnt_per_srs should not be >  MAX_SR_FANOUT */
937 		fanout_cnt_per_srs = min(fanout_cnt_per_srs, MAX_SR_FANOUT);
938 
939 		/* Do the assignment for the default Rx ring */
940 		cpu_cnt = 0;
941 		rx_srs = flent->fe_rx_srs[0];
942 		ASSERT(rx_srs->srs_ring == NULL);
943 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
944 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
945 		srs_cpu = &rx_srs->srs_cpu;
946 		srs_cpu->mc_ncpus = no_of_cpus;
947 		bcopy(mrp->mrp_cpu,
948 		    srs_cpu->mc_cpus, sizeof (srs_cpu->mc_cpus));
949 		srs_cpu->mc_rx_fanout_cnt = fanout_cnt_per_srs;
950 		srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt++];
951 		/* Retarget the interrupt to the same CPU as the poll */
952 		srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
953 		srs_cpu->mc_rx_workerid = (mac_latency_optimize ?
954 		    srs_cpu->mc_rx_pollid : mrp->mrp_cpu[cpu_cnt++]);
955 		for (i = 0; i < fanout_cnt_per_srs; i++)
956 			srs_cpu->mc_rx_fanout_cpus[i] = mrp->mrp_cpu[cpu_cnt++];
957 
958 		/* Do the assignment for h/w Rx SRSes */
959 		if (flent->fe_rx_srs_cnt > 1) {
960 			cpu_cnt = 0;
961 			for (srs_cnt = 1;
962 			    srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
963 				rx_srs = flent->fe_rx_srs[srs_cnt];
964 				ASSERT(rx_srs->srs_ring != NULL);
965 				if (rx_srs->srs_fanout_state ==
966 				    SRS_FANOUT_INIT) {
967 					rx_srs->srs_fanout_state =
968 					    SRS_FANOUT_REINIT;
969 				}
970 				srs_cpu = &rx_srs->srs_cpu;
971 				srs_cpu->mc_ncpus = no_of_cpus;
972 				bcopy(mrp->mrp_cpu, srs_cpu->mc_cpus,
973 				    sizeof (srs_cpu->mc_cpus));
974 				srs_cpu->mc_rx_fanout_cnt = fanout_cnt_per_srs;
975 				/* The first CPU in the list is the intr CPU */
976 				srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt++];
977 				srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
978 				srs_cpu->mc_rx_workerid =
979 				    (mac_latency_optimize ?
980 				    srs_cpu->mc_rx_pollid :
981 				    mrp->mrp_cpu[cpu_cnt++]);
982 				for (i = 0; i < fanout_cnt_per_srs; i++) {
983 					srs_cpu->mc_rx_fanout_cpus[i] =
984 					    mrp->mrp_cpu[cpu_cnt++];
985 				}
986 				ASSERT(cpu_cnt <= no_of_cpus);
987 			}
988 		}
989 		goto tx_cpu_init;
990 	}
991 
992 	/*
993 	 * Sub-optimal case.
994 	 * We have the following information:
995 	 * no_of_cpus - no. of cpus that user passed.
996 	 * rx_srs_cnt - no. of rx rings.
997 	 * reqd_rx_cpu_cnt = mac_latency_optimize?rx_srs_cnt*2:rx_srs_cnt*3
998 	 * reqd_tx_cpu_cnt - no. of cpus reqd. for Tx side.
999 	 * reqd_poll_worker_cnt = mac_latency_optimize?rx_srs_cnt:rx_srs_cnt*2
1000 	 */
1001 	/*
1002 	 * If we bind the Rx fanout soft rings to the same CPUs
1003 	 * as poll/worker, would that be enough?
1004 	 */
1005 	if (no_of_cpus >= (rx_srs_cnt + reqd_tx_cpu_cnt)) {
1006 		boolean_t worker_assign = B_FALSE;
1007 
1008 		/*
1009 		 * If mac_latency_optimize is not set, are there
1010 		 * enough CPUs to assign a CPU for worker also?
1011 		 */
1012 		if (no_of_cpus >= (reqd_poll_worker_cnt + reqd_tx_cpu_cnt))
1013 			worker_assign = B_TRUE;
1014 		/*
1015 		 * Zero'th Rx SRS is the default Rx ring. It is not
1016 		 * associated with h/w Rx ring.
1017 		 */
1018 		rx_srs = flent->fe_rx_srs[0];
1019 		ASSERT(rx_srs->srs_ring == NULL);
1020 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
1021 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
1022 		cpu_cnt = 0;
1023 		srs_cpu = &rx_srs->srs_cpu;
1024 		srs_cpu->mc_ncpus = no_of_cpus;
1025 		bcopy(mrp->mrp_cpu,
1026 		    srs_cpu->mc_cpus, sizeof (srs_cpu->mc_cpus));
1027 		srs_cpu->mc_rx_fanout_cnt = 1;
1028 		srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt++];
1029 		/* Retarget the interrupt to the same CPU as the poll */
1030 		srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
1031 		srs_cpu->mc_rx_workerid =
1032 		    ((!mac_latency_optimize && worker_assign) ?
1033 		    mrp->mrp_cpu[cpu_cnt++] : srs_cpu->mc_rx_pollid);
1034 
1035 		srs_cpu->mc_rx_fanout_cpus[0] = mrp->mrp_cpu[cpu_cnt];
1036 
1037 		/* Do CPU bindings for SRSes having h/w Rx rings */
1038 		if (flent->fe_rx_srs_cnt > 1) {
1039 			cpu_cnt = 0;
1040 			for (srs_cnt = 1;
1041 			    srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
1042 				rx_srs = flent->fe_rx_srs[srs_cnt];
1043 				ASSERT(rx_srs->srs_ring != NULL);
1044 				if (rx_srs->srs_fanout_state ==
1045 				    SRS_FANOUT_INIT) {
1046 					rx_srs->srs_fanout_state =
1047 					    SRS_FANOUT_REINIT;
1048 				}
1049 				srs_cpu = &rx_srs->srs_cpu;
1050 				srs_cpu->mc_ncpus = no_of_cpus;
1051 				bcopy(mrp->mrp_cpu, srs_cpu->mc_cpus,
1052 				    sizeof (srs_cpu->mc_cpus));
1053 				srs_cpu->mc_rx_pollid =
1054 				    mrp->mrp_cpu[cpu_cnt];
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] :
1059 				    srs_cpu->mc_rx_pollid);
1060 				srs_cpu->mc_rx_fanout_cnt = 1;
1061 				srs_cpu->mc_rx_fanout_cpus[0] =
1062 				    mrp->mrp_cpu[cpu_cnt];
1063 				cpu_cnt++;
1064 				ASSERT(cpu_cnt <= no_of_cpus);
1065 			}
1066 		}
1067 		goto tx_cpu_init;
1068 	}
1069 
1070 	/*
1071 	 * Real sub-optimal case. Not enough CPUs for poll and
1072 	 * Tx soft rings. Do a round robin assignment where
1073 	 * each Rx SRS will get the same CPU for poll, worker
1074 	 * and fanout soft ring.
1075 	 */
1076 	cpu_cnt = 0;
1077 	for (srs_cnt = 0; srs_cnt < flent->fe_rx_srs_cnt; srs_cnt++) {
1078 		rx_srs = flent->fe_rx_srs[srs_cnt];
1079 		srs_cpu = &rx_srs->srs_cpu;
1080 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
1081 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
1082 		srs_cpu->mc_ncpus = no_of_cpus;
1083 		bcopy(mrp->mrp_cpu,
1084 		    srs_cpu->mc_cpus, sizeof (srs_cpu->mc_cpus));
1085 		srs_cpu->mc_rx_fanout_cnt = 1;
1086 		srs_cpu->mc_rx_pollid = mrp->mrp_cpu[cpu_cnt];
1087 		/* Retarget the interrupt to the same CPU as the poll */
1088 		srs_cpu->mc_rx_intr_cpu = srs_cpu->mc_rx_pollid;
1089 		srs_cpu->mc_rx_workerid = mrp->mrp_cpu[cpu_cnt];
1090 		srs_cpu->mc_rx_fanout_cpus[0] = mrp->mrp_cpu[cpu_cnt];
1091 		if (++cpu_cnt >= no_of_cpus)
1092 			cpu_cnt = 0;
1093 	}
1094 
1095 tx_cpu_init:
1096 	mac_tx_cpu_init(flent, mrp, NULL);
1097 
1098 	/*
1099 	 * Copy the user specified CPUs to the effective CPUs
1100 	 */
1101 	for (i = 0; i < mrp->mrp_ncpus; i++) {
1102 		emrp->mrp_cpu[i] = mrp->mrp_cpu[i];
1103 	}
1104 	emrp->mrp_ncpus = mrp->mrp_ncpus;
1105 	emrp->mrp_mask = mrp->mrp_mask;
1106 	bzero(emrp->mrp_pool, MAXPATHLEN);
1107 }
1108 
1109 /*
1110  * mac_flow_cpu_init():
1111  *
1112  * Each SRS has a mac_cpu_t structure, srs_cpu. This routine fills in
1113  * the CPU binding information in srs_cpu for all Rx SRSes associated
1114  * with a flent.
1115  */
1116 static void
1117 mac_flow_cpu_init(flow_entry_t *flent, cpupart_t *cpupart)
1118 {
1119 	mac_soft_ring_set_t *rx_srs;
1120 	processorid_t cpuid;
1121 	int i, j, k, srs_cnt, maxcpus, soft_ring_cnt = 0;
1122 	mac_cpus_t *srs_cpu;
1123 	mac_resource_props_t *emrp = &flent->fe_effective_props;
1124 
1125 	/*
1126 	 * The maximum number of CPUs available can either be
1127 	 * the number of CPUs in the pool or the number of CPUs
1128 	 * in the system.
1129 	 */
1130 	maxcpus = (cpupart != NULL) ? cpupart->cp_ncpus : ncpus;
1131 	/*
1132 	 * We cannot exceed the hard limit imposed by data structures.
1133 	 * Leave space for polling CPU and the SRS worker thread when
1134 	 * "mac_latency_optimize" is not set.
1135 	 */
1136 	maxcpus = MIN(maxcpus, MRP_NCPUS - 2);
1137 
1138 	/*
1139 	 * Compute the number of soft rings needed on top for each Rx
1140 	 * SRS. "rx_srs_cnt-1" indicates the number of Rx SRS
1141 	 * associated with h/w Rx rings. Soft ring count needed for
1142 	 * each h/w Rx SRS is computed and the same is applied to
1143 	 * software classified Rx SRS. The first Rx SRS in fe_rx_srs[]
1144 	 * is the software classified Rx SRS.
1145 	 */
1146 	soft_ring_cnt = mac_compute_soft_ring_count(flent,
1147 	    flent->fe_rx_srs_cnt - 1, maxcpus);
1148 	if (soft_ring_cnt == 0) {
1149 		/*
1150 		 * Even when soft_ring_cnt is 0, we still need
1151 		 * to create a soft ring for TCP, UDP and
1152 		 * OTHER. So set it to 1.
1153 		 */
1154 		soft_ring_cnt = 1;
1155 	}
1156 
1157 	emrp->mrp_ncpus = 0;
1158 	for (srs_cnt = 0; srs_cnt < flent->fe_rx_srs_cnt &&
1159 	    emrp->mrp_ncpus < MRP_NCPUS; srs_cnt++) {
1160 		rx_srs = flent->fe_rx_srs[srs_cnt];
1161 		srs_cpu = &rx_srs->srs_cpu;
1162 		if (rx_srs->srs_fanout_state == SRS_FANOUT_INIT)
1163 			rx_srs->srs_fanout_state = SRS_FANOUT_REINIT;
1164 		srs_cpu->mc_ncpus = soft_ring_cnt;
1165 		srs_cpu->mc_rx_fanout_cnt = soft_ring_cnt;
1166 		mutex_enter(&cpu_lock);
1167 		for (j = 0; j < soft_ring_cnt; j++) {
1168 			cpuid = mac_next_bind_cpu(cpupart);
1169 			srs_cpu->mc_cpus[j] = cpuid;
1170 			srs_cpu->mc_rx_fanout_cpus[j] = cpuid;
1171 		}
1172 		cpuid = mac_next_bind_cpu(cpupart);
1173 		srs_cpu->mc_rx_pollid = cpuid;
1174 		srs_cpu->mc_rx_intr_cpu = (mac_rx_intr_retarget ?
1175 		    srs_cpu->mc_rx_pollid : -1);
1176 		/* increment ncpus to account for polling cpu */
1177 		srs_cpu->mc_ncpus++;
1178 		srs_cpu->mc_cpus[j++] = cpuid;
1179 		if (!mac_latency_optimize) {
1180 			cpuid = mac_next_bind_cpu(cpupart);
1181 			srs_cpu->mc_ncpus++;
1182 			srs_cpu->mc_cpus[j++] = cpuid;
1183 		}
1184 		srs_cpu->mc_rx_workerid = cpuid;
1185 		mutex_exit(&cpu_lock);
1186 
1187 		/*
1188 		 * Copy fanout CPUs to fe_effective_props without duplicates.
1189 		 */
1190 		for (i = 0; i < srs_cpu->mc_ncpus &&
1191 		    emrp->mrp_ncpus < MRP_NCPUS; i++) {
1192 			for (j = 0; j < emrp->mrp_ncpus; j++) {
1193 				if (emrp->mrp_cpu[j] == srs_cpu->mc_cpus[i])
1194 					break;
1195 			}
1196 			if (j == emrp->mrp_ncpus) {
1197 				emrp->mrp_cpu[emrp->mrp_ncpus++] =
1198 				    srs_cpu->mc_cpus[i];
1199 			}
1200 		}
1201 	}
1202 
1203 	mac_tx_cpu_init(flent, NULL, cpupart);
1204 }
1205 
1206 /*
1207  * DATAPATH SETUP ROUTINES
1208  * (setup SRS and set/update FANOUT, B/W and PRIORITY)
1209  */
1210 
1211 /*
1212  * mac_srs_fanout_list_alloc:
1213  *
1214  * The underlying device can expose upto MAX_RINGS_PER_GROUP worth of
1215  * rings to a client. In such a case, MAX_RINGS_PER_GROUP worth of
1216  * array space is needed to store Tx soft rings. Thus we allocate so
1217  * much array space for srs_tx_soft_rings.
1218  *
1219  * And when it is an aggr, again we allocate MAX_RINGS_PER_GROUP worth
1220  * of space to st_soft_rings. This array is used for quick access to
1221  * soft ring associated with a pseudo Tx ring based on the pseudo
1222  * ring's index (mr_index).
1223  */
1224 static void
1225 mac_srs_fanout_list_alloc(mac_soft_ring_set_t *mac_srs)
1226 {
1227 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
1228 
1229 	if (mac_srs->srs_type & SRST_TX) {
1230 		mac_srs->srs_tx_soft_rings = (mac_soft_ring_t **)
1231 		    kmem_zalloc(sizeof (mac_soft_ring_t *) *
1232 		    MAX_RINGS_PER_GROUP, KM_SLEEP);
1233 		if (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT) {
1234 			mac_srs_tx_t *tx = &mac_srs->srs_tx;
1235 
1236 			tx->st_soft_rings = (mac_soft_ring_t **)
1237 			    kmem_zalloc(sizeof (mac_soft_ring_t *) *
1238 			    MAX_RINGS_PER_GROUP, KM_SLEEP);
1239 		}
1240 	} else {
1241 		mac_srs->srs_tcp_soft_rings = (mac_soft_ring_t **)
1242 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1243 		    KM_SLEEP);
1244 		mac_srs->srs_tcp6_soft_rings = (mac_soft_ring_t **)
1245 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1246 		    KM_SLEEP);
1247 		mac_srs->srs_udp_soft_rings = (mac_soft_ring_t **)
1248 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1249 		    KM_SLEEP);
1250 		mac_srs->srs_udp6_soft_rings = (mac_soft_ring_t **)
1251 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1252 		    KM_SLEEP);
1253 		mac_srs->srs_oth_soft_rings = (mac_soft_ring_t **)
1254 		    kmem_zalloc(sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT,
1255 		    KM_SLEEP);
1256 	}
1257 }
1258 
1259 static void
1260 mac_srs_worker_bind(mac_soft_ring_set_t *mac_srs, processorid_t cpuid)
1261 {
1262 	cpu_t *cp;
1263 	boolean_t clear = B_FALSE;
1264 
1265 	ASSERT(MUTEX_HELD(&cpu_lock));
1266 
1267 	if (!mac_srs_thread_bind)
1268 		return;
1269 
1270 	cp = cpu_get(cpuid);
1271 	if (cp == NULL || !cpu_is_online(cp))
1272 		return;
1273 
1274 	mutex_enter(&mac_srs->srs_lock);
1275 	mac_srs->srs_state |= SRS_WORKER_BOUND;
1276 	if (mac_srs->srs_worker_cpuid != -1)
1277 		clear = B_TRUE;
1278 	mac_srs->srs_worker_cpuid = cpuid;
1279 	mutex_exit(&mac_srs->srs_lock);
1280 
1281 	if (clear)
1282 		thread_affinity_clear(mac_srs->srs_worker);
1283 
1284 	thread_affinity_set(mac_srs->srs_worker, cpuid);
1285 	DTRACE_PROBE1(worker__CPU, processorid_t, cpuid);
1286 }
1287 
1288 static void
1289 mac_srs_poll_bind(mac_soft_ring_set_t *mac_srs, processorid_t cpuid)
1290 {
1291 	cpu_t *cp;
1292 	boolean_t clear = B_FALSE;
1293 
1294 	ASSERT(MUTEX_HELD(&cpu_lock));
1295 
1296 	if (!mac_srs_thread_bind || mac_srs->srs_poll_thr == NULL)
1297 		return;
1298 
1299 	cp = cpu_get(cpuid);
1300 	if (cp == NULL || !cpu_is_online(cp))
1301 		return;
1302 
1303 	mutex_enter(&mac_srs->srs_lock);
1304 	mac_srs->srs_state |= SRS_POLL_BOUND;
1305 	if (mac_srs->srs_poll_cpuid != -1)
1306 		clear = B_TRUE;
1307 	mac_srs->srs_poll_cpuid = cpuid;
1308 	mutex_exit(&mac_srs->srs_lock);
1309 
1310 	if (clear)
1311 		thread_affinity_clear(mac_srs->srs_poll_thr);
1312 
1313 	thread_affinity_set(mac_srs->srs_poll_thr, cpuid);
1314 	DTRACE_PROBE1(poll__CPU, processorid_t, cpuid);
1315 }
1316 
1317 /*
1318  * Re-target interrupt to the passed CPU. If re-target is successful,
1319  * set mc_rx_intr_cpu to the re-targeted CPU. Otherwise set it to -1.
1320  */
1321 void
1322 mac_rx_srs_retarget_intr(mac_soft_ring_set_t *mac_srs, processorid_t cpuid)
1323 {
1324 	cpu_t *cp;
1325 	mac_ring_t *ring = mac_srs->srs_ring;
1326 	mac_intr_t *mintr = &ring->mr_info.mri_intr;
1327 	flow_entry_t *flent = mac_srs->srs_flent;
1328 	boolean_t primary = mac_is_primary_client(mac_srs->srs_mcip);
1329 
1330 	ASSERT(MUTEX_HELD(&cpu_lock));
1331 
1332 	/*
1333 	 * Don't re-target the interrupt for these cases:
1334 	 * 1) ring is NULL
1335 	 * 2) the interrupt is shared (mi_ddi_shared)
1336 	 * 3) ddi_handle is NULL and !primary
1337 	 * 4) primary, ddi_handle is NULL but fe_rx_srs_cnt > 2
1338 	 * Case 3 & 4 are because of mac_client_intr_cpu() routine.
1339 	 * This routine will re-target fixed interrupt for primary
1340 	 * mac client if the client has only one ring. In that
1341 	 * case, mc_rx_intr_cpu will already have the correct value.
1342 	 */
1343 	if (ring == NULL || mintr->mi_ddi_shared || cpuid == -1 ||
1344 	    (mintr->mi_ddi_handle == NULL && !primary) || (primary &&
1345 	    mintr->mi_ddi_handle == NULL && flent->fe_rx_srs_cnt > 2)) {
1346 		mac_srs->srs_cpu.mc_rx_intr_cpu = -1;
1347 		return;
1348 	}
1349 
1350 	if (mintr->mi_ddi_handle == NULL)
1351 		return;
1352 
1353 	cp = cpu_get(cpuid);
1354 	if (cp == NULL || !cpu_is_online(cp))
1355 		return;
1356 
1357 	/* Drop the cpu_lock as set_intr_affinity() holds it */
1358 	mutex_exit(&cpu_lock);
1359 	if (set_intr_affinity(mintr->mi_ddi_handle, cpuid) == DDI_SUCCESS)
1360 		mac_srs->srs_cpu.mc_rx_intr_cpu = cpuid;
1361 	else
1362 		mac_srs->srs_cpu.mc_rx_intr_cpu = -1;
1363 	mutex_enter(&cpu_lock);
1364 }
1365 
1366 /*
1367  * Re-target Tx interrupts
1368  */
1369 void
1370 mac_tx_srs_retarget_intr(mac_soft_ring_set_t *mac_srs)
1371 {
1372 	cpu_t *cp;
1373 	mac_ring_t *ring;
1374 	mac_intr_t *mintr;
1375 	mac_soft_ring_t *sringp;
1376 	mac_srs_tx_t *srs_tx;
1377 	mac_cpus_t *srs_cpu;
1378 	processorid_t cpuid;
1379 	int i;
1380 
1381 	ASSERT(MUTEX_HELD(&cpu_lock));
1382 
1383 	srs_cpu = &mac_srs->srs_cpu;
1384 	if (MAC_TX_SOFT_RINGS(mac_srs)) {
1385 		for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
1386 			sringp = mac_srs->srs_tx_soft_rings[i];
1387 			ring = (mac_ring_t *)sringp->s_ring_tx_arg2;
1388 			cpuid = srs_cpu->mc_tx_intr_cpu[i];
1389 			cp = cpu_get(cpuid);
1390 			if (cp == NULL || !cpu_is_online(cp) ||
1391 			    !MAC_RING_RETARGETABLE(ring)) {
1392 				srs_cpu->mc_tx_retargeted_cpu[i] = -1;
1393 				continue;
1394 			}
1395 			mintr = &ring->mr_info.mri_intr;
1396 			/*
1397 			 * Drop the cpu_lock as set_intr_affinity()
1398 			 * holds it
1399 			 */
1400 			mutex_exit(&cpu_lock);
1401 			if (set_intr_affinity(mintr->mi_ddi_handle,
1402 			    cpuid) == DDI_SUCCESS) {
1403 				srs_cpu->mc_tx_retargeted_cpu[i] = cpuid;
1404 			} else {
1405 				srs_cpu->mc_tx_retargeted_cpu[i] = -1;
1406 			}
1407 			mutex_enter(&cpu_lock);
1408 		}
1409 	} else {
1410 		cpuid = srs_cpu->mc_tx_intr_cpu[0];
1411 		cp = cpu_get(cpuid);
1412 		if (cp == NULL || !cpu_is_online(cp)) {
1413 			srs_cpu->mc_tx_retargeted_cpu[0] = -1;
1414 			return;
1415 		}
1416 		srs_tx = &mac_srs->srs_tx;
1417 		ring = (mac_ring_t *)srs_tx->st_arg2;
1418 		if (MAC_RING_RETARGETABLE(ring)) {
1419 			mintr = &ring->mr_info.mri_intr;
1420 			mutex_exit(&cpu_lock);
1421 			if ((set_intr_affinity(mintr->mi_ddi_handle,
1422 			    cpuid) == DDI_SUCCESS)) {
1423 				srs_cpu->mc_tx_retargeted_cpu[0] = cpuid;
1424 			} else {
1425 				srs_cpu->mc_tx_retargeted_cpu[0] = -1;
1426 			}
1427 			mutex_enter(&cpu_lock);
1428 		}
1429 	}
1430 }
1431 
1432 /*
1433  * When a CPU comes back online, bind the MAC kernel threads which
1434  * were previously bound to that CPU, and had to be unbound because
1435  * the CPU was going away.
1436  *
1437  * These functions are called with cpu_lock held and hence we can't
1438  * cv_wait to grab the mac perimeter. Since these functions walk the soft
1439  * ring list of an SRS without being in the perimeter, the list itself
1440  * is protected by the SRS lock.
1441  */
1442 static void
1443 mac_walk_srs_and_bind(int cpuid)
1444 {
1445 	mac_soft_ring_set_t *mac_srs;
1446 	mac_soft_ring_t *soft_ring;
1447 
1448 	rw_enter(&mac_srs_g_lock, RW_READER);
1449 
1450 	if ((mac_srs = mac_srs_g_list) == NULL)
1451 		goto done;
1452 
1453 	for (; mac_srs != NULL; mac_srs = mac_srs->srs_next) {
1454 		if (mac_srs->srs_worker_cpuid == -1 &&
1455 		    mac_srs->srs_worker_cpuid_save == cpuid) {
1456 			mac_srs->srs_worker_cpuid_save = -1;
1457 			mac_srs_worker_bind(mac_srs, cpuid);
1458 		}
1459 
1460 		if (!(mac_srs->srs_type & SRST_TX)) {
1461 			if (mac_srs->srs_poll_cpuid == -1 &&
1462 			    mac_srs->srs_poll_cpuid_save == cpuid) {
1463 				mac_srs->srs_poll_cpuid_save = -1;
1464 				mac_srs_poll_bind(mac_srs, cpuid);
1465 			}
1466 		}
1467 
1468 		/* Next tackle the soft rings associated with the srs */
1469 		mutex_enter(&mac_srs->srs_lock);
1470 		for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
1471 		    soft_ring = soft_ring->s_ring_next) {
1472 			if (soft_ring->s_ring_cpuid == -1 &&
1473 			    soft_ring->s_ring_cpuid_save == cpuid) {
1474 				soft_ring->s_ring_cpuid_save = -1;
1475 				(void) mac_soft_ring_bind(soft_ring, cpuid);
1476 			}
1477 		}
1478 		mutex_exit(&mac_srs->srs_lock);
1479 	}
1480 done:
1481 	rw_exit(&mac_srs_g_lock);
1482 }
1483 
1484 /*
1485  * Change the priority of the SRS's poll and worker thread. Additionally,
1486  * update the priority of the worker threads for the SRS's soft rings.
1487  * Need to modify any associated squeue threads.
1488  */
1489 void
1490 mac_update_srs_priority(mac_soft_ring_set_t *mac_srs, pri_t prival)
1491 {
1492 	mac_soft_ring_t		*ringp;
1493 
1494 	mac_srs->srs_pri = prival;
1495 	thread_lock(mac_srs->srs_worker);
1496 	(void) thread_change_pri(mac_srs->srs_worker, mac_srs->srs_pri, 0);
1497 	thread_unlock(mac_srs->srs_worker);
1498 	if (mac_srs->srs_poll_thr != NULL) {
1499 		thread_lock(mac_srs->srs_poll_thr);
1500 		(void) thread_change_pri(mac_srs->srs_poll_thr,
1501 		    mac_srs->srs_pri, 0);
1502 		thread_unlock(mac_srs->srs_poll_thr);
1503 	}
1504 	if ((ringp = mac_srs->srs_soft_ring_head) == NULL)
1505 		return;
1506 	while (ringp != mac_srs->srs_soft_ring_tail) {
1507 		thread_lock(ringp->s_ring_worker);
1508 		(void) thread_change_pri(ringp->s_ring_worker,
1509 		    mac_srs->srs_pri, 0);
1510 		thread_unlock(ringp->s_ring_worker);
1511 		ringp = ringp->s_ring_next;
1512 	}
1513 	ASSERT(ringp == mac_srs->srs_soft_ring_tail);
1514 	thread_lock(ringp->s_ring_worker);
1515 	(void) thread_change_pri(ringp->s_ring_worker, mac_srs->srs_pri, 0);
1516 	thread_unlock(ringp->s_ring_worker);
1517 }
1518 
1519 /*
1520  * Update a bandwidth control to reflect its new state, clearing any existing
1521  * usage if moving from disabled to active.
1522  */
1523 static void
1524 mac_bw_ctl_set_state(mac_bw_ctl_t *bw, const boolean_t do_enable,
1525     const mac_resource_props_t *mrp)
1526 {
1527 	VERIFY(MUTEX_HELD(&bw->mac_bw_lock));
1528 	if (do_enable) {
1529 		const boolean_t was_disabled = !mac_bw_ctl_is_enabled(bw);
1530 
1531 		/* Set/Modify bandwidth limit */
1532 		bw->mac_bw_state |= BW_ENABLED;
1533 		bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
1534 		/*
1535 		 * Give twice the queuing capability before
1536 		 * dropping packets. The unit is bytes/tick.
1537 		 */
1538 		bw->mac_bw_drop_threshold = bw->mac_bw_limit << 1;
1539 
1540 		/*
1541 		 * Don't clear any expended bytes if we are moving between two
1542 		 * bandwidth limits, but attempt to clear enforcement status if
1543 		 * we've increased the limit.
1544 		 */
1545 		if (was_disabled) {
1546 			bw->mac_bw_state &= ~BW_ENFORCED;
1547 			bw->mac_bw_curr_time = gethrtime();
1548 			bw->mac_bw_used = 0;
1549 			bw->mac_bw_sz = 0;
1550 		} else if (bw->mac_bw_used < bw->mac_bw_limit) {
1551 			bw->mac_bw_state &= ~BW_ENFORCED;
1552 		}
1553 	} else {
1554 		/*
1555 		 * As there is no bandwidth limit, there is nothing to enforce.
1556 		 */
1557 		bw->mac_bw_state &= ~(BW_ENABLED | BW_ENFORCED);
1558 	}
1559 }
1560 
1561 /*
1562  * Chooses the correct `mac_srs_drain_proc_t` for `srs` dependent on its type
1563  * and whether it has a bandwidth limit configured. This allows for the SRS
1564  * drain to perform logic for each case unconditionally.
1565  *
1566  * If this method is called on an active SRS, this must be done under either
1567  * quiescence or srs_lock.
1568  */
1569 static void
1570 mac_srs_update_drain_proc(mac_soft_ring_set_t *srs)
1571 {
1572 	mac_srs_drain_proc_t drain_fn = NULL;
1573 	if ((srs->srs_type & SRST_TX) != 0) {
1574 		drain_fn = mac_tx_srs_drain;
1575 	} else if (mac_srs_is_bw_controlled(srs)) {
1576 		drain_fn = mac_rx_srs_drain_bw;
1577 	} else {
1578 		drain_fn = mac_rx_srs_drain;
1579 	}
1580 
1581 	VERIFY3P(drain_fn, !=, NULL);
1582 
1583 	srs->srs_drain_func = drain_fn;
1584 }
1585 
1586 /*
1587  * Change a Tx SRS's state to reflect whether it is bandwidth controlled.
1588  */
1589 static void
1590 mac_tx_srs_update_bwlimit_state(mac_soft_ring_set_t *srs,
1591     const boolean_t is_enabled)
1592 {
1593 	uint32_t		ring_info = 0;
1594 	mac_srs_tx_t		*srs_tx = &srs->srs_tx;
1595 	mac_client_impl_t	*mcip = srs->srs_mcip;
1596 
1597 	VERIFY3U(srs->srs_type & SRST_TX, !=, 0);
1598 
1599 	/*
1600 	 * We need to quiesce/restart the client here because mac_tx() and
1601 	 * srs->srs_tx.st_func do not hold srs->srs_lock while accessing
1602 	 * st_mode and related fields, which are modified by the code below.
1603 	 */
1604 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
1605 
1606 	mutex_enter(&srs->srs_lock);
1607 
1608 	mac_tx_srs_mode_t tx_mode = srs_tx->st_mode;
1609 	if (is_enabled) {
1610 		if (tx_mode != SRS_TX_BW && tx_mode != SRS_TX_BW_FANOUT &&
1611 		    tx_mode != SRS_TX_BW_AGGR) {
1612 			if (tx_mode == SRS_TX_SERIALIZE ||
1613 			    tx_mode == SRS_TX_DEFAULT) {
1614 				srs_tx->st_mode = SRS_TX_BW;
1615 			} else if (tx_mode == SRS_TX_FANOUT) {
1616 				srs_tx->st_mode = SRS_TX_BW_FANOUT;
1617 			} else if (tx_mode == SRS_TX_AGGR) {
1618 				srs_tx->st_mode = SRS_TX_BW_AGGR;
1619 			} else {
1620 				panic("Unhandled BW->non-BW mode change: %d",
1621 				    tx_mode);
1622 			}
1623 		}
1624 
1625 		srs->srs_type |= SRST_BW_CONTROL;
1626 	} else {
1627 		if (tx_mode == SRS_TX_BW) {
1628 			if (srs_tx->st_arg2 != NULL) {
1629 				mac_ring_handle_t mrh =
1630 				    (mac_ring_handle_t)srs_tx->st_arg2;
1631 				ring_info = mac_hwring_getinfo(mrh);
1632 			}
1633 			if (mac_tx_serialize ||
1634 			    (ring_info & MAC_RING_TX_SERIALIZE)) {
1635 				srs_tx->st_mode = SRS_TX_SERIALIZE;
1636 			} else {
1637 				srs_tx->st_mode = SRS_TX_DEFAULT;
1638 			}
1639 		} else if (tx_mode == SRS_TX_BW_FANOUT) {
1640 			srs_tx->st_mode = SRS_TX_FANOUT;
1641 		} else if (tx_mode == SRS_TX_BW_AGGR) {
1642 			srs_tx->st_mode = SRS_TX_AGGR;
1643 		}
1644 
1645 		srs->srs_type &= ~SRST_BW_CONTROL;
1646 	}
1647 
1648 	srs_tx->st_func = mac_tx_get_func(srs_tx->st_mode);
1649 	mutex_exit(&srs->srs_lock);
1650 
1651 	mac_tx_client_restart((mac_client_handle_t)mcip);
1652 }
1653 
1654 /*
1655  * Change an Rx SRS's state to reflect whether it is bandwidth controlled.
1656  */
1657 static void
1658 mac_srs_update_rx_bwlimit_state(mac_soft_ring_set_t *srs,
1659     const boolean_t is_enabled)
1660 {
1661 	VERIFY3U(srs->srs_type & SRST_TX, ==, 0);
1662 
1663 	mutex_enter(&srs->srs_lock);
1664 
1665 	if (is_enabled) {
1666 		srs->srs_type |= SRST_BW_CONTROL;
1667 	} else {
1668 		srs->srs_type &= ~SRST_BW_CONTROL;
1669 	}
1670 
1671 	mac_srs_update_drain_proc(srs);
1672 
1673 	mutex_exit(&srs->srs_lock);
1674 }
1675 
1676 /*
1677  * Update the Tx and Rx bandwidth control on a target flent, then reconfigure
1678  * any downstream SRSes to use the correct drain/process methods to use or skip
1679  * bandwidth checking as required.
1680  */
1681 void
1682 mac_srs_update_bwlimit(flow_entry_t *flent, mac_resource_props_t *mrp)
1683 {
1684 	const boolean_t enable = mrp->mrp_maxbw != MRP_MAXBW_RESETVAL;
1685 
1686 	mutex_enter(&flent->fe_rx_bw.mac_bw_lock);
1687 	mac_bw_ctl_set_state(&flent->fe_rx_bw, enable, mrp);
1688 	mutex_exit(&flent->fe_rx_bw.mac_bw_lock);
1689 	for (uint32_t i = 0; i < flent->fe_rx_srs_cnt; i++) {
1690 		mac_srs_update_rx_bwlimit_state(flent->fe_rx_srs[i], enable);
1691 	}
1692 
1693 	mutex_enter(&flent->fe_tx_bw.mac_bw_lock);
1694 	mac_bw_ctl_set_state(&flent->fe_tx_bw, enable, mrp);
1695 	mutex_exit(&flent->fe_tx_bw.mac_bw_lock);
1696 	mac_tx_srs_update_bwlimit_state(flent->fe_tx_srs, enable);
1697 }
1698 
1699 /*
1700  * When the first sub-flow is added to a link, we disable polling on the
1701  * link and also modify the entry point to mac_rx_srs_subflow_process().
1702  * (polling is disabled because with the subflow added, accounting
1703  * for polling needs additional logic, it is assumed that when a subflow is
1704  * added, we can take some hit as a result of disabling polling rather than
1705  * adding more complexity - if this becomes a perf. issue we need to
1706  * re-rvaluate this logic).  When the last subflow is removed, we turn back
1707  * polling and also reset the entry point to mac_rx_srs_process().
1708  *
1709  * In the future if there are multiple SRS, we can simply
1710  * take one and give it to the flow rather than disabling polling and
1711  * resetting the entry point.
1712  */
1713 void
1714 mac_client_update_classifier(mac_client_impl_t *mcip, boolean_t enable)
1715 {
1716 	flow_entry_t		*flent = mcip->mci_flent;
1717 	int			i;
1718 	mac_impl_t		*mip = mcip->mci_mip;
1719 	mac_rx_func_t		rx_func;
1720 	uint_t			rx_srs_cnt;
1721 	boolean_t		enable_classifier;
1722 
1723 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1724 
1725 	enable_classifier = !FLOW_TAB_EMPTY(mcip->mci_subflow_tab) && enable;
1726 
1727 	rx_func = enable_classifier ? mac_rx_srs_subflow_process :
1728 	    mac_rx_srs_process;
1729 
1730 	/* Tell mac_srs_poll_state_change to disable polling if necessary */
1731 	if (mip->mi_state_flags & MIS_POLL_DISABLE)
1732 		enable_classifier = B_TRUE;
1733 
1734 	/*
1735 	 * If receive function has already been configured correctly for
1736 	 * current subflow configuration, do nothing.
1737 	 */
1738 	if (flent->fe_cb_fn == (flow_fn_t)rx_func)
1739 		return;
1740 
1741 	rx_srs_cnt = flent->fe_rx_srs_cnt;
1742 	for (i = 0; i < rx_srs_cnt; i++) {
1743 		ASSERT(flent->fe_rx_srs[i] != NULL);
1744 		mac_srs_poll_state_change(flent->fe_rx_srs[i],
1745 		    enable_classifier, rx_func);
1746 	}
1747 
1748 	/*
1749 	 * Change the S/W classifier so that we can land in the
1750 	 * correct processing function with correct argument.
1751 	 * If all subflows have been removed we can revert to
1752 	 * mac_rx_srs_process(), else we need mac_rx_srs_subflow_process().
1753 	 */
1754 	mutex_enter(&flent->fe_lock);
1755 	flent->fe_cb_fn = (flow_fn_t)rx_func;
1756 	flent->fe_cb_arg1 = (void *)mip;
1757 	flent->fe_cb_arg2 = flent->fe_rx_srs[0];
1758 	mutex_exit(&flent->fe_lock);
1759 }
1760 
1761 static void
1762 mac_srs_update_fanout_list(mac_soft_ring_set_t *mac_srs)
1763 {
1764 	int tcp_count = 0, tcp6_count = 0, udp_count = 0, udp6_count = 0,
1765 	    oth_count = 0, tx_count = 0;
1766 
1767 	mac_soft_ring_t *softring;
1768 
1769 	softring = mac_srs->srs_soft_ring_head;
1770 	if (softring == NULL) {
1771 		ASSERT(mac_srs->srs_soft_ring_count == 0);
1772 		mac_srs->srs_tcp_ring_count = 0;
1773 		mac_srs->srs_udp_ring_count = 0;
1774 		mac_srs->srs_tcp6_ring_count = 0;
1775 		mac_srs->srs_udp6_ring_count = 0;
1776 		mac_srs->srs_oth_ring_count = 0;
1777 		mac_srs->srs_tx_ring_count = 0;
1778 
1779 		/*
1780 		 * `SRST_NO_SOFT_RINGS` is a static property of Rx SRSes, and
1781 		 * determines their processing model. Adjust this only on Tx
1782 		 * SRSes, where its meaning is something of a vanity flag.
1783 		 */
1784 		if ((mac_srs->srs_type & SRST_TX) != 0) {
1785 			mac_srs->srs_type |= SRST_NO_SOFT_RINGS;
1786 		}
1787 
1788 		return;
1789 	}
1790 
1791 	if ((mac_srs->srs_type & SRST_TX) != 0) {
1792 		mac_srs->srs_type &= ~SRST_NO_SOFT_RINGS;
1793 	}
1794 
1795 	while (softring != NULL) {
1796 		if (softring->s_ring_state & ST_RING_TCP) {
1797 			mac_srs->srs_tcp_soft_rings[tcp_count++] = softring;
1798 		} else if (softring->s_ring_state & ST_RING_TCP6) {
1799 			mac_srs->srs_tcp6_soft_rings[tcp6_count++] = softring;
1800 		} else if (softring->s_ring_state & ST_RING_UDP) {
1801 			mac_srs->srs_udp_soft_rings[udp_count++] = softring;
1802 		} else if (softring->s_ring_state & ST_RING_UDP6) {
1803 			mac_srs->srs_udp6_soft_rings[udp6_count++] = softring;
1804 		} else if (softring->s_ring_state & ST_RING_OTH) {
1805 			mac_srs->srs_oth_soft_rings[oth_count++] = softring;
1806 		} else {
1807 			ASSERT(softring->s_ring_state & ST_RING_TX);
1808 			mac_srs->srs_tx_soft_rings[tx_count++] = softring;
1809 		}
1810 		softring = softring->s_ring_next;
1811 	}
1812 
1813 	ASSERT(mac_srs->srs_soft_ring_count == (tcp_count + tcp6_count +
1814 	    udp_count + udp6_count + oth_count + tx_count));
1815 	mac_srs->srs_tcp_ring_count = tcp_count;
1816 	mac_srs->srs_tcp6_ring_count = tcp6_count;
1817 	mac_srs->srs_udp_ring_count = udp_count;
1818 	mac_srs->srs_udp6_ring_count = udp6_count;
1819 	mac_srs->srs_oth_ring_count = oth_count;
1820 	mac_srs->srs_tx_ring_count = tx_count;
1821 }
1822 
1823 static void
1824 mac_srs_create_proto_softrings(int id, pri_t pri, mac_client_impl_t *mcip,
1825     mac_soft_ring_set_t *mac_srs, processorid_t cpuid, mac_direct_rx_t rx_func,
1826     void *x_arg1, boolean_t set_bypass)
1827 {
1828 	mac_soft_ring_t	*softring;
1829 
1830 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1831 	    ST_RING_TCP, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1832 
1833 	/*
1834 	 * TCP and UDP support DLS bypass. In addition TCP
1835 	 * squeue can also poll their corresponding soft rings.
1836 	 */
1837 	if (set_bypass && mcip->mci_direct_rx.mdrx_v4 != NULL &&
1838 	    (mcip->mci_rcb4.mrc_arg != NULL)) {
1839 		/*
1840 		 * Make a call in IP to get a TCP squeue assigned to
1841 		 * this softring to maintain full CPU locality through
1842 		 * the stack and allow the squeue to be able to poll
1843 		 * the softring so the flow control can be pushed
1844 		 * all the way to H/W.
1845 		 */
1846 		mac_soft_ring_poll_enable(softring, mcip->mci_direct_rx.mdrx_v4,
1847 		    mcip->mci_direct_rx.mdrx_arg_v4, &mcip->mci_rcb4, pri);
1848 	}
1849 
1850 	/*
1851 	 * Non-TCP protocols don't support squeues. Hence we
1852 	 * don't make any ring addition callbacks for non-TCP
1853 	 * rings. Now create the UDP softring and allow it to
1854 	 * bypass the DLS layer.
1855 	 */
1856 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1857 	    ST_RING_UDP, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1858 
1859 	if (set_bypass && mcip->mci_direct_rx.mdrx_v4 != NULL) {
1860 		mac_soft_ring_dls_bypass_enable(softring,
1861 		    mcip->mci_direct_rx.mdrx_v4,
1862 		    mcip->mci_direct_rx.mdrx_arg_v4);
1863 	}
1864 
1865 	/* TCP for IPv6. */
1866 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1867 	    ST_RING_TCP6, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1868 
1869 	if (set_bypass && mcip->mci_direct_rx.mdrx_v6 != NULL &&
1870 	    (mcip->mci_rcb6.mrc_arg != NULL)) {
1871 		mac_soft_ring_poll_enable(softring, mcip->mci_direct_rx.mdrx_v6,
1872 		    mcip->mci_direct_rx.mdrx_arg_v6, &mcip->mci_rcb6, pri);
1873 	}
1874 
1875 	/* UDP for IPv6. */
1876 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1877 	    ST_RING_UDP6, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1878 	softring->s_ring_rx_arg2 = NULL;
1879 
1880 	if (set_bypass && mcip->mci_direct_rx.mdrx_v6 != NULL) {
1881 		mac_soft_ring_dls_bypass_enable(softring,
1882 		    mcip->mci_direct_rx.mdrx_v6,
1883 		    mcip->mci_direct_rx.mdrx_arg_v6);
1884 	}
1885 
1886 	/* Create the Oth softrings which has to go through the DLS. */
1887 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1888 	    ST_RING_OTH, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1889 }
1890 
1891 /*
1892  * This routine associates a CPU or a set of CPU to process incoming
1893  * traffic from a mac client. If multiple CPUs are specified, then
1894  * so many soft rings are created with each soft ring worker thread
1895  * bound to a CPU in the set. Each soft ring in turn will be
1896  * associated with an squeue and the squeue will be moved to the
1897  * same CPU as that of the soft ring's.
1898  */
1899 static void
1900 mac_srs_fanout_modify(mac_client_impl_t *mcip, mac_direct_rx_t rx_func,
1901     void *x_arg1, mac_soft_ring_set_t *mac_rx_srs,
1902     mac_soft_ring_set_t *mac_tx_srs)
1903 {
1904 	mac_soft_ring_t *softring;
1905 	processorid_t cpuid = -1;
1906 	int i, srings_present, new_fanout_cnt;
1907 	mac_cpus_t *srs_cpu;
1908 
1909 	/* fanout state is REINIT. Set it back to INIT */
1910 	ASSERT(mac_rx_srs->srs_fanout_state == SRS_FANOUT_REINIT);
1911 	mac_rx_srs->srs_fanout_state = SRS_FANOUT_INIT;
1912 
1913 	/* how many are present right now */
1914 	srings_present = mac_rx_srs->srs_tcp_ring_count;
1915 	/* new request */
1916 	srs_cpu = &mac_rx_srs->srs_cpu;
1917 	new_fanout_cnt = srs_cpu->mc_rx_fanout_cnt;
1918 
1919 	if (new_fanout_cnt > srings_present) {
1920 		/* soft rings increased */
1921 		mutex_enter(&mac_rx_srs->srs_lock);
1922 		mac_rx_srs->srs_type |= SRST_FANOUT_SRC_IP;
1923 		mutex_exit(&mac_rx_srs->srs_lock);
1924 
1925 		for (i = mac_rx_srs->srs_tcp_ring_count;
1926 		    i < new_fanout_cnt; i++) {
1927 			/*
1928 			 * Create the protocol softrings and set the
1929 			 * DLS bypass where possible.
1930 			 */
1931 			mac_srs_create_proto_softrings(i, mac_rx_srs->srs_pri,
1932 			    mcip, mac_rx_srs, cpuid, rx_func, x_arg1, B_TRUE);
1933 		}
1934 		mac_srs_update_fanout_list(mac_rx_srs);
1935 	} else if (new_fanout_cnt < srings_present) {
1936 		/* soft rings decreased */
1937 		if (new_fanout_cnt == 1) {
1938 			mutex_enter(&mac_rx_srs->srs_lock);
1939 			mac_rx_srs->srs_type &= ~SRST_FANOUT_SRC_IP;
1940 			ASSERT(mac_rx_srs->srs_type & SRST_FANOUT_PROTO);
1941 			mutex_exit(&mac_rx_srs->srs_lock);
1942 		}
1943 		/* Get rid of extra soft rings */
1944 		for (i = new_fanout_cnt;
1945 		    i < mac_rx_srs->srs_tcp_ring_count; i++) {
1946 			softring = mac_rx_srs->srs_tcp_soft_rings[i];
1947 			if (softring->s_ring_rx_arg2 != NULL) {
1948 				mcip->mci_rcb4.mrc_remove(
1949 				    mcip->mci_rcb4.mrc_arg,
1950 				    softring->s_ring_rx_arg2);
1951 			}
1952 			softring = mac_rx_srs->srs_tcp6_soft_rings[i];
1953 			if (softring->s_ring_rx_arg2 != NULL) {
1954 				mcip->mci_rcb6.mrc_remove(
1955 				    mcip->mci_rcb6.mrc_arg,
1956 				    softring->s_ring_rx_arg2);
1957 			}
1958 			mac_soft_ring_remove(mac_rx_srs,
1959 			    mac_rx_srs->srs_tcp_soft_rings[i]);
1960 			mac_soft_ring_remove(mac_rx_srs,
1961 			    mac_rx_srs->srs_tcp6_soft_rings[i]);
1962 			mac_soft_ring_remove(mac_rx_srs,
1963 			    mac_rx_srs->srs_udp_soft_rings[i]);
1964 			mac_soft_ring_remove(mac_rx_srs,
1965 			    mac_rx_srs->srs_udp6_soft_rings[i]);
1966 			mac_soft_ring_remove(mac_rx_srs,
1967 			    mac_rx_srs->srs_oth_soft_rings[i]);
1968 		}
1969 		mac_srs_update_fanout_list(mac_rx_srs);
1970 	}
1971 
1972 	ASSERT(new_fanout_cnt == mac_rx_srs->srs_tcp_ring_count);
1973 	mutex_enter(&cpu_lock);
1974 	for (i = 0; i < mac_rx_srs->srs_tcp_ring_count; i++) {
1975 		cpuid = srs_cpu->mc_rx_fanout_cpus[i];
1976 		(void) mac_soft_ring_bind(mac_rx_srs->srs_udp_soft_rings[i],
1977 		    cpuid);
1978 		(void) mac_soft_ring_bind(mac_rx_srs->srs_udp6_soft_rings[i],
1979 		    cpuid);
1980 		(void) mac_soft_ring_bind(mac_rx_srs->srs_oth_soft_rings[i],
1981 		    cpuid);
1982 		(void) mac_soft_ring_bind(mac_rx_srs->srs_tcp_soft_rings[i],
1983 		    cpuid);
1984 		(void) mac_soft_ring_bind(mac_rx_srs->srs_tcp6_soft_rings[i],
1985 		    cpuid);
1986 		softring = mac_rx_srs->srs_tcp_soft_rings[i];
1987 		if (softring->s_ring_rx_arg2 != NULL) {
1988 			mcip->mci_rcb4.mrc_bind(mcip->mci_rcb4.mrc_arg,
1989 			    softring->s_ring_rx_arg2, cpuid);
1990 		}
1991 		softring = mac_rx_srs->srs_tcp6_soft_rings[i];
1992 		if (softring->s_ring_rx_arg2 != NULL) {
1993 			mcip->mci_rcb6.mrc_bind(mcip->mci_rcb6.mrc_arg,
1994 			    softring->s_ring_rx_arg2, cpuid);
1995 		}
1996 	}
1997 
1998 	mac_srs_worker_bind(mac_rx_srs, srs_cpu->mc_rx_workerid);
1999 	mac_srs_poll_bind(mac_rx_srs, srs_cpu->mc_rx_pollid);
2000 	mac_rx_srs_retarget_intr(mac_rx_srs, srs_cpu->mc_rx_intr_cpu);
2001 	/*
2002 	 * Bind Tx srs and soft ring threads too. Let's bind tx
2003 	 * srs to the last cpu in mrp list.
2004 	 */
2005 	if (mac_tx_srs != NULL) {
2006 		BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp);
2007 		mac_tx_srs_retarget_intr(mac_tx_srs);
2008 	}
2009 	mutex_exit(&cpu_lock);
2010 }
2011 
2012 /*
2013  * Bind SRS threads and soft rings to CPUs/create fanout list.
2014  */
2015 void
2016 mac_srs_fanout_init(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
2017     mac_direct_rx_t rx_func, void *x_arg1, mac_soft_ring_set_t *mac_rx_srs,
2018     mac_soft_ring_set_t *mac_tx_srs, cpupart_t *cpupart)
2019 {
2020 	int		i;
2021 	processorid_t	cpuid;
2022 	int soft_ring_cnt;
2023 	mac_cpus_t *srs_cpu = &mac_rx_srs->srs_cpu;
2024 
2025 	/*
2026 	 * Remove the no soft ring flag and we will adjust it
2027 	 * appropriately further down.
2028 	 */
2029 	mutex_enter(&mac_rx_srs->srs_lock);
2030 	mac_rx_srs->srs_type &= ~SRST_NO_SOFT_RINGS;
2031 	mutex_exit(&mac_rx_srs->srs_lock);
2032 
2033 	ASSERT(mac_rx_srs->srs_soft_ring_head == NULL);
2034 	ASSERT(mac_rx_srs->srs_fanout_state == SRS_FANOUT_UNINIT);
2035 	mac_rx_srs->srs_fanout_state = SRS_FANOUT_INIT;
2036 	/*
2037 	 * Ring count can be 0 if no fanout is required and no cpu
2038 	 * were specified. Leave the SRS worker and poll thread
2039 	 * unbound
2040 	 */
2041 	ASSERT(mrp != NULL);
2042 	soft_ring_cnt = srs_cpu->mc_rx_fanout_cnt;
2043 
2044 	/* Step 1: bind cpu contains cpu list where threads need to bind */
2045 	if (soft_ring_cnt > 0) {
2046 		mutex_enter(&cpu_lock);
2047 		for (i = 0; i < soft_ring_cnt; i++) {
2048 			cpuid = srs_cpu->mc_rx_fanout_cpus[i];
2049 			/* Create the protocol softrings */
2050 			mac_srs_create_proto_softrings(i, mac_rx_srs->srs_pri,
2051 			    mcip, mac_rx_srs, cpuid, rx_func, x_arg1, B_FALSE);
2052 		}
2053 		mac_srs_worker_bind(mac_rx_srs, srs_cpu->mc_rx_workerid);
2054 		mac_srs_poll_bind(mac_rx_srs, srs_cpu->mc_rx_pollid);
2055 		mac_rx_srs_retarget_intr(mac_rx_srs, srs_cpu->mc_rx_intr_cpu);
2056 		/*
2057 		 * Bind Tx srs and soft ring threads too.
2058 		 * Let's bind tx srs to the last cpu in
2059 		 * mrp list.
2060 		 */
2061 		if (mac_tx_srs == NULL) {
2062 			mutex_exit(&cpu_lock);
2063 			goto alldone;
2064 		}
2065 
2066 		BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp);
2067 		mac_tx_srs_retarget_intr(mac_tx_srs);
2068 		mutex_exit(&cpu_lock);
2069 	} else {
2070 		mutex_enter(&cpu_lock);
2071 		/*
2072 		 * For a subflow, mrp_workerid and mrp_pollid
2073 		 * is not set.
2074 		 */
2075 		mac_srs_worker_bind(mac_rx_srs, mrp->mrp_rx_workerid);
2076 		mac_srs_poll_bind(mac_rx_srs, mrp->mrp_rx_pollid);
2077 		mutex_exit(&cpu_lock);
2078 		goto no_softrings;
2079 	}
2080 
2081 alldone:
2082 	if (soft_ring_cnt > 1)
2083 		mac_rx_srs->srs_type |= SRST_FANOUT_SRC_IP;
2084 	mac_srs_update_fanout_list(mac_rx_srs);
2085 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_FALSE);
2086 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_TRUE);
2087 	return;
2088 
2089 no_softrings:
2090 	if (mac_rx_srs->srs_type & SRST_FANOUT_PROTO) {
2091 		mutex_enter(&cpu_lock);
2092 		cpuid = mac_next_bind_cpu(cpupart);
2093 		/* Create the protocol softrings */
2094 		mac_srs_create_proto_softrings(0, mac_rx_srs->srs_pri, mcip,
2095 		    mac_rx_srs, cpuid, rx_func, x_arg1, B_FALSE);
2096 		mutex_exit(&cpu_lock);
2097 	} else {
2098 		/*
2099 		 * This is the case when there is no fanout which is
2100 		 * true for subflows.
2101 		 */
2102 		mac_rx_srs->srs_type |= SRST_NO_SOFT_RINGS;
2103 	}
2104 	mac_srs_update_fanout_list(mac_rx_srs);
2105 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_FALSE);
2106 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_TRUE);
2107 }
2108 
2109 /*
2110  * Calls mac_srs_fanout_init() or modify() depending upon whether
2111  * the SRS is getting initialized or re-initialized.
2112  */
2113 void
2114 mac_fanout_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2115     mac_resource_props_t *mrp, mac_direct_rx_t rx_func, void *x_arg1,
2116     cpupart_t *cpupart)
2117 {
2118 	mac_soft_ring_set_t *mac_rx_srs, *mac_tx_srs;
2119 	int i, rx_srs_cnt;
2120 
2121 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2122 
2123 	/*
2124 	 * Aggr ports do not have SRSes. This function should never be
2125 	 * called on an aggr port.
2126 	 */
2127 	ASSERT3U((mcip->mci_state_flags & MCIS_IS_AGGR_PORT), ==, 0);
2128 	mac_rx_srs = flent->fe_rx_srs[0];
2129 
2130 	/*
2131 	 * Set up the fanout on the tx side only once, with the
2132 	 * first rx SRS. The CPU binding, fanout, and bandwidth
2133 	 * criteria are common to both RX and TX, so
2134 	 * initializing them along side avoids redundant code.
2135 	 */
2136 	mac_tx_srs = flent->fe_tx_srs;
2137 	rx_srs_cnt = flent->fe_rx_srs_cnt;
2138 
2139 	/* No fanout for subflows */
2140 	if (flent->fe_type & FLOW_USER) {
2141 		mac_srs_fanout_init(mcip, mrp, rx_func,
2142 		    x_arg1, mac_rx_srs, mac_tx_srs, cpupart);
2143 		return;
2144 	}
2145 
2146 	if (mrp->mrp_mask & MRP_CPUS_USERSPEC)
2147 		mac_flow_user_cpu_init(flent, mrp);
2148 	else
2149 		mac_flow_cpu_init(flent, cpupart);
2150 
2151 	mrp->mrp_rx_fanout_cnt = mac_rx_srs->srs_cpu.mc_rx_fanout_cnt;
2152 
2153 	/*
2154 	 * Set up fanout for both SW (0th SRS) and HW classified
2155 	 * SRS (the rest of Rx SRSs in flent).
2156 	 */
2157 	for (i = 0; i < rx_srs_cnt; i++) {
2158 		mac_rx_srs = flent->fe_rx_srs[i];
2159 		if (i != 0)
2160 			mac_tx_srs = NULL;
2161 		switch (mac_rx_srs->srs_fanout_state) {
2162 		case SRS_FANOUT_UNINIT:
2163 			mac_srs_fanout_init(mcip, mrp, rx_func, x_arg1,
2164 			    mac_rx_srs, mac_tx_srs, cpupart);
2165 			break;
2166 		case SRS_FANOUT_INIT:
2167 			break;
2168 		case SRS_FANOUT_REINIT:
2169 			mac_rx_srs_quiesce(mac_rx_srs, SRS_QUIESCE);
2170 			mac_srs_fanout_modify(mcip, rx_func, x_arg1, mac_rx_srs,
2171 			    mac_tx_srs);
2172 			mac_rx_srs_restart(mac_rx_srs);
2173 			break;
2174 		default:
2175 			VERIFY(mac_rx_srs->srs_fanout_state <=
2176 			    SRS_FANOUT_REINIT);
2177 			break;
2178 		}
2179 	}
2180 }
2181 
2182 /*
2183  * Create a mac_soft_ring_set_t (SRS). If soft_ring_fanout_type is
2184  * SRST_TX, an SRS for Tx side is created. Otherwise an SRS for Rx side
2185  * processing is created.
2186  *
2187  * Details on Rx SRS:
2188  * Create a SRS and also add the necessary soft rings for TCP and
2189  * non-TCP based on fanout type and count specified.
2190  *
2191  * mac_soft_ring_fanout, mac_srs_fanout_modify (?),
2192  * mac_soft_ring_stop_workers, mac_soft_ring_set_destroy, etc need
2193  * to be heavily modified.
2194  *
2195  * mi_soft_ring_list_size, mi_soft_ring_size, etc need to disappear.
2196  */
2197 static mac_soft_ring_set_t *
2198 mac_srs_create(mac_client_impl_t *mcip, flow_entry_t *flent,
2199     const mac_soft_ring_set_type_t srs_type, mac_direct_rx_t rx_func,
2200     mac_ring_t *ring)
2201 {
2202 	mac_soft_ring_set_t	*mac_srs;
2203 	mac_srs_rx_t		*srs_rx;
2204 	mac_srs_tx_t		*srs_tx;
2205 	mac_resource_props_t	*mrp;
2206 	const boolean_t		is_tx_srs = ((srs_type & SRST_TX) != 0);
2207 
2208 	mac_srs = kmem_cache_alloc(mac_srs_cache, KM_SLEEP);
2209 	bzero(mac_srs, sizeof (mac_soft_ring_set_t));
2210 	srs_rx = &mac_srs->srs_rx;
2211 	srs_tx = &mac_srs->srs_tx;
2212 
2213 	mutex_enter(&flent->fe_lock);
2214 
2215 	/*
2216 	 * Get the bandwidth control structure from the flent. Get
2217 	 * rid of any residual values in the control structure for
2218 	 * the tx bw struct and also for the rx, if the rx srs is
2219 	 * the 1st one being brought up (the rx bw ctl struct may
2220 	 * be shared by multiple SRSs)
2221 	 */
2222 	mac_bw_ctl_t *my_bw = is_tx_srs ? &flent->fe_tx_bw : &flent->fe_rx_bw;
2223 	mac_srs->srs_bw = my_bw;
2224 
2225 	if (is_tx_srs) {
2226 		bzero(my_bw, sizeof (*my_bw));
2227 		flent->fe_tx_srs = mac_srs;
2228 	} else {
2229 		/* First rx SRS, clear the bw structure */
2230 		if (flent->fe_rx_srs_cnt == 0)
2231 			bzero(my_bw, sizeof (*my_bw));
2232 
2233 		/*
2234 		 * It is better to panic here rather than just assert because
2235 		 * on a non-debug kernel we might end up courrupting memory
2236 		 * and making it difficult to debug.
2237 		 */
2238 		if (flent->fe_rx_srs_cnt >= MAX_RINGS_PER_GROUP) {
2239 			panic("Array Overrun detected due to MAC client %p "
2240 			    " having more rings than %d", (void *)mcip,
2241 			    MAX_RINGS_PER_GROUP);
2242 		}
2243 		flent->fe_rx_srs[flent->fe_rx_srs_cnt] = mac_srs;
2244 		flent->fe_rx_srs_cnt++;
2245 	}
2246 	mac_srs->srs_flent = flent;
2247 	mutex_exit(&flent->fe_lock);
2248 
2249 	mac_srs->srs_state = 0;
2250 	mac_srs->srs_type = (srs_type | SRST_NO_SOFT_RINGS);
2251 	mac_srs->srs_worker_cpuid = mac_srs->srs_worker_cpuid_save = -1;
2252 	mac_srs->srs_poll_cpuid = mac_srs->srs_poll_cpuid_save = -1;
2253 	mac_srs->srs_mcip = mcip;
2254 	mac_srs_fanout_list_alloc(mac_srs);
2255 
2256 	/*
2257 	 * For a flow we use the underlying MAC client's priority range with
2258 	 * the priority value to find an absolute priority value. For a MAC
2259 	 * client we use the MAC client's maximum priority as the value.
2260 	 */
2261 	mrp = &flent->fe_effective_props;
2262 	if ((mac_srs->srs_type & SRST_FLOW) != 0) {
2263 		mac_srs->srs_pri = FLOW_PRIORITY(mcip->mci_min_pri,
2264 		    mcip->mci_max_pri, mrp->mrp_priority);
2265 	} else {
2266 		mac_srs->srs_pri = mcip->mci_max_pri;
2267 	}
2268 	/*
2269 	 * We need to insert the SRS in the global list before
2270 	 * binding the SRS and SR threads. Otherwise there is a
2271 	 * is a small window where the cpu reconfig callbacks
2272 	 * may miss the SRS in the list walk and DR could fail
2273 	 * as there are bound threads.
2274 	 */
2275 	mac_srs_add_glist(mac_srs);
2276 
2277 	/* Initialize bw limit */
2278 	if ((mrp->mrp_mask & MRP_MAXBW) != 0) {
2279 		mutex_enter(&my_bw->mac_bw_lock);
2280 		mac_bw_ctl_set_state(my_bw, B_TRUE, mrp);
2281 		mutex_exit(&my_bw->mac_bw_lock);
2282 		mac_srs->srs_type |= SRST_BW_CONTROL;
2283 	}
2284 	mac_srs_update_drain_proc(mac_srs);
2285 
2286 	/*
2287 	 * We use the following policy to control Receive
2288 	 * Side Dynamic Polling:
2289 	 * 1) We switch to poll mode anytime the processing thread causes
2290 	 *    a backlog to build up in SRS and its associated Soft Rings
2291 	 *    (sr_poll_pkt_cnt > 0).
2292 	 * 2) As long as the backlog stays under the low water mark
2293 	 *    (sr_lowat), we poll the H/W for more packets.
2294 	 * 3) If the backlog (sr_poll_pkt_cnt) exceeds low water mark, we
2295 	 *    stay in poll mode but don't poll the H/W for more packets.
2296 	 * 4) Anytime in polling mode, if we poll the H/W for packets and
2297 	 *    find nothing plus we have an existing backlog
2298 	 *    (sr_poll_pkt_cnt > 0), we stay in polling mode but don't poll
2299 	 *    the H/W for packets anymore (let the polling thread go to sleep).
2300 	 * 5) Once the backlog is relieved (packets are processed) we reenable
2301 	 *    polling (by signalling the poll thread) only when the backlog
2302 	 *    dips below sr_poll_thres.
2303 	 * 6) sr_hiwat is used exclusively when we are not polling capable
2304 	 *    and is used to decide when to drop packets so the SRS queue
2305 	 *    length doesn't grow infinitely.
2306 	 */
2307 	if (!is_tx_srs) {
2308 		srs_rx->sr_hiwat = mac_soft_ring_max_q_cnt;
2309 		/* Low water mark needs to be less than high water mark */
2310 		srs_rx->sr_lowat = mac_soft_ring_min_q_cnt <=
2311 		    mac_soft_ring_max_q_cnt ? mac_soft_ring_min_q_cnt :
2312 		    (mac_soft_ring_max_q_cnt >> 2);
2313 		/* Poll threshold need to be half of low water mark or less */
2314 		srs_rx->sr_poll_thres = mac_soft_ring_poll_thres <=
2315 		    (srs_rx->sr_lowat >> 1) ? mac_soft_ring_poll_thres :
2316 		    (srs_rx->sr_lowat >> 1);
2317 		mac_srs->srs_type |= mac_latency_optimize ?
2318 		    SRST_LATENCY_OPT : SRST_ENQUEUE;
2319 	}
2320 
2321 	/*
2322 	 * Create the srs_worker with twice the stack of a normal kernel thread
2323 	 * to reduce the likelihood of stack overflows in receive-side
2324 	 * processing.  (The larger stacks are not the only precaution taken
2325 	 * against stack overflows; see the use of mac_rx_srs_stack_needed
2326 	 * in mac_sched.c).
2327 	 */
2328 	mac_srs->srs_worker = thread_create(NULL, default_stksize << 1,
2329 	    mac_srs_worker, mac_srs, 0, &p0, TS_RUN, mac_srs->srs_pri);
2330 
2331 	if (is_tx_srs) {
2332 		/* Handle everything about Tx SRS and return */
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 = mcip;
2338 		srs_tx->st_arg2 = NULL;
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 = mcip;
2350 
2351 	if (ring != NULL) {
2352 		uint_t ring_info;
2353 
2354 		/* Is the mac_srs created over the RX default group? */
2355 		if (ring->mr_gh == (mac_group_handle_t)
2356 		    MAC_DEFAULT_RX_GROUP(mcip->mci_mip)) {
2357 			mac_srs->srs_type |= SRST_DEFAULT_GRP;
2358 		}
2359 		mac_srs->srs_ring = ring;
2360 		ring->mr_srs = mac_srs;
2361 		ring->mr_classify_type = MAC_HW_CLASSIFIER;
2362 		ring->mr_flag |= MR_INCIPIENT;
2363 
2364 		if (!(mcip->mci_mip->mi_state_flags & MIS_POLL_DISABLE) &&
2365 		    FLOW_TAB_EMPTY(mcip->mci_subflow_tab) && mac_poll_enable)
2366 			mac_srs->srs_state |= SRS_POLLING_CAPAB;
2367 
2368 		mac_srs->srs_poll_thr = thread_create(NULL, 0,
2369 		    mac_rx_srs_poll_ring, mac_srs, 0, &p0, TS_RUN,
2370 		    mac_srs->srs_pri);
2371 		/*
2372 		 * Some drivers require serialization and don't send
2373 		 * packet chains in interrupt context. For such
2374 		 * drivers, we should always queue in the soft ring
2375 		 * so that we get a chance to switch into polling
2376 		 * mode under backlog.
2377 		 */
2378 		ring_info = mac_hwring_getinfo((mac_ring_handle_t)ring);
2379 		if (ring_info & MAC_RING_RX_ENQUEUE) {
2380 			mac_srs->srs_type |= SRST_ENQUEUE;
2381 		}
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 mac_soft_ring_set_type_t
2395 mac_find_fanout(flow_entry_t *flent, const mac_soft_ring_set_type_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, 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     const mac_soft_ring_set_type_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 	mac_group_t		*rx_group = flent->fe_rx_ring_group;
2501 	boolean_t		no_unicast;
2502 
2503 	/*
2504 	 * If this is an an aggr port, then don't setup Rx SRS and Rx
2505 	 * soft rings as they won't be used. However, we still need to
2506 	 * start the rings to receive data on them.
2507 	 */
2508 	if (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) {
2509 		if (rx_group == NULL)
2510 			return;
2511 
2512 		for (ring = rx_group->mrg_rings; ring != NULL;
2513 		    ring = ring->mr_next) {
2514 			if (ring->mr_state != MR_INUSE)
2515 				(void) mac_start_ring(ring);
2516 		}
2517 
2518 		return;
2519 	}
2520 
2521 	/*
2522 	 * Aggr ports should never have SRSes.
2523 	 */
2524 	ASSERT3U((mcip->mci_state_flags & MCIS_IS_AGGR_PORT), ==, 0);
2525 
2526 	const mac_soft_ring_set_type_t fanout_type =
2527 	    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, 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, 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     const mac_soft_ring_set_type_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, 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     const mac_soft_ring_set_type_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     const mac_soft_ring_set_type_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 		VERIFY(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, cpupart);
3228 				mac_set_pool_effective(use_default, cpupart,
3229 				    mrp, emrp);
3230 				pool_unlock();
3231 			}
3232 			ASSERT(default_rgroup->mrg_state ==
3233 			    MAC_GROUP_STATE_SHARED);
3234 		}
3235 
3236 		/*
3237 		 * A VLAN MAC client on a reserved group still
3238 		 * requires SW classification if the MAC doesn't
3239 		 * provide VLAN HW filtering.
3240 		 *
3241 		 * Clients with no unicast address also require SW
3242 		 * classification.
3243 		 */
3244 		if (rgroup->mrg_state == MAC_GROUP_STATE_RESERVED &&
3245 		    ((!MAC_GROUP_HW_VLAN(rgroup) && vid != VLAN_ID_NONE) ||
3246 		    no_unicast)) {
3247 			mac_rx_switch_grp_to_sw(rgroup);
3248 		}
3249 
3250 	}
3251 
3252 	mac_set_rings_effective(mcip);
3253 	return (0);
3254 
3255 setup_failed:
3256 	/* Switch the primary back to default group */
3257 	if (reloc_pmcip != NULL) {
3258 		(void) mac_rx_switch_group(reloc_pmcip,
3259 		    reloc_pmcip->mci_flent->fe_rx_ring_group, default_rgroup);
3260 	}
3261 	mac_datapath_teardown(mcip, flent, link_type);
3262 	return (err);
3263 }
3264 
3265 void
3266 mac_datapath_teardown(mac_client_impl_t *mcip, flow_entry_t *flent,
3267     const mac_soft_ring_set_type_t link_type)
3268 {
3269 	mac_impl_t		*mip = mcip->mci_mip;
3270 	mac_group_t		*group = NULL;
3271 	mac_client_impl_t	*grp_only_mcip;
3272 	flow_entry_t		*group_only_flent;
3273 	mac_group_t		*default_group;
3274 	boolean_t		check_default_group = B_FALSE;
3275 	mac_group_state_t	next_state;
3276 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
3277 	uint16_t		vid;
3278 
3279 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3280 
3281 	switch (link_type) {
3282 	case SRST_FLOW:
3283 		mac_rx_srs_group_teardown(flent, B_FALSE);
3284 		mac_tx_srs_group_teardown(mcip, flent, SRST_FLOW);
3285 		return;
3286 
3287 	case SRST_LINK:
3288 		/* Stop sending packets */
3289 		mac_tx_client_block(mcip);
3290 		group = flent->fe_rx_ring_group;
3291 		vid = i_mac_flow_vid(flent);
3292 
3293 		/*
3294 		 * Stop the packet flow from the hardware by disabling
3295 		 * any hardware filters assigned to this client.
3296 		 */
3297 		if (mcip->mci_unicast != NULL) {
3298 			int err;
3299 
3300 			err = mac_remove_macaddr_vlan(mcip->mci_unicast, vid);
3301 
3302 			if (err != 0) {
3303 				cmn_err(CE_WARN, "%s: failed to remove a MAC HW"
3304 				    " filters because of error 0x%x",
3305 				    mip->mi_name, err);
3306 			}
3307 
3308 			mcip->mci_unicast = NULL;
3309 		}
3310 
3311 		/* Stop the packets coming from the S/W classifier */
3312 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
3313 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
3314 
3315 		/* Quiesce and destroy all the SRSes. */
3316 		mac_rx_srs_group_teardown(flent, B_FALSE);
3317 		mac_tx_srs_group_teardown(mcip, flent, SRST_LINK);
3318 
3319 		ASSERT3P(mcip->mci_flent, ==, flent);
3320 		ASSERT3P(flent->fe_next, ==, NULL);
3321 
3322 		/*
3323 		 * Release our hold on the group as well. We need
3324 		 * to check if the shared group has only one client
3325 		 * left who can use it exclusively. Also, if we
3326 		 * were the last client, release the group.
3327 		 */
3328 		default_group = MAC_DEFAULT_RX_GROUP(mip);
3329 		if (group != NULL) {
3330 			mac_group_remove_client(group, mcip);
3331 			next_state = mac_group_next_state(group,
3332 			    &grp_only_mcip, default_group, B_TRUE);
3333 
3334 			if (next_state == MAC_GROUP_STATE_RESERVED) {
3335 				/*
3336 				 * Only one client left on this RX group.
3337 				 */
3338 				VERIFY(grp_only_mcip != NULL);
3339 				mac_set_group_state(group,
3340 				    MAC_GROUP_STATE_RESERVED);
3341 				group_only_flent = grp_only_mcip->mci_flent;
3342 
3343 				/*
3344 				 * The only remaining client has exclusive
3345 				 * access on the group. Allow it to
3346 				 * dynamically poll the H/W rings etc.
3347 				 */
3348 				mac_rx_srs_group_setup(grp_only_mcip,
3349 				    group_only_flent, SRST_LINK);
3350 				mac_fanout_setup(grp_only_mcip,
3351 				    group_only_flent,
3352 				    MCIP_RESOURCE_PROPS(grp_only_mcip),
3353 				    mac_rx_deliver, grp_only_mcip, NULL);
3354 				mac_rx_group_unmark(group, MR_INCIPIENT);
3355 				mac_set_rings_effective(grp_only_mcip);
3356 			} else if (next_state == MAC_GROUP_STATE_REGISTERED) {
3357 				/*
3358 				 * This is a non-default group being freed up.
3359 				 * We need to reevaluate the default group
3360 				 * to see if the primary client can get
3361 				 * exclusive access to the default group.
3362 				 */
3363 				VERIFY3P(group, !=, MAC_DEFAULT_RX_GROUP(mip));
3364 				if (mrp->mrp_mask & MRP_RX_RINGS) {
3365 					MAC_RX_GRP_RELEASED(mip);
3366 					if (mip->mi_rx_group_type ==
3367 					    MAC_GROUP_TYPE_DYNAMIC) {
3368 						MAC_RX_RING_RELEASED(mip,
3369 						    group->mrg_cur_count);
3370 					}
3371 				}
3372 				mac_release_rx_group(mcip, group);
3373 				mac_set_group_state(group,
3374 				    MAC_GROUP_STATE_REGISTERED);
3375 				check_default_group = B_TRUE;
3376 			} else {
3377 				VERIFY3S(next_state, ==,
3378 				    MAC_GROUP_STATE_SHARED);
3379 				mac_set_group_state(group,
3380 				    MAC_GROUP_STATE_SHARED);
3381 				mac_rx_group_unmark(group, MR_CONDEMNED);
3382 			}
3383 			flent->fe_rx_ring_group = NULL;
3384 		}
3385 		/*
3386 		 * Remove the client from the TX group. Additionally, if
3387 		 * this a non-default group, then we also need to release
3388 		 * the group.
3389 		 */
3390 		group = flent->fe_tx_ring_group;
3391 		default_group = MAC_DEFAULT_TX_GROUP(mip);
3392 		if (group != NULL) {
3393 			mac_group_remove_client(group, mcip);
3394 			next_state = mac_group_next_state(group,
3395 			    &grp_only_mcip, default_group, B_FALSE);
3396 			if (next_state == MAC_GROUP_STATE_REGISTERED) {
3397 				if (group != default_group) {
3398 					if (mrp->mrp_mask & MRP_TX_RINGS) {
3399 						MAC_TX_GRP_RELEASED(mip);
3400 						if (mip->mi_tx_group_type ==
3401 						    MAC_GROUP_TYPE_DYNAMIC) {
3402 							MAC_TX_RING_RELEASED(
3403 							    mip, group->
3404 							    mrg_cur_count);
3405 						}
3406 					}
3407 					mac_release_tx_group(mcip, group);
3408 					/*
3409 					 * If the default group is reserved,
3410 					 * then we need to set the effective
3411 					 * rings as we would have given
3412 					 * back some rings when the group
3413 					 * was released
3414 					 */
3415 					if (mip->mi_tx_group_type ==
3416 					    MAC_GROUP_TYPE_DYNAMIC &&
3417 					    default_group->mrg_state ==
3418 					    MAC_GROUP_STATE_RESERVED) {
3419 						grp_only_mcip =
3420 						    MAC_GROUP_ONLY_CLIENT
3421 						    (default_group);
3422 						mac_set_rings_effective(
3423 						    grp_only_mcip);
3424 					}
3425 				} else {
3426 					mac_ring_t	*ring;
3427 					int		cnt;
3428 					int		ringcnt;
3429 
3430 					/*
3431 					 * Stop all the rings except the
3432 					 * default ring.
3433 					 */
3434 					ringcnt = group->mrg_cur_count;
3435 					ring = group->mrg_rings;
3436 					for (cnt = 0; cnt < ringcnt; cnt++) {
3437 						if (ring->mr_state ==
3438 						    MR_INUSE && ring !=
3439 						    (mac_ring_t *)
3440 						    mip->mi_default_tx_ring) {
3441 							mac_stop_ring(ring);
3442 							ring->mr_flag = 0;
3443 						}
3444 						ring = ring->mr_next;
3445 					}
3446 				}
3447 			} else if (next_state == MAC_GROUP_STATE_RESERVED) {
3448 				mac_set_rings_effective(grp_only_mcip);
3449 			}
3450 			flent->fe_tx_ring_group = NULL;
3451 			group->mrg_state = next_state;
3452 		}
3453 		break;
3454 	default:
3455 		ASSERT(B_FALSE);
3456 		break;
3457 	}
3458 
3459 	/*
3460 	 * The mac client using the default group gets exclusive access to the
3461 	 * default group if and only if it is the sole client on the entire
3462 	 * mip. If so set the group state to reserved, and set up the SRSes
3463 	 * over the default group.
3464 	 */
3465 	if (check_default_group) {
3466 		default_group = MAC_DEFAULT_RX_GROUP(mip);
3467 		VERIFY3S(default_group->mrg_state, ==, MAC_GROUP_STATE_SHARED);
3468 		next_state = mac_group_next_state(default_group,
3469 		    &grp_only_mcip, default_group, B_TRUE);
3470 		if (next_state == MAC_GROUP_STATE_RESERVED) {
3471 			VERIFY(grp_only_mcip != NULL);
3472 			VERIFY3U(mip->mi_nactiveclients, ==, 1);
3473 			mac_set_group_state(default_group,
3474 			    MAC_GROUP_STATE_RESERVED);
3475 			mac_rx_srs_group_setup(grp_only_mcip,
3476 			    grp_only_mcip->mci_flent, SRST_LINK);
3477 			mac_fanout_setup(grp_only_mcip,
3478 			    grp_only_mcip->mci_flent,
3479 			    MCIP_RESOURCE_PROPS(grp_only_mcip), mac_rx_deliver,
3480 			    grp_only_mcip, NULL);
3481 			mac_rx_group_unmark(default_group, MR_INCIPIENT);
3482 			mac_set_rings_effective(grp_only_mcip);
3483 		}
3484 	}
3485 
3486 	/*
3487 	 * If the primary is the only one left and the MAC supports
3488 	 * dynamic grouping, we need to see if the primary needs to
3489 	 * be moved to the default group so that it can use all the
3490 	 * H/W rings.
3491 	 */
3492 	if (!(flent->fe_type & FLOW_PRIMARY_MAC) &&
3493 	    mip->mi_nactiveclients == 1 &&
3494 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
3495 		default_group = MAC_DEFAULT_RX_GROUP(mip);
3496 		grp_only_mcip = mac_primary_client_handle(mip);
3497 		if (grp_only_mcip == NULL)
3498 			return;
3499 		group_only_flent = grp_only_mcip->mci_flent;
3500 		mrp = MCIP_RESOURCE_PROPS(grp_only_mcip);
3501 		/*
3502 		 * If the primary has an explicit property set, leave it
3503 		 * alone.
3504 		 */
3505 		if (mrp->mrp_mask & MRP_RX_RINGS)
3506 			return;
3507 		/*
3508 		 * Switch the primary to the default group.
3509 		 */
3510 		(void) mac_rx_switch_group(grp_only_mcip,
3511 		    group_only_flent->fe_rx_ring_group, default_group);
3512 	}
3513 }
3514 
3515 /* DATAPATH TEAR DOWN ROUTINES (SRS and FANOUT teardown) */
3516 
3517 static void
3518 mac_srs_fanout_list_free(mac_soft_ring_set_t *mac_srs)
3519 {
3520 	if (mac_srs->srs_type & SRST_TX) {
3521 		mac_srs_tx_t *tx;
3522 
3523 		ASSERT(mac_srs->srs_tcp_soft_rings == NULL);
3524 		ASSERT(mac_srs->srs_udp_soft_rings == NULL);
3525 		ASSERT(mac_srs->srs_tcp6_soft_rings == NULL);
3526 		ASSERT(mac_srs->srs_udp6_soft_rings == NULL);
3527 		ASSERT(mac_srs->srs_oth_soft_rings == NULL);
3528 		ASSERT(mac_srs->srs_tx_soft_rings != NULL);
3529 		kmem_free(mac_srs->srs_tx_soft_rings,
3530 		    sizeof (mac_soft_ring_t *) * MAX_RINGS_PER_GROUP);
3531 		mac_srs->srs_tx_soft_rings = NULL;
3532 		tx = &mac_srs->srs_tx;
3533 		if (tx->st_soft_rings != NULL) {
3534 			kmem_free(tx->st_soft_rings,
3535 			    sizeof (mac_soft_ring_t *) * MAX_RINGS_PER_GROUP);
3536 		}
3537 	} else {
3538 		ASSERT(mac_srs->srs_tx_soft_rings == NULL);
3539 
3540 		ASSERT(mac_srs->srs_tcp_soft_rings != NULL);
3541 		kmem_free(mac_srs->srs_tcp_soft_rings,
3542 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3543 		mac_srs->srs_tcp_soft_rings = NULL;
3544 
3545 		ASSERT(mac_srs->srs_udp_soft_rings != NULL);
3546 		kmem_free(mac_srs->srs_udp_soft_rings,
3547 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3548 		mac_srs->srs_udp_soft_rings = NULL;
3549 
3550 		ASSERT(mac_srs->srs_tcp6_soft_rings != NULL);
3551 		kmem_free(mac_srs->srs_tcp6_soft_rings,
3552 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3553 		mac_srs->srs_tcp6_soft_rings = NULL;
3554 
3555 		ASSERT(mac_srs->srs_udp6_soft_rings != NULL);
3556 		kmem_free(mac_srs->srs_udp6_soft_rings,
3557 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3558 		mac_srs->srs_udp6_soft_rings = NULL;
3559 
3560 		ASSERT(mac_srs->srs_oth_soft_rings != NULL);
3561 		kmem_free(mac_srs->srs_oth_soft_rings,
3562 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3563 		mac_srs->srs_oth_soft_rings = NULL;
3564 	}
3565 }
3566 
3567 /*
3568  * An RX SRS is attached to at most one mac_ring.
3569  * A TX SRS  has no  rings.
3570  */
3571 static void
3572 mac_srs_ring_free(mac_soft_ring_set_t *mac_srs)
3573 {
3574 	mac_client_impl_t	*mcip;
3575 	mac_ring_t		*ring;
3576 	flow_entry_t		*flent;
3577 
3578 	ring = mac_srs->srs_ring;
3579 	if (mac_srs->srs_type & SRST_TX) {
3580 		ASSERT(ring == NULL);
3581 		return;
3582 	}
3583 
3584 	if (ring == NULL)
3585 		return;
3586 
3587 	/*
3588 	 * Broadcast flows don't have a client impl association, but they
3589 	 * use only soft rings.
3590 	 */
3591 	flent = mac_srs->srs_flent;
3592 	mcip = flent->fe_mcip;
3593 	ASSERT(mcip != NULL);
3594 
3595 	ring->mr_classify_type = MAC_NO_CLASSIFIER;
3596 	ring->mr_srs = NULL;
3597 }
3598 
3599 /*
3600  * Physical unlink and free of the data structures happen below. This is
3601  * driven from mac_flow_destroy(), on the last refrele of a flow.
3602  *
3603  * Assumes Rx srs is 1-1 mapped with an ring.
3604  */
3605 void
3606 mac_srs_free(mac_soft_ring_set_t *mac_srs)
3607 {
3608 	ASSERT(mac_srs->srs_mcip == NULL ||
3609 	    MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
3610 	ASSERT((mac_srs->srs_state & (SRS_CONDEMNED | SRS_CONDEMNED_DONE |
3611 	    SRS_PROC | SRS_PROC_FAST)) == (SRS_CONDEMNED | SRS_CONDEMNED_DONE));
3612 
3613 	mac_drop_chain(mac_srs->srs_first, "SRS free");
3614 	mac_srs_ring_free(mac_srs);
3615 	mac_srs_soft_rings_free(mac_srs);
3616 	mac_srs_fanout_list_free(mac_srs);
3617 
3618 	mac_srs->srs_bw = NULL;
3619 	mac_srs_stat_delete(mac_srs);
3620 	kmem_cache_free(mac_srs_cache, mac_srs);
3621 }
3622 
3623 static void
3624 mac_srs_soft_rings_quiesce(mac_soft_ring_set_t *mac_srs,
3625     const mac_soft_ring_state_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 	VERIFY(MUTEX_HELD(&mac_srs->srs_lock));
3666 
3667 	VERIFY((mac_srs->srs_state & (SRS_CONDEMNED | SRS_QUIESCE)) != 0);
3668 	const boolean_t condemn = (mac_srs->srs_state & SRS_CONDEMNED) != 0;
3669 	const mac_soft_ring_state_t s_ring_flag = condemn ?
3670 	    S_RING_CONDEMNED : S_RING_QUIESCE;
3671 	const mac_soft_ring_set_state_t srs_poll_wait_flag = condemn ?
3672 	    SRS_POLL_THR_EXITED : SRS_POLL_THR_QUIESCED;
3673 
3674 	/*
3675 	 * In the case of Rx SRS wait till the poll thread is done.
3676 	 */
3677 	if ((mac_srs->srs_type & SRST_TX) == 0 &&
3678 	    mac_srs->srs_poll_thr != NULL) {
3679 		while (!(mac_srs->srs_state & srs_poll_wait_flag))
3680 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3681 
3682 		/*
3683 		 * Turn off polling as part of the quiesce operation.
3684 		 */
3685 		MAC_SRS_POLLING_OFF(mac_srs);
3686 		mac_srs->srs_state &= ~(SRS_POLLING | SRS_GET_PKTS);
3687 	}
3688 
3689 	/*
3690 	 * Then signal the soft ring worker threads to quiesce or quit
3691 	 * as needed and then wait till that happens.
3692 	 */
3693 	mac_srs_soft_rings_quiesce(mac_srs, s_ring_flag);
3694 
3695 	if (condemn)
3696 		mac_srs->srs_state |= (SRS_QUIESCE_DONE | SRS_CONDEMNED_DONE);
3697 	else
3698 		mac_srs->srs_state |= SRS_QUIESCE_DONE;
3699 	cv_signal(&mac_srs->srs_quiesce_done_cv);
3700 }
3701 
3702 /*
3703  * Signal an SRS to start a temporary quiesce, or permanent removal, or restart
3704  * a quiesced SRS by setting the appropriate flags and signaling the SRS worker
3705  * or poll thread. This function is internal to the quiescing logic and is
3706  * called internally from the SRS quiesce or flow quiesce or client quiesce
3707  * higher level functions.
3708  */
3709 void
3710 mac_srs_signal(mac_soft_ring_set_t *mac_srs,
3711     const mac_soft_ring_set_state_t srs_flag)
3712 {
3713 	mac_ring_t	*ring;
3714 
3715 	ring = mac_srs->srs_ring;
3716 	ASSERT(ring == NULL || ring->mr_refcnt == 0);
3717 
3718 	if (srs_flag == SRS_CONDEMNED) {
3719 		/*
3720 		 * The SRS is going away. We need to unbind the SRS and SR
3721 		 * threads before removing from the global SRS list. Otherwise
3722 		 * there is a small window where the cpu reconfig callbacks
3723 		 * may miss the SRS in the list walk and DR could fail since
3724 		 * there are still bound threads.
3725 		 */
3726 		mac_srs_threads_unbind(mac_srs);
3727 		mac_srs_remove_glist(mac_srs);
3728 	}
3729 	/*
3730 	 * Wakeup the SRS worker and poll threads.
3731 	 */
3732 	mutex_enter(&mac_srs->srs_lock);
3733 	mac_srs->srs_state |= srs_flag;
3734 	cv_signal(&mac_srs->srs_async);
3735 	cv_signal(&mac_srs->srs_cv);
3736 	mutex_exit(&mac_srs->srs_lock);
3737 }
3738 
3739 /*
3740  * In the Rx side, the quiescing is done bottom up. After the Rx upcalls
3741  * from the driver are done, then the Rx SRS is quiesced and only then can
3742  * we signal the soft rings. Thus this function can't be called arbitrarily
3743  * without satisfying the prerequisites. On the Tx side, the threads from
3744  * top need to quiesced, then the Tx SRS and only then can we signal the
3745  * Tx soft rings.
3746  */
3747 static void
3748 mac_srs_soft_rings_signal(mac_soft_ring_set_t *mac_srs,
3749     const mac_soft_ring_state_t sr_flag)
3750 {
3751 	mac_soft_ring_t		*softring;
3752 
3753 	for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
3754 	    softring = softring->s_ring_next)
3755 		mac_soft_ring_signal(softring, sr_flag);
3756 }
3757 
3758 /*
3759  * The block comment above mac_rx_classify_flow_state_change explains the
3760  * background. At this point the SRS is quiesced and we need to restart the
3761  * SRS worker, poll, and softring threads. The SRS worker thread serves as
3762  * the master controller. The steps involved are described below in the function
3763  */
3764 void
3765 mac_srs_worker_restart(mac_soft_ring_set_t *mac_srs)
3766 {
3767 	boolean_t	iam_rx_srs;
3768 	mac_soft_ring_t	*softring;
3769 
3770 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
3771 	if ((mac_srs->srs_type & SRST_TX) != 0) {
3772 		iam_rx_srs = B_FALSE;
3773 		ASSERT((mac_srs->srs_state &
3774 		    (SRS_POLL_THR_QUIESCED | SRS_QUIESCE_DONE | SRS_QUIESCE)) ==
3775 		    (SRS_QUIESCE_DONE | SRS_QUIESCE));
3776 	} else {
3777 		iam_rx_srs = B_TRUE;
3778 		ASSERT((mac_srs->srs_state &
3779 		    (SRS_QUIESCE_DONE | SRS_QUIESCE)) ==
3780 		    (SRS_QUIESCE_DONE | SRS_QUIESCE));
3781 		if (mac_srs->srs_poll_thr != NULL) {
3782 			ASSERT((mac_srs->srs_state & SRS_POLL_THR_QUIESCED) ==
3783 			    SRS_POLL_THR_QUIESCED);
3784 		}
3785 	}
3786 
3787 	/*
3788 	 * Signal any quiesced soft ring workers to restart and wait for the
3789 	 * soft ring down count to come down to zero.
3790 	 */
3791 	if (mac_srs->srs_soft_ring_quiesced_count != 0) {
3792 		for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
3793 		    softring = softring->s_ring_next) {
3794 			if (!(softring->s_ring_state & S_RING_QUIESCE))
3795 				continue;
3796 			mac_soft_ring_signal(softring, S_RING_RESTART);
3797 		}
3798 		while (mac_srs->srs_soft_ring_quiesced_count != 0)
3799 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3800 	}
3801 
3802 	mac_srs->srs_state &= ~(SRS_QUIESCE_DONE | SRS_QUIESCE | SRS_RESTART);
3803 	if (iam_rx_srs && mac_srs->srs_poll_thr != NULL) {
3804 		/*
3805 		 * Signal the poll thread and ask it to restart. Wait till it
3806 		 * actually restarts and the SRS_POLL_THR_QUIESCED flag gets
3807 		 * cleared.
3808 		 */
3809 		mac_srs->srs_state |= SRS_POLL_THR_RESTART;
3810 		cv_signal(&mac_srs->srs_cv);
3811 		while (mac_srs->srs_state & SRS_POLL_THR_QUIESCED)
3812 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3813 		ASSERT(!(mac_srs->srs_state & SRS_POLL_THR_RESTART));
3814 	}
3815 	/* Wake up any waiter waiting for the restart to complete */
3816 	mac_srs->srs_state |= SRS_RESTART_DONE;
3817 	cv_signal(&mac_srs->srs_quiesce_done_cv);
3818 }
3819 
3820 static void
3821 mac_srs_worker_unbind(mac_soft_ring_set_t *mac_srs)
3822 {
3823 	mutex_enter(&mac_srs->srs_lock);
3824 	if (!(mac_srs->srs_state & SRS_WORKER_BOUND)) {
3825 		ASSERT(mac_srs->srs_worker_cpuid == -1);
3826 		mutex_exit(&mac_srs->srs_lock);
3827 		return;
3828 	}
3829 
3830 	mac_srs->srs_worker_cpuid = -1;
3831 	mac_srs->srs_state &= ~SRS_WORKER_BOUND;
3832 	thread_affinity_clear(mac_srs->srs_worker);
3833 	mutex_exit(&mac_srs->srs_lock);
3834 }
3835 
3836 static void
3837 mac_srs_poll_unbind(mac_soft_ring_set_t *mac_srs)
3838 {
3839 	mutex_enter(&mac_srs->srs_lock);
3840 	if (mac_srs->srs_poll_thr == NULL ||
3841 	    (mac_srs->srs_state & SRS_POLL_BOUND) == 0) {
3842 		ASSERT(mac_srs->srs_poll_cpuid == -1);
3843 		mutex_exit(&mac_srs->srs_lock);
3844 		return;
3845 	}
3846 
3847 	mac_srs->srs_poll_cpuid = -1;
3848 	mac_srs->srs_state &= ~SRS_POLL_BOUND;
3849 	thread_affinity_clear(mac_srs->srs_poll_thr);
3850 	mutex_exit(&mac_srs->srs_lock);
3851 }
3852 
3853 static void
3854 mac_srs_threads_unbind(mac_soft_ring_set_t *mac_srs)
3855 {
3856 	mac_soft_ring_t	*soft_ring;
3857 
3858 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
3859 
3860 	mutex_enter(&cpu_lock);
3861 	mac_srs_worker_unbind(mac_srs);
3862 	if (!(mac_srs->srs_type & SRST_TX))
3863 		mac_srs_poll_unbind(mac_srs);
3864 
3865 	for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
3866 	    soft_ring = soft_ring->s_ring_next) {
3867 		mac_soft_ring_unbind(soft_ring);
3868 	}
3869 	mutex_exit(&cpu_lock);
3870 }
3871 
3872 /*
3873  * When a CPU is going away, unbind all MAC threads which are bound
3874  * to that CPU. The affinity of the thread to the CPU is saved to allow
3875  * the thread to be rebound to the CPU if it comes back online.
3876  */
3877 static void
3878 mac_walk_srs_and_unbind(int cpuid)
3879 {
3880 	mac_soft_ring_set_t *mac_srs;
3881 	mac_soft_ring_t *soft_ring;
3882 
3883 	rw_enter(&mac_srs_g_lock, RW_READER);
3884 
3885 	if ((mac_srs = mac_srs_g_list) == NULL)
3886 		goto done;
3887 
3888 	for (; mac_srs != NULL; mac_srs = mac_srs->srs_next) {
3889 		if (mac_srs->srs_worker_cpuid == cpuid) {
3890 			mac_srs->srs_worker_cpuid_save = cpuid;
3891 			mac_srs_worker_unbind(mac_srs);
3892 		}
3893 
3894 		if (!(mac_srs->srs_type & SRST_TX)) {
3895 			if (mac_srs->srs_poll_cpuid == cpuid) {
3896 				mac_srs->srs_poll_cpuid_save = cpuid;
3897 				mac_srs_poll_unbind(mac_srs);
3898 			}
3899 		}
3900 
3901 		/* Next tackle the soft rings associated with the srs */
3902 		mutex_enter(&mac_srs->srs_lock);
3903 		for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
3904 		    soft_ring = soft_ring->s_ring_next) {
3905 			if (soft_ring->s_ring_cpuid == cpuid) {
3906 				soft_ring->s_ring_cpuid_save = cpuid;
3907 				mac_soft_ring_unbind(soft_ring);
3908 			}
3909 		}
3910 		mutex_exit(&mac_srs->srs_lock);
3911 	}
3912 done:
3913 	rw_exit(&mac_srs_g_lock);
3914 }
3915 
3916 /* TX SETUP and TEARDOWN ROUTINES */
3917 
3918 /*
3919  * XXXHIO need to make sure the two mac_tx_srs_{add,del}_ring()
3920  * handle the case where the number of rings is one. I.e. there is
3921  * a ring pointed to by mac_srs->srs_tx_arg2.
3922  */
3923 void
3924 mac_tx_srs_add_ring(mac_soft_ring_set_t *mac_srs, mac_ring_t *tx_ring)
3925 {
3926 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
3927 	mac_soft_ring_t *soft_ring;
3928 	int count = mac_srs->srs_tx_ring_count;
3929 	mac_soft_ring_state_t soft_ring_type = 0;
3930 	uint_t ring_info;
3931 
3932 	ASSERT(mac_srs->srs_state & SRS_QUIESCE);
3933 	ring_info = mac_hwring_getinfo((mac_ring_handle_t)tx_ring);
3934 	if (mac_tx_serialize || (ring_info & MAC_RING_TX_SERIALIZE))
3935 		soft_ring_type |= ST_RING_WORKER_ONLY;
3936 	soft_ring = mac_soft_ring_create_tx(count, 0, soft_ring_type,
3937 	    maxclsyspri, mcip, mac_srs, -1, tx_ring);
3938 	mac_srs->srs_tx_ring_count++;
3939 	mac_srs_update_fanout_list(mac_srs);
3940 	/*
3941 	 * put this soft ring in quiesce mode too so when we restart
3942 	 * all soft rings in the srs are in the same state.
3943 	 */
3944 	mac_soft_ring_signal(soft_ring, S_RING_QUIESCE);
3945 }
3946 
3947 static void
3948 mac_soft_ring_remove(mac_soft_ring_set_t *mac_srs, mac_soft_ring_t *softring)
3949 {
3950 	int sringcnt;
3951 
3952 	mutex_enter(&mac_srs->srs_lock);
3953 	sringcnt = mac_srs->srs_soft_ring_count;
3954 	ASSERT(sringcnt > 0);
3955 	mac_soft_ring_signal(softring, S_RING_CONDEMNED);
3956 
3957 	ASSERT(mac_srs->srs_soft_ring_condemned_count == 0);
3958 	while (mac_srs->srs_soft_ring_condemned_count != 1)
3959 		cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3960 
3961 	if (softring == mac_srs->srs_soft_ring_head) {
3962 		mac_srs->srs_soft_ring_head = softring->s_ring_next;
3963 		if (mac_srs->srs_soft_ring_head != NULL) {
3964 			mac_srs->srs_soft_ring_head->s_ring_prev = NULL;
3965 		} else {
3966 			mac_srs->srs_soft_ring_tail = NULL;
3967 		}
3968 	} else {
3969 		softring->s_ring_prev->s_ring_next =
3970 		    softring->s_ring_next;
3971 		if (softring->s_ring_next != NULL) {
3972 			softring->s_ring_next->s_ring_prev =
3973 			    softring->s_ring_prev;
3974 		} else {
3975 			mac_srs->srs_soft_ring_tail =
3976 			    softring->s_ring_prev;
3977 		}
3978 	}
3979 	mac_srs->srs_soft_ring_count--;
3980 
3981 	mac_srs->srs_soft_ring_condemned_count--;
3982 	mutex_exit(&mac_srs->srs_lock);
3983 
3984 	mac_soft_ring_free(softring);
3985 }
3986 
3987 void
3988 mac_tx_srs_del_ring(mac_soft_ring_set_t *mac_srs, mac_ring_t *tx_ring)
3989 {
3990 	int i;
3991 	mac_soft_ring_t *soft_ring, *remove_sring;
3992 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
3993 
3994 	mutex_enter(&mac_srs->srs_lock);
3995 	for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
3996 		soft_ring =  mac_srs->srs_tx_soft_rings[i];
3997 		if (soft_ring->s_ring_tx_arg2 == tx_ring)
3998 			break;
3999 	}
4000 	mutex_exit(&mac_srs->srs_lock);
4001 	ASSERT(i < mac_srs->srs_tx_ring_count);
4002 	remove_sring = soft_ring;
4003 	/*
4004 	 * In the case of aggr, the soft ring associated with a Tx ring
4005 	 * is also stored in st_soft_rings[] array. That entry should
4006 	 * be removed.
4007 	 */
4008 	if (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT) {
4009 		mac_srs_tx_t *tx = &mac_srs->srs_tx;
4010 
4011 		ASSERT(tx->st_soft_rings[tx_ring->mr_index] == remove_sring);
4012 		tx->st_soft_rings[tx_ring->mr_index] = NULL;
4013 	}
4014 	mac_soft_ring_remove(mac_srs, remove_sring);
4015 	mac_srs_update_fanout_list(mac_srs);
4016 }
4017 
4018 /*
4019  * mac_tx_srs_setup():
4020  * Used to setup Tx rings. If no free Tx ring is available, then default
4021  * Tx ring is used.
4022  */
4023 void
4024 mac_tx_srs_setup(mac_client_impl_t *mcip, flow_entry_t *flent)
4025 {
4026 	mac_impl_t		*mip = mcip->mci_mip;
4027 	mac_soft_ring_set_t	*tx_srs = flent->fe_tx_srs;
4028 	int			i;
4029 	int			tx_ring_count = 0;
4030 	mac_group_t		*grp = NULL;
4031 	mac_ring_t		*ring;
4032 	mac_srs_tx_t		*tx = &tx_srs->srs_tx;
4033 	boolean_t		is_aggr;
4034 	uint_t			ring_info = 0;
4035 
4036 	is_aggr = (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT) != 0;
4037 	grp = flent->fe_tx_ring_group;
4038 	if (grp == NULL) {
4039 		ring = (mac_ring_t *)mip->mi_default_tx_ring;
4040 		goto no_group;
4041 	}
4042 	tx_ring_count = grp->mrg_cur_count;
4043 	ring = grp->mrg_rings;
4044 	/*
4045 	 * An attempt is made to reserve 'tx_ring_count' number
4046 	 * of Tx rings. If tx_ring_count is 0, default Tx ring
4047 	 * is used. If it is 1, an attempt is made to reserve one
4048 	 * Tx ring. In both the cases, the ring information is
4049 	 * stored in Tx SRS. If multiple Tx rings are specified,
4050 	 * then each Tx ring will have a Tx-side soft ring. All
4051 	 * these soft rings will be hang off Tx SRS.
4052 	 */
4053 	switch (grp->mrg_state) {
4054 		case MAC_GROUP_STATE_SHARED:
4055 		case MAC_GROUP_STATE_RESERVED:
4056 			if (tx_ring_count <= 1 && !is_aggr) {
4057 no_group:
4058 				if (ring != NULL &&
4059 				    ring->mr_state != MR_INUSE) {
4060 					(void) mac_start_ring(ring);
4061 					ring_info = mac_hwring_getinfo(
4062 					    (mac_ring_handle_t)ring);
4063 				}
4064 				tx->st_arg2 = (void *)ring;
4065 				mac_tx_srs_stat_recreate(tx_srs, B_FALSE);
4066 				if (mac_srs_is_bw_controlled(tx_srs)) {
4067 					tx->st_mode = SRS_TX_BW;
4068 				} else if (mac_tx_serialize ||
4069 				    (ring_info & MAC_RING_TX_SERIALIZE)) {
4070 					tx->st_mode = SRS_TX_SERIALIZE;
4071 				} else {
4072 					tx->st_mode = SRS_TX_DEFAULT;
4073 				}
4074 				break;
4075 			}
4076 			if (mac_srs_is_bw_controlled(tx_srs)) {
4077 				tx->st_mode = is_aggr ?
4078 				    SRS_TX_BW_AGGR : SRS_TX_BW_FANOUT;
4079 			} else {
4080 				tx->st_mode = is_aggr ? SRS_TX_AGGR :
4081 				    SRS_TX_FANOUT;
4082 			}
4083 			for (i = 0; i < tx_ring_count; i++) {
4084 				VERIFY(ring != NULL);
4085 				mac_soft_ring_state_t soft_ring_type = 0;
4086 
4087 				switch (ring->mr_state) {
4088 				case MR_INUSE:
4089 				case MR_FREE:
4090 					VERIFY3P(ring->mr_srs, ==, NULL);
4091 
4092 					if (ring->mr_state != MR_INUSE)
4093 						(void) mac_start_ring(ring);
4094 					ring_info = mac_hwring_getinfo(
4095 					    (mac_ring_handle_t)ring);
4096 					if (mac_tx_serialize || (ring_info &
4097 					    MAC_RING_TX_SERIALIZE)) {
4098 						soft_ring_type |=
4099 						    ST_RING_WORKER_ONLY;
4100 					}
4101 					(void) mac_soft_ring_create_tx(i, 0,
4102 					    soft_ring_type, maxclsyspri,
4103 					    mcip, tx_srs, -1, ring);
4104 					break;
4105 				default:
4106 					cmn_err(CE_PANIC,
4107 					    "srs_setup: mcip = %p "
4108 					    "trying to add UNKNOWN ring = %p\n",
4109 					    (void *)mcip, (void *)ring);
4110 					break;
4111 				}
4112 				ring = ring->mr_next;
4113 			}
4114 			mac_srs_update_fanout_list(tx_srs);
4115 			break;
4116 		default:
4117 			ASSERT(B_FALSE);
4118 			break;
4119 	}
4120 	tx->st_func = mac_tx_get_func(tx->st_mode);
4121 	if (is_aggr) {
4122 		VERIFY(i_mac_capab_get((mac_handle_t)mip,
4123 		    MAC_CAPAB_AGGR, &tx->st_capab_aggr));
4124 	}
4125 	DTRACE_PROBE3(tx__srs___setup__return, mac_soft_ring_set_t *, tx_srs,
4126 	    mac_tx_srs_mode_t, tx->st_mode, int, tx_srs->srs_tx_ring_count);
4127 }
4128 
4129 /*
4130  * Update the fanout of a client if its recorded link speed doesn't match
4131  * its current link speed.
4132  */
4133 void
4134 mac_fanout_recompute_client(mac_client_impl_t *mcip, cpupart_t *cpupart)
4135 {
4136 	uint64_t link_speed;
4137 	mac_resource_props_t *mcip_mrp;
4138 	flow_entry_t *flent = mcip->mci_flent;
4139 	mac_soft_ring_set_t *rx_srs;
4140 	mac_cpus_t *srs_cpu;
4141 	int soft_ring_count, maxcpus;
4142 
4143 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4144 
4145 	link_speed = mac_client_stat_get(
4146 	    (mac_client_handle_t)mcip->mci_flent->fe_mcip, MAC_STAT_IFSPEED);
4147 
4148 	if ((link_speed != 0) &&
4149 	    (link_speed != mcip->mci_flent->fe_nic_speed)) {
4150 		mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
4151 		/*
4152 		 * Before calling mac_fanout_setup(), check to see if
4153 		 * the SRSes already have the right number of soft
4154 		 * rings. mac_fanout_setup() is a heavy duty operation
4155 		 * where new cpu bindings are done for SRS and soft
4156 		 * ring threads and interrupts re-targeted.
4157 		 */
4158 		maxcpus = (cpupart != NULL) ? cpupart->cp_ncpus : ncpus;
4159 		soft_ring_count = mac_compute_soft_ring_count(flent,
4160 		    flent->fe_rx_srs_cnt - 1, maxcpus);
4161 		/*
4162 		 * If soft_ring_count returned by
4163 		 * mac_compute_soft_ring_count() is 0, bump it
4164 		 * up by 1 because we always have atleast one
4165 		 * TCP, UDP, and OTH soft ring associated with
4166 		 * an SRS.
4167 		 */
4168 		soft_ring_count = (soft_ring_count == 0) ?
4169 		    1 : soft_ring_count;
4170 		rx_srs = flent->fe_rx_srs[0];
4171 		srs_cpu = &rx_srs->srs_cpu;
4172 		if (soft_ring_count != srs_cpu->mc_rx_fanout_cnt) {
4173 			mac_fanout_setup(mcip, flent, mcip_mrp,
4174 			    mac_rx_deliver, mcip, cpupart);
4175 		}
4176 	}
4177 }
4178 
4179 /*
4180  * Walk through the list of MAC clients for the MAC.
4181  * For each active MAC client, recompute the number of soft rings
4182  * associated with every client, only if current speed is different
4183  * from the speed that was previously used for soft ring computation.
4184  * If the cable is disconnected whlie the NIC is started, we would get
4185  * notification with speed set to 0. We do not recompute in that case.
4186  */
4187 void
4188 mac_fanout_recompute(mac_impl_t *mip)
4189 {
4190 	mac_client_impl_t	*mcip;
4191 	cpupart_t		*cpupart;
4192 	boolean_t		use_default;
4193 	mac_resource_props_t	*mrp, *emrp;
4194 
4195 	i_mac_perim_enter(mip);
4196 	if ((mip->mi_state_flags & MIS_IS_VNIC) != 0 ||
4197 	    mip->mi_linkstate != LINK_STATE_UP) {
4198 		i_mac_perim_exit(mip);
4199 		return;
4200 	}
4201 
4202 	for (mcip = mip->mi_clients_list; mcip != NULL;
4203 	    mcip = mcip->mci_client_next) {
4204 		/* Aggr port clients don't have SRSes. */
4205 		if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) != 0)
4206 			continue;
4207 
4208 		if ((mcip->mci_state_flags & MCIS_SHARE_BOUND) != 0 ||
4209 		    !MCIP_DATAPATH_SETUP(mcip))
4210 			continue;
4211 		mrp = MCIP_RESOURCE_PROPS(mcip);
4212 		emrp = MCIP_EFFECTIVE_PROPS(mcip);
4213 		use_default = B_FALSE;
4214 		pool_lock();
4215 		cpupart = mac_pset_find(mrp, &use_default);
4216 		mac_fanout_recompute_client(mcip, cpupart);
4217 		mac_set_pool_effective(use_default, cpupart, mrp, emrp);
4218 		pool_unlock();
4219 	}
4220 
4221 	i_mac_perim_exit(mip);
4222 }
4223 
4224 /*
4225  * Given a MAC, change the polling state for all its MAC clients.  'enable' is
4226  * B_TRUE to enable polling or B_FALSE to disable.  Polling is enabled by
4227  * default.
4228  */
4229 void
4230 mac_poll_state_change(mac_handle_t mh, boolean_t enable)
4231 {
4232 	mac_impl_t *mip = (mac_impl_t *)mh;
4233 	mac_client_impl_t *mcip;
4234 
4235 	i_mac_perim_enter(mip);
4236 	if (enable)
4237 		mip->mi_state_flags &= ~MIS_POLL_DISABLE;
4238 	else
4239 		mip->mi_state_flags |= MIS_POLL_DISABLE;
4240 	for (mcip = mip->mi_clients_list; mcip != NULL;
4241 	    mcip = mcip->mci_client_next)
4242 		mac_client_update_classifier(mcip, B_TRUE);
4243 	i_mac_perim_exit(mip);
4244 }
4245