xref: /illumos-gate/usr/src/uts/common/io/mac/mac_datapath_setup.c (revision a2ecc5f71752edcbcac96b54ec9b3108d17cf6c2)
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  * Change the receive bandwidth limit.
1521  */
1522 static void
1523 mac_rx_srs_update_bwlimit(mac_soft_ring_set_t *srs, mac_resource_props_t *mrp)
1524 {
1525 	mac_soft_ring_t		*softring;
1526 
1527 	mutex_enter(&srs->srs_lock);
1528 	mutex_enter(&srs->srs_bw->mac_bw_lock);
1529 
1530 	if (mrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
1531 		/* Reset bandwidth limit */
1532 		if (srs->srs_type & SRST_BW_CONTROL) {
1533 			srs->srs_type &= ~SRST_BW_CONTROL;
1534 			srs->srs_drain_func = mac_rx_srs_drain;
1535 		}
1536 	} else {
1537 		/* Set/Modify bandwidth limit */
1538 		srs->srs_bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
1539 		/*
1540 		 * Give twice the queuing capability before
1541 		 * dropping packets. The unit is bytes/tick.
1542 		 */
1543 		srs->srs_bw->mac_bw_drop_threshold =
1544 		    srs->srs_bw->mac_bw_limit << 1;
1545 		if (!(srs->srs_type & SRST_BW_CONTROL)) {
1546 			srs->srs_type |= SRST_BW_CONTROL;
1547 			srs->srs_drain_func = mac_rx_srs_drain_bw;
1548 		}
1549 	}
1550 
1551 	mutex_exit(&srs->srs_bw->mac_bw_lock);
1552 	mutex_exit(&srs->srs_lock);
1553 }
1554 
1555 /* Change the transmit bandwidth limit */
1556 static void
1557 mac_tx_srs_update_bwlimit(mac_soft_ring_set_t *srs, mac_resource_props_t *mrp)
1558 {
1559 	uint32_t		tx_mode, ring_info = 0;
1560 	mac_srs_tx_t		*srs_tx = &srs->srs_tx;
1561 	mac_client_impl_t	*mcip = srs->srs_mcip;
1562 
1563 	/*
1564 	 * We need to quiesce/restart the client here because mac_tx() and
1565 	 * srs->srs_tx->st_func do not hold srs->srs_lock while accessing
1566 	 * st_mode and related fields, which are modified by the code below.
1567 	 */
1568 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
1569 
1570 	mutex_enter(&srs->srs_lock);
1571 	mutex_enter(&srs->srs_bw->mac_bw_lock);
1572 
1573 	tx_mode = srs_tx->st_mode;
1574 	if (mrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
1575 		/* Reset bandwidth limit */
1576 		if (tx_mode == SRS_TX_BW) {
1577 			if (srs_tx->st_arg2 != NULL) {
1578 				mac_ring_handle_t mrh =
1579 				    (mac_ring_handle_t)srs_tx->st_arg2;
1580 				ring_info = mac_hwring_getinfo(mrh);
1581 			}
1582 			if (mac_tx_serialize ||
1583 			    (ring_info & MAC_RING_TX_SERIALIZE)) {
1584 				srs_tx->st_mode = SRS_TX_SERIALIZE;
1585 			} else {
1586 				srs_tx->st_mode = SRS_TX_DEFAULT;
1587 			}
1588 		} else if (tx_mode == SRS_TX_BW_FANOUT) {
1589 			srs_tx->st_mode = SRS_TX_FANOUT;
1590 		} else if (tx_mode == SRS_TX_BW_AGGR) {
1591 			srs_tx->st_mode = SRS_TX_AGGR;
1592 		}
1593 		srs->srs_type &= ~SRST_BW_CONTROL;
1594 	} else {
1595 		/* Set/Modify bandwidth limit */
1596 		srs->srs_bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
1597 		/*
1598 		 * Give twice the queuing capability before
1599 		 * dropping packets. The unit is bytes/tick.
1600 		 */
1601 		srs->srs_bw->mac_bw_drop_threshold =
1602 		    srs->srs_bw->mac_bw_limit << 1;
1603 		srs->srs_type |= SRST_BW_CONTROL;
1604 		if (tx_mode != SRS_TX_BW && tx_mode != SRS_TX_BW_FANOUT &&
1605 		    tx_mode != SRS_TX_BW_AGGR) {
1606 			if (tx_mode == SRS_TX_SERIALIZE ||
1607 			    tx_mode == SRS_TX_DEFAULT) {
1608 				srs_tx->st_mode = SRS_TX_BW;
1609 			} else if (tx_mode == SRS_TX_FANOUT) {
1610 				srs_tx->st_mode = SRS_TX_BW_FANOUT;
1611 			} else if (tx_mode == SRS_TX_AGGR) {
1612 				srs_tx->st_mode = SRS_TX_BW_AGGR;
1613 			} else {
1614 				ASSERT(0);
1615 			}
1616 		}
1617 	}
1618 
1619 	srs_tx->st_func = mac_tx_get_func(srs_tx->st_mode);
1620 	mutex_exit(&srs->srs_bw->mac_bw_lock);
1621 	mutex_exit(&srs->srs_lock);
1622 
1623 	mac_tx_client_restart((mac_client_handle_t)mcip);
1624 }
1625 
1626 /*
1627  * The uber function that deals with any update to bandwidth limits.
1628  */
1629 void
1630 mac_srs_update_bwlimit(flow_entry_t *flent, mac_resource_props_t *mrp)
1631 {
1632 	int			count;
1633 
1634 	for (count = 0; count < flent->fe_rx_srs_cnt; count++)
1635 		mac_rx_srs_update_bwlimit(flent->fe_rx_srs[count], mrp);
1636 	mac_tx_srs_update_bwlimit(flent->fe_tx_srs, mrp);
1637 }
1638 
1639 /*
1640  * When the first sub-flow is added to a link, we disable polling on the
1641  * link and also modify the entry point to mac_rx_srs_subflow_process().
1642  * (polling is disabled because with the subflow added, accounting
1643  * for polling needs additional logic, it is assumed that when a subflow is
1644  * added, we can take some hit as a result of disabling polling rather than
1645  * adding more complexity - if this becomes a perf. issue we need to
1646  * re-rvaluate this logic).  When the last subflow is removed, we turn back
1647  * polling and also reset the entry point to mac_rx_srs_process().
1648  *
1649  * In the future if there are multiple SRS, we can simply
1650  * take one and give it to the flow rather than disabling polling and
1651  * resetting the entry point.
1652  */
1653 void
1654 mac_client_update_classifier(mac_client_impl_t *mcip, boolean_t enable)
1655 {
1656 	flow_entry_t		*flent = mcip->mci_flent;
1657 	int			i;
1658 	mac_impl_t		*mip = mcip->mci_mip;
1659 	mac_rx_func_t		rx_func;
1660 	uint_t			rx_srs_cnt;
1661 	boolean_t		enable_classifier;
1662 
1663 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1664 
1665 	enable_classifier = !FLOW_TAB_EMPTY(mcip->mci_subflow_tab) && enable;
1666 
1667 	rx_func = enable_classifier ? mac_rx_srs_subflow_process :
1668 	    mac_rx_srs_process;
1669 
1670 	/* Tell mac_srs_poll_state_change to disable polling if necessary */
1671 	if (mip->mi_state_flags & MIS_POLL_DISABLE)
1672 		enable_classifier = B_TRUE;
1673 
1674 	/*
1675 	 * If receive function has already been configured correctly for
1676 	 * current subflow configuration, do nothing.
1677 	 */
1678 	if (flent->fe_cb_fn == (flow_fn_t)rx_func)
1679 		return;
1680 
1681 	rx_srs_cnt = flent->fe_rx_srs_cnt;
1682 	for (i = 0; i < rx_srs_cnt; i++) {
1683 		ASSERT(flent->fe_rx_srs[i] != NULL);
1684 		mac_srs_poll_state_change(flent->fe_rx_srs[i],
1685 		    enable_classifier, rx_func);
1686 	}
1687 
1688 	/*
1689 	 * Change the S/W classifier so that we can land in the
1690 	 * correct processing function with correct argument.
1691 	 * If all subflows have been removed we can revert to
1692 	 * mac_rx_srs_process(), else we need mac_rx_srs_subflow_process().
1693 	 */
1694 	mutex_enter(&flent->fe_lock);
1695 	flent->fe_cb_fn = (flow_fn_t)rx_func;
1696 	flent->fe_cb_arg1 = (void *)mip;
1697 	flent->fe_cb_arg2 = flent->fe_rx_srs[0];
1698 	mutex_exit(&flent->fe_lock);
1699 }
1700 
1701 static void
1702 mac_srs_update_fanout_list(mac_soft_ring_set_t *mac_srs)
1703 {
1704 	int tcp_count = 0, tcp6_count = 0, udp_count = 0, udp6_count = 0,
1705 	    oth_count = 0, tx_count = 0;
1706 
1707 	mac_soft_ring_t *softring;
1708 
1709 	softring = mac_srs->srs_soft_ring_head;
1710 	if (softring == NULL) {
1711 		ASSERT(mac_srs->srs_soft_ring_count == 0);
1712 		mac_srs->srs_tcp_ring_count = 0;
1713 		mac_srs->srs_udp_ring_count = 0;
1714 		mac_srs->srs_tcp6_ring_count = 0;
1715 		mac_srs->srs_udp6_ring_count = 0;
1716 		mac_srs->srs_oth_ring_count = 0;
1717 		mac_srs->srs_tx_ring_count = 0;
1718 
1719 		/*
1720 		 * `SRST_NO_SOFT_RINGS` is a static property of Rx SRSes, and
1721 		 * determines their processing model. Adjust this only on Tx
1722 		 * SRSes, where its meaning is something of a vanity flag.
1723 		 */
1724 		if ((mac_srs->srs_type & SRST_TX) != 0) {
1725 			mac_srs->srs_type |= SRST_NO_SOFT_RINGS;
1726 		}
1727 
1728 		return;
1729 	}
1730 
1731 	if ((mac_srs->srs_type & SRST_TX) != 0) {
1732 		mac_srs->srs_type &= ~SRST_NO_SOFT_RINGS;
1733 	}
1734 
1735 	while (softring != NULL) {
1736 		if (softring->s_ring_state & ST_RING_TCP) {
1737 			mac_srs->srs_tcp_soft_rings[tcp_count++] = softring;
1738 		} else if (softring->s_ring_state & ST_RING_TCP6) {
1739 			mac_srs->srs_tcp6_soft_rings[tcp6_count++] = softring;
1740 		} else if (softring->s_ring_state & ST_RING_UDP) {
1741 			mac_srs->srs_udp_soft_rings[udp_count++] = softring;
1742 		} else if (softring->s_ring_state & ST_RING_UDP6) {
1743 			mac_srs->srs_udp6_soft_rings[udp6_count++] = softring;
1744 		} else if (softring->s_ring_state & ST_RING_OTH) {
1745 			mac_srs->srs_oth_soft_rings[oth_count++] = softring;
1746 		} else {
1747 			ASSERT(softring->s_ring_state & ST_RING_TX);
1748 			mac_srs->srs_tx_soft_rings[tx_count++] = softring;
1749 		}
1750 		softring = softring->s_ring_next;
1751 	}
1752 
1753 	ASSERT(mac_srs->srs_soft_ring_count == (tcp_count + tcp6_count +
1754 	    udp_count + udp6_count + oth_count + tx_count));
1755 	mac_srs->srs_tcp_ring_count = tcp_count;
1756 	mac_srs->srs_tcp6_ring_count = tcp6_count;
1757 	mac_srs->srs_udp_ring_count = udp_count;
1758 	mac_srs->srs_udp6_ring_count = udp6_count;
1759 	mac_srs->srs_oth_ring_count = oth_count;
1760 	mac_srs->srs_tx_ring_count = tx_count;
1761 }
1762 
1763 static void
1764 mac_srs_create_proto_softrings(int id, pri_t pri, mac_client_impl_t *mcip,
1765     mac_soft_ring_set_t *mac_srs, processorid_t cpuid, mac_direct_rx_t rx_func,
1766     void *x_arg1, boolean_t set_bypass)
1767 {
1768 	mac_soft_ring_t	*softring;
1769 
1770 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1771 	    ST_RING_TCP, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1772 
1773 	/*
1774 	 * TCP and UDP support DLS bypass. In addition TCP
1775 	 * squeue can also poll their corresponding soft rings.
1776 	 */
1777 	if (set_bypass && mcip->mci_direct_rx.mdrx_v4 != NULL &&
1778 	    (mcip->mci_rcb4.mrc_arg != NULL)) {
1779 		/*
1780 		 * Make a call in IP to get a TCP squeue assigned to
1781 		 * this softring to maintain full CPU locality through
1782 		 * the stack and allow the squeue to be able to poll
1783 		 * the softring so the flow control can be pushed
1784 		 * all the way to H/W.
1785 		 */
1786 		mac_soft_ring_poll_enable(softring, mcip->mci_direct_rx.mdrx_v4,
1787 		    mcip->mci_direct_rx.mdrx_arg_v4, &mcip->mci_rcb4, pri);
1788 	}
1789 
1790 	/*
1791 	 * Non-TCP protocols don't support squeues. Hence we
1792 	 * don't make any ring addition callbacks for non-TCP
1793 	 * rings. Now create the UDP softring and allow it to
1794 	 * bypass the DLS layer.
1795 	 */
1796 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1797 	    ST_RING_UDP, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1798 
1799 	if (set_bypass && mcip->mci_direct_rx.mdrx_v4 != NULL) {
1800 		mac_soft_ring_dls_bypass_enable(softring,
1801 		    mcip->mci_direct_rx.mdrx_v4,
1802 		    mcip->mci_direct_rx.mdrx_arg_v4);
1803 	}
1804 
1805 	/* TCP for IPv6. */
1806 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1807 	    ST_RING_TCP6, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1808 
1809 	if (set_bypass && mcip->mci_direct_rx.mdrx_v6 != NULL &&
1810 	    (mcip->mci_rcb6.mrc_arg != NULL)) {
1811 		mac_soft_ring_poll_enable(softring, mcip->mci_direct_rx.mdrx_v6,
1812 		    mcip->mci_direct_rx.mdrx_arg_v6, &mcip->mci_rcb6, pri);
1813 	}
1814 
1815 	/* UDP for IPv6. */
1816 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1817 	    ST_RING_UDP6, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1818 	softring->s_ring_rx_arg2 = NULL;
1819 
1820 	if (set_bypass && mcip->mci_direct_rx.mdrx_v6 != NULL) {
1821 		mac_soft_ring_dls_bypass_enable(softring,
1822 		    mcip->mci_direct_rx.mdrx_v6,
1823 		    mcip->mci_direct_rx.mdrx_arg_v6);
1824 	}
1825 
1826 	/* Create the Oth softrings which has to go through the DLS. */
1827 	softring = mac_soft_ring_create_rx(id, mac_soft_ring_worker_wait,
1828 	    ST_RING_OTH, pri, mcip, mac_srs, cpuid, rx_func, x_arg1);
1829 }
1830 
1831 /*
1832  * This routine associates a CPU or a set of CPU to process incoming
1833  * traffic from a mac client. If multiple CPUs are specified, then
1834  * so many soft rings are created with each soft ring worker thread
1835  * bound to a CPU in the set. Each soft ring in turn will be
1836  * associated with an squeue and the squeue will be moved to the
1837  * same CPU as that of the soft ring's.
1838  */
1839 static void
1840 mac_srs_fanout_modify(mac_client_impl_t *mcip, mac_direct_rx_t rx_func,
1841     void *x_arg1, mac_soft_ring_set_t *mac_rx_srs,
1842     mac_soft_ring_set_t *mac_tx_srs)
1843 {
1844 	mac_soft_ring_t *softring;
1845 	processorid_t cpuid = -1;
1846 	int i, srings_present, new_fanout_cnt;
1847 	mac_cpus_t *srs_cpu;
1848 
1849 	/* fanout state is REINIT. Set it back to INIT */
1850 	ASSERT(mac_rx_srs->srs_fanout_state == SRS_FANOUT_REINIT);
1851 	mac_rx_srs->srs_fanout_state = SRS_FANOUT_INIT;
1852 
1853 	/* how many are present right now */
1854 	srings_present = mac_rx_srs->srs_tcp_ring_count;
1855 	/* new request */
1856 	srs_cpu = &mac_rx_srs->srs_cpu;
1857 	new_fanout_cnt = srs_cpu->mc_rx_fanout_cnt;
1858 
1859 	if (new_fanout_cnt > srings_present) {
1860 		/* soft rings increased */
1861 		mutex_enter(&mac_rx_srs->srs_lock);
1862 		mac_rx_srs->srs_type |= SRST_FANOUT_SRC_IP;
1863 		mutex_exit(&mac_rx_srs->srs_lock);
1864 
1865 		for (i = mac_rx_srs->srs_tcp_ring_count;
1866 		    i < new_fanout_cnt; i++) {
1867 			/*
1868 			 * Create the protocol softrings and set the
1869 			 * DLS bypass where possible.
1870 			 */
1871 			mac_srs_create_proto_softrings(i, mac_rx_srs->srs_pri,
1872 			    mcip, mac_rx_srs, cpuid, rx_func, x_arg1, B_TRUE);
1873 		}
1874 		mac_srs_update_fanout_list(mac_rx_srs);
1875 	} else if (new_fanout_cnt < srings_present) {
1876 		/* soft rings decreased */
1877 		if (new_fanout_cnt == 1) {
1878 			mutex_enter(&mac_rx_srs->srs_lock);
1879 			mac_rx_srs->srs_type &= ~SRST_FANOUT_SRC_IP;
1880 			ASSERT(mac_rx_srs->srs_type & SRST_FANOUT_PROTO);
1881 			mutex_exit(&mac_rx_srs->srs_lock);
1882 		}
1883 		/* Get rid of extra soft rings */
1884 		for (i = new_fanout_cnt;
1885 		    i < mac_rx_srs->srs_tcp_ring_count; i++) {
1886 			softring = mac_rx_srs->srs_tcp_soft_rings[i];
1887 			if (softring->s_ring_rx_arg2 != NULL) {
1888 				mcip->mci_rcb4.mrc_remove(
1889 				    mcip->mci_rcb4.mrc_arg,
1890 				    softring->s_ring_rx_arg2);
1891 			}
1892 			softring = mac_rx_srs->srs_tcp6_soft_rings[i];
1893 			if (softring->s_ring_rx_arg2 != NULL) {
1894 				mcip->mci_rcb6.mrc_remove(
1895 				    mcip->mci_rcb6.mrc_arg,
1896 				    softring->s_ring_rx_arg2);
1897 			}
1898 			mac_soft_ring_remove(mac_rx_srs,
1899 			    mac_rx_srs->srs_tcp_soft_rings[i]);
1900 			mac_soft_ring_remove(mac_rx_srs,
1901 			    mac_rx_srs->srs_tcp6_soft_rings[i]);
1902 			mac_soft_ring_remove(mac_rx_srs,
1903 			    mac_rx_srs->srs_udp_soft_rings[i]);
1904 			mac_soft_ring_remove(mac_rx_srs,
1905 			    mac_rx_srs->srs_udp6_soft_rings[i]);
1906 			mac_soft_ring_remove(mac_rx_srs,
1907 			    mac_rx_srs->srs_oth_soft_rings[i]);
1908 		}
1909 		mac_srs_update_fanout_list(mac_rx_srs);
1910 	}
1911 
1912 	ASSERT(new_fanout_cnt == mac_rx_srs->srs_tcp_ring_count);
1913 	mutex_enter(&cpu_lock);
1914 	for (i = 0; i < mac_rx_srs->srs_tcp_ring_count; i++) {
1915 		cpuid = srs_cpu->mc_rx_fanout_cpus[i];
1916 		(void) mac_soft_ring_bind(mac_rx_srs->srs_udp_soft_rings[i],
1917 		    cpuid);
1918 		(void) mac_soft_ring_bind(mac_rx_srs->srs_udp6_soft_rings[i],
1919 		    cpuid);
1920 		(void) mac_soft_ring_bind(mac_rx_srs->srs_oth_soft_rings[i],
1921 		    cpuid);
1922 		(void) mac_soft_ring_bind(mac_rx_srs->srs_tcp_soft_rings[i],
1923 		    cpuid);
1924 		(void) mac_soft_ring_bind(mac_rx_srs->srs_tcp6_soft_rings[i],
1925 		    cpuid);
1926 		softring = mac_rx_srs->srs_tcp_soft_rings[i];
1927 		if (softring->s_ring_rx_arg2 != NULL) {
1928 			mcip->mci_rcb4.mrc_bind(mcip->mci_rcb4.mrc_arg,
1929 			    softring->s_ring_rx_arg2, cpuid);
1930 		}
1931 		softring = mac_rx_srs->srs_tcp6_soft_rings[i];
1932 		if (softring->s_ring_rx_arg2 != NULL) {
1933 			mcip->mci_rcb6.mrc_bind(mcip->mci_rcb6.mrc_arg,
1934 			    softring->s_ring_rx_arg2, cpuid);
1935 		}
1936 	}
1937 
1938 	mac_srs_worker_bind(mac_rx_srs, srs_cpu->mc_rx_workerid);
1939 	mac_srs_poll_bind(mac_rx_srs, srs_cpu->mc_rx_pollid);
1940 	mac_rx_srs_retarget_intr(mac_rx_srs, srs_cpu->mc_rx_intr_cpu);
1941 	/*
1942 	 * Bind Tx srs and soft ring threads too. Let's bind tx
1943 	 * srs to the last cpu in mrp list.
1944 	 */
1945 	if (mac_tx_srs != NULL) {
1946 		BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp);
1947 		mac_tx_srs_retarget_intr(mac_tx_srs);
1948 	}
1949 	mutex_exit(&cpu_lock);
1950 }
1951 
1952 /*
1953  * Bind SRS threads and soft rings to CPUs/create fanout list.
1954  */
1955 void
1956 mac_srs_fanout_init(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
1957     mac_direct_rx_t rx_func, void *x_arg1, mac_soft_ring_set_t *mac_rx_srs,
1958     mac_soft_ring_set_t *mac_tx_srs, cpupart_t *cpupart)
1959 {
1960 	int		i;
1961 	processorid_t	cpuid;
1962 	int soft_ring_cnt;
1963 	mac_cpus_t *srs_cpu = &mac_rx_srs->srs_cpu;
1964 
1965 	/*
1966 	 * Remove the no soft ring flag and we will adjust it
1967 	 * appropriately further down.
1968 	 */
1969 	mutex_enter(&mac_rx_srs->srs_lock);
1970 	mac_rx_srs->srs_type &= ~SRST_NO_SOFT_RINGS;
1971 	mutex_exit(&mac_rx_srs->srs_lock);
1972 
1973 	ASSERT(mac_rx_srs->srs_soft_ring_head == NULL);
1974 	ASSERT(mac_rx_srs->srs_fanout_state == SRS_FANOUT_UNINIT);
1975 	mac_rx_srs->srs_fanout_state = SRS_FANOUT_INIT;
1976 	/*
1977 	 * Ring count can be 0 if no fanout is required and no cpu
1978 	 * were specified. Leave the SRS worker and poll thread
1979 	 * unbound
1980 	 */
1981 	ASSERT(mrp != NULL);
1982 	soft_ring_cnt = srs_cpu->mc_rx_fanout_cnt;
1983 
1984 	/* Step 1: bind cpu contains cpu list where threads need to bind */
1985 	if (soft_ring_cnt > 0) {
1986 		mutex_enter(&cpu_lock);
1987 		for (i = 0; i < soft_ring_cnt; i++) {
1988 			cpuid = srs_cpu->mc_rx_fanout_cpus[i];
1989 			/* Create the protocol softrings */
1990 			mac_srs_create_proto_softrings(i, mac_rx_srs->srs_pri,
1991 			    mcip, mac_rx_srs, cpuid, rx_func, x_arg1, B_FALSE);
1992 		}
1993 		mac_srs_worker_bind(mac_rx_srs, srs_cpu->mc_rx_workerid);
1994 		mac_srs_poll_bind(mac_rx_srs, srs_cpu->mc_rx_pollid);
1995 		mac_rx_srs_retarget_intr(mac_rx_srs, srs_cpu->mc_rx_intr_cpu);
1996 		/*
1997 		 * Bind Tx srs and soft ring threads too.
1998 		 * Let's bind tx srs to the last cpu in
1999 		 * mrp list.
2000 		 */
2001 		if (mac_tx_srs == NULL) {
2002 			mutex_exit(&cpu_lock);
2003 			goto alldone;
2004 		}
2005 
2006 		BIND_TX_SRS_AND_SOFT_RINGS(mac_tx_srs, mrp);
2007 		mac_tx_srs_retarget_intr(mac_tx_srs);
2008 		mutex_exit(&cpu_lock);
2009 	} else {
2010 		mutex_enter(&cpu_lock);
2011 		/*
2012 		 * For a subflow, mrp_workerid and mrp_pollid
2013 		 * is not set.
2014 		 */
2015 		mac_srs_worker_bind(mac_rx_srs, mrp->mrp_rx_workerid);
2016 		mac_srs_poll_bind(mac_rx_srs, mrp->mrp_rx_pollid);
2017 		mutex_exit(&cpu_lock);
2018 		goto no_softrings;
2019 	}
2020 
2021 alldone:
2022 	if (soft_ring_cnt > 1)
2023 		mac_rx_srs->srs_type |= SRST_FANOUT_SRC_IP;
2024 	mac_srs_update_fanout_list(mac_rx_srs);
2025 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_FALSE);
2026 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_TRUE);
2027 	return;
2028 
2029 no_softrings:
2030 	if (mac_rx_srs->srs_type & SRST_FANOUT_PROTO) {
2031 		mutex_enter(&cpu_lock);
2032 		cpuid = mac_next_bind_cpu(cpupart);
2033 		/* Create the protocol softrings */
2034 		mac_srs_create_proto_softrings(0, mac_rx_srs->srs_pri, mcip,
2035 		    mac_rx_srs, cpuid, rx_func, x_arg1, B_FALSE);
2036 		mutex_exit(&cpu_lock);
2037 	} else {
2038 		/*
2039 		 * This is the case when there is no fanout which is
2040 		 * true for subflows.
2041 		 */
2042 		mac_rx_srs->srs_type |= SRST_NO_SOFT_RINGS;
2043 	}
2044 	mac_srs_update_fanout_list(mac_rx_srs);
2045 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_FALSE);
2046 	mac_srs_client_poll_enable(mcip, mac_rx_srs, B_TRUE);
2047 }
2048 
2049 /*
2050  * Calls mac_srs_fanout_init() or modify() depending upon whether
2051  * the SRS is getting initialized or re-initialized.
2052  */
2053 void
2054 mac_fanout_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2055     mac_resource_props_t *mrp, mac_direct_rx_t rx_func, void *x_arg1,
2056     cpupart_t *cpupart)
2057 {
2058 	mac_soft_ring_set_t *mac_rx_srs, *mac_tx_srs;
2059 	int i, rx_srs_cnt;
2060 
2061 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2062 
2063 	/*
2064 	 * Aggr ports do not have SRSes. This function should never be
2065 	 * called on an aggr port.
2066 	 */
2067 	ASSERT3U((mcip->mci_state_flags & MCIS_IS_AGGR_PORT), ==, 0);
2068 	mac_rx_srs = flent->fe_rx_srs[0];
2069 
2070 	/*
2071 	 * Set up the fanout on the tx side only once, with the
2072 	 * first rx SRS. The CPU binding, fanout, and bandwidth
2073 	 * criteria are common to both RX and TX, so
2074 	 * initializing them along side avoids redundant code.
2075 	 */
2076 	mac_tx_srs = flent->fe_tx_srs;
2077 	rx_srs_cnt = flent->fe_rx_srs_cnt;
2078 
2079 	/* No fanout for subflows */
2080 	if (flent->fe_type & FLOW_USER) {
2081 		mac_srs_fanout_init(mcip, mrp, rx_func,
2082 		    x_arg1, mac_rx_srs, mac_tx_srs, cpupart);
2083 		return;
2084 	}
2085 
2086 	if (mrp->mrp_mask & MRP_CPUS_USERSPEC)
2087 		mac_flow_user_cpu_init(flent, mrp);
2088 	else
2089 		mac_flow_cpu_init(flent, cpupart);
2090 
2091 	mrp->mrp_rx_fanout_cnt = mac_rx_srs->srs_cpu.mc_rx_fanout_cnt;
2092 
2093 	/*
2094 	 * Set up fanout for both SW (0th SRS) and HW classified
2095 	 * SRS (the rest of Rx SRSs in flent).
2096 	 */
2097 	for (i = 0; i < rx_srs_cnt; i++) {
2098 		mac_rx_srs = flent->fe_rx_srs[i];
2099 		if (i != 0)
2100 			mac_tx_srs = NULL;
2101 		switch (mac_rx_srs->srs_fanout_state) {
2102 		case SRS_FANOUT_UNINIT:
2103 			mac_srs_fanout_init(mcip, mrp, rx_func, x_arg1,
2104 			    mac_rx_srs, mac_tx_srs, cpupart);
2105 			break;
2106 		case SRS_FANOUT_INIT:
2107 			break;
2108 		case SRS_FANOUT_REINIT:
2109 			mac_rx_srs_quiesce(mac_rx_srs, SRS_QUIESCE);
2110 			mac_srs_fanout_modify(mcip, rx_func, x_arg1, mac_rx_srs,
2111 			    mac_tx_srs);
2112 			mac_rx_srs_restart(mac_rx_srs);
2113 			break;
2114 		default:
2115 			VERIFY(mac_rx_srs->srs_fanout_state <=
2116 			    SRS_FANOUT_REINIT);
2117 			break;
2118 		}
2119 	}
2120 }
2121 
2122 /*
2123  * Create a mac_soft_ring_set_t (SRS). If soft_ring_fanout_type is
2124  * SRST_TX, an SRS for Tx side is created. Otherwise an SRS for Rx side
2125  * processing is created.
2126  *
2127  * Details on Rx SRS:
2128  * Create a SRS and also add the necessary soft rings for TCP and
2129  * non-TCP based on fanout type and count specified.
2130  *
2131  * mac_soft_ring_fanout, mac_srs_fanout_modify (?),
2132  * mac_soft_ring_stop_workers, mac_soft_ring_set_destroy, etc need
2133  * to be heavily modified.
2134  *
2135  * mi_soft_ring_list_size, mi_soft_ring_size, etc need to disappear.
2136  */
2137 static mac_soft_ring_set_t *
2138 mac_srs_create(mac_client_impl_t *mcip, flow_entry_t *flent,
2139     const mac_soft_ring_set_type_t srs_type, mac_direct_rx_t rx_func,
2140     mac_ring_t *ring)
2141 {
2142 	mac_soft_ring_set_t	*mac_srs;
2143 	mac_srs_rx_t		*srs_rx;
2144 	mac_srs_tx_t		*srs_tx;
2145 	mac_bw_ctl_t		*mac_bw;
2146 	mac_resource_props_t	*mrp;
2147 	boolean_t		is_tx_srs = ((srs_type & SRST_TX) != 0);
2148 
2149 	mac_srs = kmem_cache_alloc(mac_srs_cache, KM_SLEEP);
2150 	bzero(mac_srs, sizeof (mac_soft_ring_set_t));
2151 	srs_rx = &mac_srs->srs_rx;
2152 	srs_tx = &mac_srs->srs_tx;
2153 
2154 	mutex_enter(&flent->fe_lock);
2155 
2156 	/*
2157 	 * Get the bandwidth control structure from the flent. Get
2158 	 * rid of any residual values in the control structure for
2159 	 * the tx bw struct and also for the rx, if the rx srs is
2160 	 * the 1st one being brought up (the rx bw ctl struct may
2161 	 * be shared by multiple SRSs)
2162 	 */
2163 	if (is_tx_srs) {
2164 		mac_srs->srs_bw = &flent->fe_tx_bw;
2165 		bzero(mac_srs->srs_bw, sizeof (mac_bw_ctl_t));
2166 		flent->fe_tx_srs = mac_srs;
2167 	} else {
2168 		/*
2169 		 * The bw counter (stored in the flent) is shared
2170 		 * by SRS's within an rx group.
2171 		 */
2172 		mac_srs->srs_bw = &flent->fe_rx_bw;
2173 		/* First rx SRS, clear the bw structure */
2174 		if (flent->fe_rx_srs_cnt == 0)
2175 			bzero(mac_srs->srs_bw, sizeof (mac_bw_ctl_t));
2176 
2177 		/*
2178 		 * It is better to panic here rather than just assert because
2179 		 * on a non-debug kernel we might end up courrupting memory
2180 		 * and making it difficult to debug.
2181 		 */
2182 		if (flent->fe_rx_srs_cnt >= MAX_RINGS_PER_GROUP) {
2183 			panic("Array Overrun detected due to MAC client %p "
2184 			    " having more rings than %d", (void *)mcip,
2185 			    MAX_RINGS_PER_GROUP);
2186 		}
2187 		flent->fe_rx_srs[flent->fe_rx_srs_cnt] = mac_srs;
2188 		flent->fe_rx_srs_cnt++;
2189 	}
2190 	mac_srs->srs_flent = flent;
2191 	mutex_exit(&flent->fe_lock);
2192 
2193 	mac_srs->srs_state = 0;
2194 	mac_srs->srs_type = (srs_type | SRST_NO_SOFT_RINGS);
2195 	mac_srs->srs_worker_cpuid = mac_srs->srs_worker_cpuid_save = -1;
2196 	mac_srs->srs_poll_cpuid = mac_srs->srs_poll_cpuid_save = -1;
2197 	mac_srs->srs_mcip = mcip;
2198 	mac_srs_fanout_list_alloc(mac_srs);
2199 
2200 	/*
2201 	 * For a flow we use the underlying MAC client's priority range with
2202 	 * the priority value to find an absolute priority value. For a MAC
2203 	 * client we use the MAC client's maximum priority as the value.
2204 	 */
2205 	mrp = &flent->fe_effective_props;
2206 	if ((mac_srs->srs_type & SRST_FLOW) != 0) {
2207 		mac_srs->srs_pri = FLOW_PRIORITY(mcip->mci_min_pri,
2208 		    mcip->mci_max_pri, mrp->mrp_priority);
2209 	} else {
2210 		mac_srs->srs_pri = mcip->mci_max_pri;
2211 	}
2212 	/*
2213 	 * We need to insert the SRS in the global list before
2214 	 * binding the SRS and SR threads. Otherwise there is a
2215 	 * is a small window where the cpu reconfig callbacks
2216 	 * may miss the SRS in the list walk and DR could fail
2217 	 * as there are bound threads.
2218 	 */
2219 	mac_srs_add_glist(mac_srs);
2220 
2221 	/* Initialize bw limit */
2222 	if ((mrp->mrp_mask & MRP_MAXBW) != 0) {
2223 		mac_srs->srs_drain_func = mac_rx_srs_drain_bw;
2224 
2225 		mac_bw = mac_srs->srs_bw;
2226 		mutex_enter(&mac_bw->mac_bw_lock);
2227 		mac_bw->mac_bw_limit = FLOW_BYTES_PER_TICK(mrp->mrp_maxbw);
2228 
2229 		/*
2230 		 * Give twice the queuing capability before
2231 		 * dropping packets. The unit is bytes/tick.
2232 		 */
2233 		mac_bw->mac_bw_drop_threshold = mac_bw->mac_bw_limit << 1;
2234 		mutex_exit(&mac_bw->mac_bw_lock);
2235 		mac_srs->srs_type |= SRST_BW_CONTROL;
2236 	} else {
2237 		mac_srs->srs_drain_func = mac_rx_srs_drain;
2238 	}
2239 
2240 	/*
2241 	 * We use the following policy to control Receive
2242 	 * Side Dynamic Polling:
2243 	 * 1) We switch to poll mode anytime the processing thread causes
2244 	 *    a backlog to build up in SRS and its associated Soft Rings
2245 	 *    (sr_poll_pkt_cnt > 0).
2246 	 * 2) As long as the backlog stays under the low water mark
2247 	 *    (sr_lowat), we poll the H/W for more packets.
2248 	 * 3) If the backlog (sr_poll_pkt_cnt) exceeds low water mark, we
2249 	 *    stay in poll mode but don't poll the H/W for more packets.
2250 	 * 4) Anytime in polling mode, if we poll the H/W for packets and
2251 	 *    find nothing plus we have an existing backlog
2252 	 *    (sr_poll_pkt_cnt > 0), we stay in polling mode but don't poll
2253 	 *    the H/W for packets anymore (let the polling thread go to sleep).
2254 	 * 5) Once the backlog is relieved (packets are processed) we reenable
2255 	 *    polling (by signalling the poll thread) only when the backlog
2256 	 *    dips below sr_poll_thres.
2257 	 * 6) sr_hiwat is used exclusively when we are not polling capable
2258 	 *    and is used to decide when to drop packets so the SRS queue
2259 	 *    length doesn't grow infinitely.
2260 	 */
2261 	if (!is_tx_srs) {
2262 		srs_rx->sr_hiwat = mac_soft_ring_max_q_cnt;
2263 		/* Low water mark needs to be less than high water mark */
2264 		srs_rx->sr_lowat = mac_soft_ring_min_q_cnt <=
2265 		    mac_soft_ring_max_q_cnt ? mac_soft_ring_min_q_cnt :
2266 		    (mac_soft_ring_max_q_cnt >> 2);
2267 		/* Poll threshold need to be half of low water mark or less */
2268 		srs_rx->sr_poll_thres = mac_soft_ring_poll_thres <=
2269 		    (srs_rx->sr_lowat >> 1) ? mac_soft_ring_poll_thres :
2270 		    (srs_rx->sr_lowat >> 1);
2271 		mac_srs->srs_type |= mac_latency_optimize ?
2272 		    SRST_LATENCY_OPT : SRST_ENQUEUE;
2273 	}
2274 
2275 	/*
2276 	 * Create the srs_worker with twice the stack of a normal kernel thread
2277 	 * to reduce the likelihood of stack overflows in receive-side
2278 	 * processing.  (The larger stacks are not the only precaution taken
2279 	 * against stack overflows; see the use of mac_rx_srs_stack_needed
2280 	 * in mac_sched.c).
2281 	 */
2282 	mac_srs->srs_worker = thread_create(NULL, default_stksize << 1,
2283 	    mac_srs_worker, mac_srs, 0, &p0, TS_RUN, mac_srs->srs_pri);
2284 
2285 	if (is_tx_srs) {
2286 		/* Handle everything about Tx SRS and return */
2287 		mac_srs->srs_drain_func = mac_tx_srs_drain;
2288 		srs_tx->st_max_q_cnt = mac_tx_srs_max_q_cnt;
2289 		srs_tx->st_hiwat =
2290 		    (mac_tx_srs_hiwat > mac_tx_srs_max_q_cnt) ?
2291 		    mac_tx_srs_max_q_cnt : mac_tx_srs_hiwat;
2292 		srs_tx->st_arg1 = mcip;
2293 		srs_tx->st_arg2 = NULL;
2294 		goto done;
2295 	}
2296 
2297 	if ((srs_type & SRST_FLOW) != 0 ||
2298 	    FLOW_TAB_EMPTY(mcip->mci_subflow_tab))
2299 		srs_rx->sr_lower_proc = mac_rx_srs_process;
2300 	else
2301 		srs_rx->sr_lower_proc = mac_rx_srs_subflow_process;
2302 
2303 	srs_rx->sr_func = rx_func;
2304 	srs_rx->sr_arg1 = mcip;
2305 
2306 	if (ring != NULL) {
2307 		uint_t ring_info;
2308 
2309 		/* Is the mac_srs created over the RX default group? */
2310 		if (ring->mr_gh == (mac_group_handle_t)
2311 		    MAC_DEFAULT_RX_GROUP(mcip->mci_mip)) {
2312 			mac_srs->srs_type |= SRST_DEFAULT_GRP;
2313 		}
2314 		mac_srs->srs_ring = ring;
2315 		ring->mr_srs = mac_srs;
2316 		ring->mr_classify_type = MAC_HW_CLASSIFIER;
2317 		ring->mr_flag |= MR_INCIPIENT;
2318 
2319 		if (!(mcip->mci_mip->mi_state_flags & MIS_POLL_DISABLE) &&
2320 		    FLOW_TAB_EMPTY(mcip->mci_subflow_tab) && mac_poll_enable)
2321 			mac_srs->srs_state |= SRS_POLLING_CAPAB;
2322 
2323 		mac_srs->srs_poll_thr = thread_create(NULL, 0,
2324 		    mac_rx_srs_poll_ring, mac_srs, 0, &p0, TS_RUN,
2325 		    mac_srs->srs_pri);
2326 		/*
2327 		 * Some drivers require serialization and don't send
2328 		 * packet chains in interrupt context. For such
2329 		 * drivers, we should always queue in the soft ring
2330 		 * so that we get a chance to switch into polling
2331 		 * mode under backlog.
2332 		 */
2333 		ring_info = mac_hwring_getinfo((mac_ring_handle_t)ring);
2334 		if (ring_info & MAC_RING_RX_ENQUEUE) {
2335 			mac_srs->srs_type |= SRST_ENQUEUE;
2336 		}
2337 	}
2338 done:
2339 	mac_srs_stat_create(mac_srs);
2340 	return (mac_srs);
2341 }
2342 
2343 /*
2344  * Figure out the number of soft rings required. Its dependant on
2345  * if protocol fanout is required (for LINKs), global settings
2346  * require us to do fanout for performance (based on mac_soft_ring_enable),
2347  * or user has specifically requested fanout.
2348  */
2349 static mac_soft_ring_set_type_t
2350 mac_find_fanout(flow_entry_t *flent, const mac_soft_ring_set_type_t link_type)
2351 {
2352 	uint32_t			fanout_type;
2353 	mac_resource_props_t		*mrp = &flent->fe_effective_props;
2354 
2355 	/* no fanout for subflows */
2356 	switch (link_type) {
2357 	case SRST_FLOW:
2358 		fanout_type = SRST_NO_SOFT_RINGS;
2359 		break;
2360 	case SRST_LINK:
2361 		fanout_type = SRST_FANOUT_PROTO;
2362 		break;
2363 	}
2364 
2365 	/* A primary NIC/link is being plumbed */
2366 	if (flent->fe_type & FLOW_PRIMARY_MAC) {
2367 		if (mac_soft_ring_enable && mac_rx_soft_ring_count > 1) {
2368 			fanout_type |= SRST_FANOUT_SRC_IP;
2369 		}
2370 	} else if (flent->fe_type & FLOW_VNIC) {
2371 		/* A VNIC is being created */
2372 		if (mrp != NULL && mrp->mrp_ncpus > 0) {
2373 			fanout_type |= SRST_FANOUT_SRC_IP;
2374 		}
2375 	}
2376 
2377 	return (fanout_type);
2378 }
2379 
2380 /*
2381  * Change a group from h/w to s/w classification.
2382  */
2383 void
2384 mac_rx_switch_grp_to_sw(mac_group_t *group)
2385 {
2386 	mac_ring_t		*ring;
2387 	mac_soft_ring_set_t	*mac_srs;
2388 
2389 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) {
2390 		if (ring->mr_classify_type == MAC_HW_CLASSIFIER) {
2391 			/*
2392 			 * Remove the SRS associated with the HW ring.
2393 			 * As a result, polling will be disabled.
2394 			 */
2395 			mac_srs = ring->mr_srs;
2396 			ASSERT(mac_srs != NULL);
2397 			mac_rx_srs_remove(mac_srs);
2398 			ring->mr_srs = NULL;
2399 		}
2400 
2401 		if (ring->mr_state != MR_INUSE)
2402 			(void) mac_start_ring(ring);
2403 
2404 		/*
2405 		 * We need to perform SW classification
2406 		 * for packets landing in these rings
2407 		 */
2408 		ring->mr_flag = 0;
2409 		ring->mr_classify_type = MAC_SW_CLASSIFIER;
2410 	}
2411 }
2412 
2413 /*
2414  * Create the Rx SRS for S/W classifier and for each ring in the
2415  * group (if exclusive group). Also create the Tx SRS.
2416  */
2417 void
2418 mac_srs_group_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2419     uint32_t link_type)
2420 {
2421 	cpupart_t		*cpupart;
2422 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
2423 	mac_resource_props_t	*emrp = MCIP_EFFECTIVE_PROPS(mcip);
2424 	boolean_t		use_default = B_FALSE;
2425 
2426 	mac_rx_srs_group_setup(mcip, flent, link_type);
2427 	mac_tx_srs_group_setup(mcip, flent, link_type);
2428 
2429 	/* Aggr ports don't have SRSes; thus there is no soft ring fanout. */
2430 	if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) != 0)
2431 		return;
2432 
2433 	pool_lock();
2434 	cpupart = mac_pset_find(mrp, &use_default);
2435 	mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip),
2436 	    mac_rx_deliver, mcip, cpupart);
2437 	mac_set_pool_effective(use_default, cpupart, mrp, emrp);
2438 	pool_unlock();
2439 }
2440 
2441 /*
2442  * Set up the Rx SRSes. If there is no group associated with the
2443  * client, then only setup SW classification. If the client has
2444  * exlusive (MAC_GROUP_STATE_RESERVED) use of the group, then create an
2445  * SRS for each HW ring. If the client is sharing a group, then make
2446  * sure to teardown the HW SRSes.
2447  */
2448 void
2449 mac_rx_srs_group_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2450     const mac_soft_ring_set_type_t link_type)
2451 {
2452 	mac_impl_t		*mip = mcip->mci_mip;
2453 	mac_soft_ring_set_t	*mac_srs;
2454 	mac_ring_t		*ring;
2455 	mac_group_t		*rx_group = flent->fe_rx_ring_group;
2456 	boolean_t		no_unicast;
2457 
2458 	/*
2459 	 * If this is an an aggr port, then don't setup Rx SRS and Rx
2460 	 * soft rings as they won't be used. However, we still need to
2461 	 * start the rings to receive data on them.
2462 	 */
2463 	if (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) {
2464 		if (rx_group == NULL)
2465 			return;
2466 
2467 		for (ring = rx_group->mrg_rings; ring != NULL;
2468 		    ring = ring->mr_next) {
2469 			if (ring->mr_state != MR_INUSE)
2470 				(void) mac_start_ring(ring);
2471 		}
2472 
2473 		return;
2474 	}
2475 
2476 	/*
2477 	 * Aggr ports should never have SRSes.
2478 	 */
2479 	ASSERT3U((mcip->mci_state_flags & MCIS_IS_AGGR_PORT), ==, 0);
2480 
2481 	const mac_soft_ring_set_type_t fanout_type =
2482 	    mac_find_fanout(flent, link_type);
2483 	no_unicast = (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) != 0;
2484 
2485 	/* Create the SRS for SW classification if none exists */
2486 	if (flent->fe_rx_srs[0] == NULL) {
2487 		ASSERT(flent->fe_rx_srs_cnt == 0);
2488 		mac_srs = mac_srs_create(mcip, flent, fanout_type | link_type,
2489 		    mac_rx_deliver, NULL);
2490 		mutex_enter(&flent->fe_lock);
2491 		flent->fe_cb_fn = (flow_fn_t)mac_srs->srs_rx.sr_lower_proc;
2492 		flent->fe_cb_arg1 = (void *)mip;
2493 		flent->fe_cb_arg2 = (void *)mac_srs;
2494 		mutex_exit(&flent->fe_lock);
2495 	}
2496 
2497 	if (rx_group == NULL)
2498 		return;
2499 
2500 	/*
2501 	 * If the group is marked RESERVED then setup an SRS and
2502 	 * fanout for each HW ring.
2503 	 */
2504 	switch (rx_group->mrg_state) {
2505 	case MAC_GROUP_STATE_RESERVED:
2506 		for (ring = rx_group->mrg_rings; ring != NULL;
2507 		    ring = ring->mr_next) {
2508 			uint16_t vid = i_mac_flow_vid(mcip->mci_flent);
2509 
2510 			switch (ring->mr_state) {
2511 			case MR_INUSE:
2512 			case MR_FREE:
2513 				if (ring->mr_srs != NULL)
2514 					break;
2515 				if (ring->mr_state != MR_INUSE)
2516 					(void) mac_start_ring(ring);
2517 
2518 				/*
2519 				 * If a client requires SW VLAN
2520 				 * filtering or has no unicast address
2521 				 * then we don't create any HW ring
2522 				 * SRSes.
2523 				 */
2524 				if ((!MAC_GROUP_HW_VLAN(rx_group) &&
2525 				    vid != VLAN_ID_NONE) || no_unicast)
2526 					break;
2527 
2528 				/*
2529 				 * When a client has exclusive use of
2530 				 * a group, and that group's traffic
2531 				 * is fully HW classified, we create
2532 				 * an SRS for each HW ring in order to
2533 				 * make use of dynamic polling of said
2534 				 * HW rings.
2535 				 */
2536 				mac_srs = mac_srs_create(mcip, flent,
2537 				    fanout_type | link_type,
2538 				    mac_rx_deliver, ring);
2539 				break;
2540 			default:
2541 				cmn_err(CE_PANIC,
2542 				    "srs_setup: mcip = %p "
2543 				    "trying to add UNKNOWN ring = %p\n",
2544 				    (void *)mcip, (void *)ring);
2545 				break;
2546 			}
2547 		}
2548 		break;
2549 	case MAC_GROUP_STATE_SHARED:
2550 		/*
2551 		 * When a group is shared by multiple clients, we must
2552 		 * use SW classifiction to ensure packets are
2553 		 * delivered to the correct client.
2554 		 */
2555 		mac_rx_switch_grp_to_sw(rx_group);
2556 		break;
2557 	default:
2558 		ASSERT(B_FALSE);
2559 		break;
2560 	}
2561 }
2562 
2563 /*
2564  * Set up the TX SRS.
2565  */
2566 void
2567 mac_tx_srs_group_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2568     const mac_soft_ring_set_type_t link_type)
2569 {
2570 	/*
2571 	 * If this is an exclusive client (e.g. an aggr port), then
2572 	 * don't setup Tx SRS and Tx soft rings as they won't be used.
2573 	 * However, we still need to start the rings to send data
2574 	 * across them.
2575 	 */
2576 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2577 		mac_ring_t		*ring;
2578 		mac_group_t		*grp;
2579 
2580 		grp = (mac_group_t *)flent->fe_tx_ring_group;
2581 
2582 		if (grp == NULL)
2583 			return;
2584 
2585 		for (ring = grp->mrg_rings; ring != NULL;
2586 		    ring = ring->mr_next) {
2587 			if (ring->mr_state != MR_INUSE)
2588 				(void) mac_start_ring(ring);
2589 		}
2590 
2591 		return;
2592 	}
2593 
2594 	/*
2595 	 * Aggr ports should never have SRSes.
2596 	 */
2597 	ASSERT3U((mcip->mci_state_flags & MCIS_IS_AGGR_PORT), ==, 0);
2598 
2599 	if (flent->fe_tx_srs == NULL) {
2600 		(void) mac_srs_create(mcip, flent, SRST_TX | link_type,
2601 		    NULL, NULL);
2602 	}
2603 
2604 	mac_tx_srs_setup(mcip, flent);
2605 }
2606 
2607 /*
2608  * Teardown all the Rx SRSes. Unless hwonly is set, then only teardown
2609  * the Rx HW SRSes and leave the SW SRS alone. The hwonly flag is set
2610  * when we wish to move a MAC client from one group to another. In
2611  * that case, we need to release the current HW SRSes but keep the SW
2612  * SRS for continued traffic classifiction.
2613  */
2614 void
2615 mac_rx_srs_group_teardown(flow_entry_t *flent, boolean_t hwonly)
2616 {
2617 	mac_soft_ring_set_t	*mac_srs;
2618 	int			i;
2619 	int			count = flent->fe_rx_srs_cnt;
2620 
2621 	for (i = 0; i < count; i++) {
2622 		if (i == 0 && hwonly)
2623 			continue;
2624 		mac_srs = flent->fe_rx_srs[i];
2625 		mac_rx_srs_quiesce(mac_srs, SRS_CONDEMNED);
2626 		mac_srs_free(mac_srs);
2627 		flent->fe_rx_srs[i] = NULL;
2628 		flent->fe_rx_srs_cnt--;
2629 	}
2630 
2631 	/*
2632 	 * If we are only tearing down the HW SRSes then there must be
2633 	 * one SRS left for SW classification. Otherwise we are tearing
2634 	 * down both HW and SW and there should be no SRSes left.
2635 	 */
2636 	if (hwonly)
2637 		VERIFY3S(flent->fe_rx_srs_cnt, ==, 1);
2638 	else
2639 		VERIFY3S(flent->fe_rx_srs_cnt, ==, 0);
2640 }
2641 
2642 /*
2643  * Remove the TX SRS.
2644  */
2645 void
2646 mac_tx_srs_group_teardown(mac_client_impl_t *mcip, flow_entry_t *flent,
2647     const mac_soft_ring_set_type_t link_type)
2648 {
2649 	mac_soft_ring_set_t	*tx_srs;
2650 	mac_srs_tx_t		*tx;
2651 
2652 	if ((tx_srs = flent->fe_tx_srs) == NULL)
2653 		return;
2654 
2655 	tx = &tx_srs->srs_tx;
2656 	switch (link_type) {
2657 	case SRST_FLOW:
2658 		/*
2659 		 * For flows, we need to work with passed
2660 		 * flent to find the Rx/Tx SRS.
2661 		 */
2662 		mac_tx_srs_quiesce(tx_srs, SRS_CONDEMNED);
2663 		break;
2664 	case SRST_LINK:
2665 		mac_tx_client_condemn((mac_client_handle_t)mcip);
2666 		if (tx->st_arg2 != NULL) {
2667 			ASSERT(tx_srs->srs_type & SRST_TX);
2668 			/*
2669 			 * The ring itself will be stopped when
2670 			 * we release the group or in the
2671 			 * mac_datapath_teardown (for the default
2672 			 * group)
2673 			 */
2674 			tx->st_arg2 = NULL;
2675 		}
2676 		break;
2677 	default:
2678 		ASSERT(B_FALSE);
2679 		break;
2680 	}
2681 	mac_srs_free(tx_srs);
2682 	flent->fe_tx_srs = NULL;
2683 }
2684 
2685 /*
2686  * This is the group state machine.
2687  *
2688  * The state of an Rx group is given by
2689  * the following table. The default group and its rings are started in
2690  * mac_start itself and the default group stays in SHARED state until
2691  * mac_stop at which time the group and rings are stopped and and it
2692  * reverts to the Registered state.
2693  *
2694  * Typically this function is called on a group after adding or removing a
2695  * client from it, to find out what should be the new state of the group.
2696  * If the new state is RESERVED, then the client that owns this group
2697  * exclusively is also returned. Note that adding or removing a client from
2698  * a group could also impact the default group and the caller needs to
2699  * evaluate the effect on the default group.
2700  *
2701  * Group type		# of clients	mi_nactiveclients	Group State
2702  *			in the group
2703  *
2704  * Non-default		0		N.A.			REGISTERED
2705  * Non-default		1		N.A.			RESERVED
2706  *
2707  * Default		0		N.A.			SHARED
2708  * Default		1		1			RESERVED
2709  * Default		1		> 1			SHARED
2710  * Default		> 1		N.A.			SHARED
2711  *
2712  * For a TX group, the following is the state table.
2713  *
2714  * Group type		# of clients	Group State
2715  *			in the group
2716  *
2717  * Non-default		0		REGISTERED
2718  * Non-default		1		RESERVED
2719  *
2720  * Default		0		REGISTERED
2721  * Default		1		RESERVED
2722  * Default		> 1		SHARED
2723  */
2724 mac_group_state_t
2725 mac_group_next_state(mac_group_t *grp, mac_client_impl_t **group_only_mcip,
2726     mac_group_t *defgrp, boolean_t rx_group)
2727 {
2728 	mac_impl_t		*mip = (mac_impl_t *)grp->mrg_mh;
2729 
2730 	*group_only_mcip = NULL;
2731 
2732 	/* Non-default group */
2733 
2734 	if (grp != defgrp) {
2735 		if (MAC_GROUP_NO_CLIENT(grp))
2736 			return (MAC_GROUP_STATE_REGISTERED);
2737 
2738 		*group_only_mcip = MAC_GROUP_ONLY_CLIENT(grp);
2739 		if (*group_only_mcip != NULL)
2740 			return (MAC_GROUP_STATE_RESERVED);
2741 
2742 		return (MAC_GROUP_STATE_SHARED);
2743 	}
2744 
2745 	/* Default group */
2746 
2747 	if (MAC_GROUP_NO_CLIENT(grp)) {
2748 		if (rx_group)
2749 			return (MAC_GROUP_STATE_SHARED);
2750 		else
2751 			return (MAC_GROUP_STATE_REGISTERED);
2752 	}
2753 	*group_only_mcip = MAC_GROUP_ONLY_CLIENT(grp);
2754 	if (*group_only_mcip == NULL)
2755 		return (MAC_GROUP_STATE_SHARED);
2756 
2757 	if (rx_group && mip->mi_nactiveclients != 1)
2758 		return (MAC_GROUP_STATE_SHARED);
2759 
2760 	ASSERT(*group_only_mcip != NULL);
2761 	return (MAC_GROUP_STATE_RESERVED);
2762 }
2763 
2764 /*
2765  * OVERVIEW NOTES FOR DATAPATH
2766  * ===========================
2767  *
2768  * Create an SRS and setup the corresponding flow function and args.
2769  * Add a classification rule for the flow specified by 'flent' and program
2770  * the hardware classifier when applicable.
2771  *
2772  * Rx ring assignment, SRS, polling and B/W enforcement
2773  * ----------------------------------------------------
2774  *
2775  * We try to use H/W classification on NIC and assign traffic to a
2776  * MAC address to a particular Rx ring. There is a 1-1 mapping
2777  * between a SRS and a Rx ring. The SRS (short for soft ring set)
2778  * dynamically switches the underlying Rx ring between interrupt
2779  * and polling mode and enforces any specified B/W control.
2780  *
2781  * There is always a SRS created and tied to each H/W and S/W rule.
2782  * Whenever we create a H/W rule, we always add the the same rule to
2783  * S/W classifier and tie a SRS to it.
2784  *
2785  * In case a B/W control is specified, its broken into bytes
2786  * per ticks and as soon as the quota for a tick is exhausted,
2787  * the underlying Rx ring is forced into poll mode for remianing
2788  * tick. The SRS poll thread only polls for bytes that are
2789  * allowed to come in the SRS. We typically let 4x the configured
2790  * B/W worth of packets to come in the SRS (to prevent unnecessary
2791  * drops due to bursts) but only process the specified amount.
2792  *
2793  * A Link (primary NIC, VNIC, VLAN or aggr) can have 1 or more
2794  * Rx rings (and corresponding SRSs) assigned to it. The SRS
2795  * in turn can have softrings to do protocol level fanout or
2796  * softrings to do S/W based fanout or both. In case the NIC
2797  * has no Rx rings, we do S/W classification to respective SRS.
2798  * The S/W classification rule is always setup and ready. This
2799  * allows the MAC layer to reassign Rx rings whenever needed
2800  * but packets still continue to flow via the default path and
2801  * getting S/W classified to correct SRS.
2802  *
2803  * In other cases where a NIC or VNIC is plumbed, our goal is use
2804  * H/W classifier and get two Rx ring assigned for the Link. One
2805  * for TCP and one for UDP|SCTP. The respective SRS still do the
2806  * polling on the Rx ring. For Link that is plumbed for IP, there
2807  * is a TCP squeue which also does polling and can control the
2808  * the Rx ring directly (where SRS is just pass through). For
2809  * the following cases, the SRS does the polling underneath.
2810  * 1) non IP based Links (Links which are not plumbed via ifconfig)
2811  *    and paths which have no IP squeues (UDP & SCTP)
2812  * 2) If B/W control is specified on the Link
2813  * 3) If S/W fanout is secified
2814  *
2815  * Note1: As of current implementation, we try to assign only 1 Rx
2816  * ring per Link and more than 1 Rx ring for primary Link for
2817  * H/W based fanout. We always create following softrings per SRS:
2818  * 1) TCP softring which is polled by TCP squeue where possible
2819  *    (and also bypasses DLS)
2820  * 2) UDP/SCTP based which bypasses DLS
2821  * 3) OTH softring which goes via DLS (currently deal with IPv6
2822  *    and non TCP/UDP/SCTP for IPv4 packets).
2823  *
2824  * It is necessary to create 3 softrings since SRS has to poll
2825  * the single Rx ring underneath and enforce any link level B/W
2826  * control (we can't switch the Rx ring in poll mode just based
2827  * on TCP squeue if the same Rx ring is sharing UDP and other
2828  * traffic as well). Once polling is done and any Link level B/W
2829  * control is specified, the packets are assigned to respective
2830  * softring based on protocol. Since TCP has IP based squeue
2831  * which benefits by polling, we separate TCP packets into
2832  * its own softring which can be polled by IP squeue. We need
2833  * to separate out UDP/SCTP to UDP softring since it can bypass
2834  * the DLS layer which has heavy performance advanatges and we
2835  * need a softring (OTH) for rest.
2836  *
2837  * ToDo: The 3 softrings for protocol are needed only till we can
2838  * get rid of DLS from datapath, make IPv4 and IPv6 paths
2839  * symmetric (deal with mac_header_info for v6 and polling for
2840  * IPv4 TCP - ip_accept_tcp is IPv4 specific although squeues
2841  * are generic), and bring SAP based classification to MAC layer
2842  *
2843  * H/W and S/W based fanout and multiple Rx rings per Link
2844  * -------------------------------------------------------
2845  *
2846  * In case, fanout is requested (or determined automatically based
2847  * on Link speed and processor speed), we try to assign multiple
2848  * Rx rings per Link with their respective SRS. In this case
2849  * the NIC should be capable of fanning out incoming packets between
2850  * the assigned Rx rings (H/W based fanout). All the SRS
2851  * individually switch their Rx ring between interrupt and polling
2852  * mode but share a common B/W control counter in case of Link
2853  * level B/W is specified.
2854  *
2855  * If S/W based fanout is specified in lieu of H/W based fanout,
2856  * the Link SRS creates the specified number of softrings for
2857  * each protocol (TCP, UDP, OTH). Incoming packets are fanned
2858  * out to the correct softring based on their protocol and
2859  * protocol specific hash function.
2860  *
2861  * Primary and non primary MAC clients
2862  * -----------------------------------
2863  *
2864  * The NICs, VNICs, Vlans, and Aggrs are typically termed as Links
2865  * and are a Layer 2 construct.
2866  *
2867  * Primary NIC:
2868  *	The Link that owns the primary MAC address and typically
2869  *	is used as the data NIC in non virtualized cases. As such
2870  *	H/W resources are preferntially given to primary NIC. As
2871  *	far as code is concerned, there is no difference in the
2872  *	primary NIC vs VNICs. They are all treated as Links.
2873  *	At the very first call to mac_unicast_add() we program the S/W
2874  *	classifier for the primary MAC address, get a soft ring set
2875  *	(and soft rings based on 'ip_soft_ring_cnt')
2876  *	and a Rx ring assigned for polling to get enabled.
2877  *	When IP get plumbed and negotiates polling, we can
2878  *	let squeue do the polling on TCP softring.
2879  *
2880  * VNICs:
2881  *	Same as any other Link. As long as the H/W resource assignments
2882  *	are equal, the data path and setup for all Links is same.
2883  *
2884  * Flows:
2885  *	Can be configured on Links. They have their own SRS and the
2886  *	S/W classifier is programmed appropriately based on the flow.
2887  *	The flows typically deal with layer 3 and above and
2888  *	creates a soft ring set specific to the flow. The receive
2889  *	side function is switched from mac_rx_srs_process to
2890  *	mac_rx_srs_subflow_process which first tries to assign the
2891  *	packet to appropriate flow SRS and failing which assigns it
2892  *	to link SRS. This allows us to avoid the layered approach
2893  *	which gets complex.
2894  *
2895  * By the time mac_datapath_setup() completes, we already have the
2896  * soft rings set, Rx rings, soft rings, etc figured out and both H/W
2897  * and S/W classifiers programmed. IP is not plumbed yet (and might
2898  * never be for Virtual Machines guest OS path). When IP is plumbed
2899  * (for both NIC and VNIC), we do a capability negotiation for polling
2900  * and upcall functions etc.
2901  *
2902  * Rx ring Assignement NOTES
2903  * -------------------------
2904  *
2905  * For NICs which have only 1 Rx ring (we treat  NICs with no Rx rings
2906  * as NIC with a single default ring), we assign the only ring to
2907  * primary Link. The primary Link SRS can do polling on it as long as
2908  * it is the only link in use and we compare the MAC address for unicast
2909  * packets before accepting an incoming packet (there is no need for S/W
2910  * classification in this case). We disable polling on the only ring the
2911  * moment 2nd link gets created (the polling remains enabled even though
2912  * there are broadcast and * multicast flows created).
2913  *
2914  * If the NIC has more than 1 Rx ring, we assign the default ring (the
2915  * 1st ring) to deal with broadcast, multicast and traffic for other
2916  * NICs which needs S/W classification. We assign the primary mac
2917  * addresses to another ring by specifiying a classification rule for
2918  * primary unicast MAC address to the selected ring. The primary Link
2919  * (and its SRS) can continue to poll the assigned Rx ring at all times
2920  * independantly.
2921  *
2922  * Note: In future, if no fanout is specified, we try to assign 2 Rx
2923  * rings for the primary Link with the primary MAC address + TCP going
2924  * to one ring and primary MAC address + UDP|SCTP going to other ring.
2925  * Any remaining traffic for primary MAC address can go to the default
2926  * Rx ring and get S/W classified. This way the respective SRSs don't
2927  * need to do proto fanout and don't need to have softrings at all and
2928  * can poll their respective Rx rings.
2929  *
2930  * As an optimization, when a new NIC or VNIC is created, we can get
2931  * only one Rx ring and make it a TCP specific Rx ring and use the
2932  * H/W default Rx ring for the rest (this Rx ring is never polled).
2933  *
2934  * For clients that don't have MAC address, but want to receive and
2935  * transmit packets (e.g, bpf, gvrp etc.), we need to setup the datapath.
2936  * For such clients (identified by the MCIS_NO_UNICAST_ADDR flag) we
2937  * always give the default group and use software classification (i.e.
2938  * even if this is the only client in the default group, we will
2939  * leave group as shared).
2940  */
2941 
2942 int
2943 mac_datapath_setup(mac_client_impl_t *mcip, flow_entry_t *flent,
2944     const mac_soft_ring_set_type_t link_type)
2945 {
2946 	mac_impl_t		*mip = mcip->mci_mip;
2947 	mac_group_t		*rgroup = NULL;
2948 	mac_group_t		*tgroup = NULL;
2949 	mac_group_t		*default_rgroup;
2950 	mac_group_t		*default_tgroup;
2951 	int			err;
2952 	uint16_t		vid;
2953 	uint8_t			*mac_addr;
2954 	mac_group_state_t	next_state;
2955 	mac_client_impl_t	*group_only_mcip;
2956 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
2957 	mac_resource_props_t	*emrp = MCIP_EFFECTIVE_PROPS(mcip);
2958 	boolean_t		rxhw;
2959 	boolean_t		txhw;
2960 	boolean_t		use_default = B_FALSE;
2961 	cpupart_t		*cpupart;
2962 	boolean_t		no_unicast;
2963 	boolean_t		isprimary = flent->fe_type & FLOW_PRIMARY_MAC;
2964 	mac_client_impl_t	*reloc_pmcip = NULL;
2965 	boolean_t		use_hw;
2966 
2967 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2968 
2969 	switch (link_type) {
2970 	case SRST_FLOW:
2971 		mac_srs_group_setup(mcip, flent, link_type);
2972 		return (0);
2973 
2974 	case SRST_LINK:
2975 		no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
2976 		mac_addr = flent->fe_flow_desc.fd_dst_mac;
2977 
2978 		/* Default RX group */
2979 		default_rgroup = MAC_DEFAULT_RX_GROUP(mip);
2980 
2981 		/* Default TX group */
2982 		default_tgroup = MAC_DEFAULT_TX_GROUP(mip);
2983 
2984 		if (no_unicast) {
2985 			rgroup = default_rgroup;
2986 			tgroup = default_tgroup;
2987 			goto grp_found;
2988 		}
2989 		rxhw = (mrp->mrp_mask & MRP_RX_RINGS) &&
2990 		    (mrp->mrp_nrxrings > 0 ||
2991 		    (mrp->mrp_mask & MRP_RXRINGS_UNSPEC));
2992 		txhw = (mrp->mrp_mask & MRP_TX_RINGS) &&
2993 		    (mrp->mrp_ntxrings > 0 ||
2994 		    (mrp->mrp_mask & MRP_TXRINGS_UNSPEC));
2995 
2996 		/*
2997 		 * All the rings initially belong to the default group
2998 		 * under dynamic grouping. The primary client uses the
2999 		 * default group when it is the only client. The
3000 		 * default group is also used as the destination for
3001 		 * all multicast and broadcast traffic of all clients.
3002 		 * Therefore, the primary client loses its ability to
3003 		 * poll the softrings on addition of a second client.
3004 		 * To avoid a performance penalty, MAC will move the
3005 		 * primary client to a dedicated group when it can.
3006 		 *
3007 		 * When using static grouping, the primary client
3008 		 * begins life on a non-default group. There is
3009 		 * no moving needed upon addition of a second client.
3010 		 */
3011 		if (!isprimary && mip->mi_nactiveclients == 2 &&
3012 		    (group_only_mcip = mac_primary_client_handle(mip)) !=
3013 		    NULL && mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
3014 			reloc_pmcip = mac_check_primary_relocation(
3015 			    group_only_mcip, rxhw);
3016 		}
3017 
3018 		/*
3019 		 * Check to see if we can get an exclusive group for
3020 		 * this mac address or if there already exists a
3021 		 * group that has this mac address (case of VLANs).
3022 		 * If no groups are available, use the default group.
3023 		 */
3024 		rgroup = mac_reserve_rx_group(mcip, mac_addr, B_FALSE);
3025 		if (rgroup == NULL && rxhw) {
3026 			err = ENOSPC;
3027 			goto setup_failed;
3028 		} else if (rgroup == NULL) {
3029 			rgroup = default_rgroup;
3030 		}
3031 
3032 		/*
3033 		 * If we are adding a second client to a
3034 		 * non-default group then we need to move the
3035 		 * existing client to the default group and
3036 		 * add the new client to the default group as
3037 		 * well.
3038 		 */
3039 		if (rgroup != default_rgroup &&
3040 		    rgroup->mrg_state == MAC_GROUP_STATE_RESERVED) {
3041 			group_only_mcip = MAC_GROUP_ONLY_CLIENT(rgroup);
3042 			err = mac_rx_switch_group(group_only_mcip, rgroup,
3043 			    default_rgroup);
3044 
3045 			if (err != 0)
3046 				goto setup_failed;
3047 
3048 			rgroup = default_rgroup;
3049 		}
3050 
3051 		/*
3052 		 * Check to see if we can get an exclusive group for
3053 		 * this mac client. If no groups are available, use
3054 		 * the default group.
3055 		 */
3056 		tgroup = mac_reserve_tx_group(mcip, B_FALSE);
3057 		if (tgroup == NULL && txhw) {
3058 			if (rgroup != NULL && rgroup != default_rgroup)
3059 				mac_release_rx_group(mcip, rgroup);
3060 			err = ENOSPC;
3061 			goto setup_failed;
3062 		} else if (tgroup == NULL) {
3063 			tgroup = default_tgroup;
3064 		}
3065 
3066 		/*
3067 		 * Some NICs don't support any Rx rings, so there may not
3068 		 * even be a default group.
3069 		 */
3070 	grp_found:
3071 		if (rgroup != NULL) {
3072 			if (rgroup != default_rgroup &&
3073 			    MAC_GROUP_NO_CLIENT(rgroup) &&
3074 			    (rxhw || mcip->mci_share != 0)) {
3075 				MAC_RX_GRP_RESERVED(mip);
3076 				if (mip->mi_rx_group_type ==
3077 				    MAC_GROUP_TYPE_DYNAMIC) {
3078 					MAC_RX_RING_RESERVED(mip,
3079 					    rgroup->mrg_cur_count);
3080 				}
3081 			}
3082 
3083 			flent->fe_rx_ring_group = rgroup;
3084 			/*
3085 			 * Add the client to the group and update the
3086 			 * group's state. If rgroup != default_group
3087 			 * then the rgroup should only ever have one
3088 			 * client and be in the RESERVED state. But no
3089 			 * matter what, the default_rgroup will enter
3090 			 * the SHARED state since it has to receive
3091 			 * all broadcast and multicast traffic. This
3092 			 * case is handled later in the function.
3093 			 */
3094 			mac_group_add_client(rgroup, mcip);
3095 			next_state = mac_group_next_state(rgroup,
3096 			    &group_only_mcip, default_rgroup, B_TRUE);
3097 			mac_set_group_state(rgroup, next_state);
3098 		}
3099 
3100 		if (tgroup != NULL) {
3101 			if (tgroup != default_tgroup &&
3102 			    MAC_GROUP_NO_CLIENT(tgroup) &&
3103 			    (txhw || mcip->mci_share != 0)) {
3104 				MAC_TX_GRP_RESERVED(mip);
3105 				if (mip->mi_tx_group_type ==
3106 				    MAC_GROUP_TYPE_DYNAMIC) {
3107 					MAC_TX_RING_RESERVED(mip,
3108 					    tgroup->mrg_cur_count);
3109 				}
3110 			}
3111 			flent->fe_tx_ring_group = tgroup;
3112 			mac_group_add_client(tgroup, mcip);
3113 			next_state = mac_group_next_state(tgroup,
3114 			    &group_only_mcip, default_tgroup, B_FALSE);
3115 			tgroup->mrg_state = next_state;
3116 		}
3117 
3118 		/* We are setting up minimal datapath only */
3119 		if (no_unicast) {
3120 			mac_srs_group_setup(mcip, flent, link_type);
3121 			break;
3122 		}
3123 
3124 		/* Program software classification. */
3125 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0)
3126 			goto setup_failed;
3127 
3128 		/* Program hardware classification. */
3129 		vid = i_mac_flow_vid(flent);
3130 		use_hw = (mcip->mci_state_flags & MCIS_UNICAST_HW) != 0;
3131 		err = mac_add_macaddr_vlan(mip, rgroup, mac_addr, vid, use_hw);
3132 
3133 		if (err != 0)
3134 			goto setup_failed;
3135 
3136 		mcip->mci_unicast = mac_find_macaddr(mip, mac_addr);
3137 		VERIFY(mcip->mci_unicast != NULL);
3138 
3139 		/*
3140 		 * Setup the Rx and Tx SRSes. If the client has a
3141 		 * reserved group, then mac_srs_group_setup() creates
3142 		 * the required SRSes for the HW rings. If we have a
3143 		 * shared group, mac_srs_group_setup() dismantles the
3144 		 * HW SRSes of the previously exclusive group.
3145 		 */
3146 		mac_srs_group_setup(mcip, flent, link_type);
3147 
3148 		/* (Re)init the v6 token & local addr used by link protection */
3149 		mac_protect_update_mac_token(mcip);
3150 		break;
3151 
3152 	default:
3153 		ASSERT(B_FALSE);
3154 		break;
3155 	}
3156 
3157 	/*
3158 	 * All broadcast and multicast traffic is received only on the default
3159 	 * group. If we have setup the datapath for a non-default group above
3160 	 * then move the default group to shared state to allow distribution of
3161 	 * incoming broadcast traffic to the other groups and dismantle the
3162 	 * SRSes over the default group.
3163 	 */
3164 	if (rgroup != NULL) {
3165 		if (rgroup != default_rgroup) {
3166 			if (default_rgroup->mrg_state ==
3167 			    MAC_GROUP_STATE_RESERVED) {
3168 				group_only_mcip = MAC_GROUP_ONLY_CLIENT(
3169 				    default_rgroup);
3170 				ASSERT(group_only_mcip != NULL &&
3171 				    mip->mi_nactiveclients > 1);
3172 
3173 				mac_set_group_state(default_rgroup,
3174 				    MAC_GROUP_STATE_SHARED);
3175 				mac_rx_srs_group_setup(group_only_mcip,
3176 				    group_only_mcip->mci_flent, SRST_LINK);
3177 				pool_lock();
3178 				cpupart = mac_pset_find(mrp, &use_default);
3179 				mac_fanout_setup(group_only_mcip,
3180 				    group_only_mcip->mci_flent,
3181 				    MCIP_RESOURCE_PROPS(group_only_mcip),
3182 				    mac_rx_deliver, group_only_mcip, cpupart);
3183 				mac_set_pool_effective(use_default, cpupart,
3184 				    mrp, emrp);
3185 				pool_unlock();
3186 			}
3187 			ASSERT(default_rgroup->mrg_state ==
3188 			    MAC_GROUP_STATE_SHARED);
3189 		}
3190 
3191 		/*
3192 		 * A VLAN MAC client on a reserved group still
3193 		 * requires SW classification if the MAC doesn't
3194 		 * provide VLAN HW filtering.
3195 		 *
3196 		 * Clients with no unicast address also require SW
3197 		 * classification.
3198 		 */
3199 		if (rgroup->mrg_state == MAC_GROUP_STATE_RESERVED &&
3200 		    ((!MAC_GROUP_HW_VLAN(rgroup) && vid != VLAN_ID_NONE) ||
3201 		    no_unicast)) {
3202 			mac_rx_switch_grp_to_sw(rgroup);
3203 		}
3204 
3205 	}
3206 
3207 	mac_set_rings_effective(mcip);
3208 	return (0);
3209 
3210 setup_failed:
3211 	/* Switch the primary back to default group */
3212 	if (reloc_pmcip != NULL) {
3213 		(void) mac_rx_switch_group(reloc_pmcip,
3214 		    reloc_pmcip->mci_flent->fe_rx_ring_group, default_rgroup);
3215 	}
3216 	mac_datapath_teardown(mcip, flent, link_type);
3217 	return (err);
3218 }
3219 
3220 void
3221 mac_datapath_teardown(mac_client_impl_t *mcip, flow_entry_t *flent,
3222     const mac_soft_ring_set_type_t link_type)
3223 {
3224 	mac_impl_t		*mip = mcip->mci_mip;
3225 	mac_group_t		*group = NULL;
3226 	mac_client_impl_t	*grp_only_mcip;
3227 	flow_entry_t		*group_only_flent;
3228 	mac_group_t		*default_group;
3229 	boolean_t		check_default_group = B_FALSE;
3230 	mac_group_state_t	next_state;
3231 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
3232 	uint16_t		vid;
3233 
3234 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3235 
3236 	switch (link_type) {
3237 	case SRST_FLOW:
3238 		mac_rx_srs_group_teardown(flent, B_FALSE);
3239 		mac_tx_srs_group_teardown(mcip, flent, SRST_FLOW);
3240 		return;
3241 
3242 	case SRST_LINK:
3243 		/* Stop sending packets */
3244 		mac_tx_client_block(mcip);
3245 		group = flent->fe_rx_ring_group;
3246 		vid = i_mac_flow_vid(flent);
3247 
3248 		/*
3249 		 * Stop the packet flow from the hardware by disabling
3250 		 * any hardware filters assigned to this client.
3251 		 */
3252 		if (mcip->mci_unicast != NULL) {
3253 			int err;
3254 
3255 			err = mac_remove_macaddr_vlan(mcip->mci_unicast, vid);
3256 
3257 			if (err != 0) {
3258 				cmn_err(CE_WARN, "%s: failed to remove a MAC HW"
3259 				    " filters because of error 0x%x",
3260 				    mip->mi_name, err);
3261 			}
3262 
3263 			mcip->mci_unicast = NULL;
3264 		}
3265 
3266 		/* Stop the packets coming from the S/W classifier */
3267 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
3268 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
3269 
3270 		/* Quiesce and destroy all the SRSes. */
3271 		mac_rx_srs_group_teardown(flent, B_FALSE);
3272 		mac_tx_srs_group_teardown(mcip, flent, SRST_LINK);
3273 
3274 		ASSERT3P(mcip->mci_flent, ==, flent);
3275 		ASSERT3P(flent->fe_next, ==, NULL);
3276 
3277 		/*
3278 		 * Release our hold on the group as well. We need
3279 		 * to check if the shared group has only one client
3280 		 * left who can use it exclusively. Also, if we
3281 		 * were the last client, release the group.
3282 		 */
3283 		default_group = MAC_DEFAULT_RX_GROUP(mip);
3284 		if (group != NULL) {
3285 			mac_group_remove_client(group, mcip);
3286 			next_state = mac_group_next_state(group,
3287 			    &grp_only_mcip, default_group, B_TRUE);
3288 
3289 			if (next_state == MAC_GROUP_STATE_RESERVED) {
3290 				/*
3291 				 * Only one client left on this RX group.
3292 				 */
3293 				VERIFY(grp_only_mcip != NULL);
3294 				mac_set_group_state(group,
3295 				    MAC_GROUP_STATE_RESERVED);
3296 				group_only_flent = grp_only_mcip->mci_flent;
3297 
3298 				/*
3299 				 * The only remaining client has exclusive
3300 				 * access on the group. Allow it to
3301 				 * dynamically poll the H/W rings etc.
3302 				 */
3303 				mac_rx_srs_group_setup(grp_only_mcip,
3304 				    group_only_flent, SRST_LINK);
3305 				mac_fanout_setup(grp_only_mcip,
3306 				    group_only_flent,
3307 				    MCIP_RESOURCE_PROPS(grp_only_mcip),
3308 				    mac_rx_deliver, grp_only_mcip, NULL);
3309 				mac_rx_group_unmark(group, MR_INCIPIENT);
3310 				mac_set_rings_effective(grp_only_mcip);
3311 			} else if (next_state == MAC_GROUP_STATE_REGISTERED) {
3312 				/*
3313 				 * This is a non-default group being freed up.
3314 				 * We need to reevaluate the default group
3315 				 * to see if the primary client can get
3316 				 * exclusive access to the default group.
3317 				 */
3318 				VERIFY3P(group, !=, MAC_DEFAULT_RX_GROUP(mip));
3319 				if (mrp->mrp_mask & MRP_RX_RINGS) {
3320 					MAC_RX_GRP_RELEASED(mip);
3321 					if (mip->mi_rx_group_type ==
3322 					    MAC_GROUP_TYPE_DYNAMIC) {
3323 						MAC_RX_RING_RELEASED(mip,
3324 						    group->mrg_cur_count);
3325 					}
3326 				}
3327 				mac_release_rx_group(mcip, group);
3328 				mac_set_group_state(group,
3329 				    MAC_GROUP_STATE_REGISTERED);
3330 				check_default_group = B_TRUE;
3331 			} else {
3332 				VERIFY3S(next_state, ==,
3333 				    MAC_GROUP_STATE_SHARED);
3334 				mac_set_group_state(group,
3335 				    MAC_GROUP_STATE_SHARED);
3336 				mac_rx_group_unmark(group, MR_CONDEMNED);
3337 			}
3338 			flent->fe_rx_ring_group = NULL;
3339 		}
3340 		/*
3341 		 * Remove the client from the TX group. Additionally, if
3342 		 * this a non-default group, then we also need to release
3343 		 * the group.
3344 		 */
3345 		group = flent->fe_tx_ring_group;
3346 		default_group = MAC_DEFAULT_TX_GROUP(mip);
3347 		if (group != NULL) {
3348 			mac_group_remove_client(group, mcip);
3349 			next_state = mac_group_next_state(group,
3350 			    &grp_only_mcip, default_group, B_FALSE);
3351 			if (next_state == MAC_GROUP_STATE_REGISTERED) {
3352 				if (group != default_group) {
3353 					if (mrp->mrp_mask & MRP_TX_RINGS) {
3354 						MAC_TX_GRP_RELEASED(mip);
3355 						if (mip->mi_tx_group_type ==
3356 						    MAC_GROUP_TYPE_DYNAMIC) {
3357 							MAC_TX_RING_RELEASED(
3358 							    mip, group->
3359 							    mrg_cur_count);
3360 						}
3361 					}
3362 					mac_release_tx_group(mcip, group);
3363 					/*
3364 					 * If the default group is reserved,
3365 					 * then we need to set the effective
3366 					 * rings as we would have given
3367 					 * back some rings when the group
3368 					 * was released
3369 					 */
3370 					if (mip->mi_tx_group_type ==
3371 					    MAC_GROUP_TYPE_DYNAMIC &&
3372 					    default_group->mrg_state ==
3373 					    MAC_GROUP_STATE_RESERVED) {
3374 						grp_only_mcip =
3375 						    MAC_GROUP_ONLY_CLIENT
3376 						    (default_group);
3377 						mac_set_rings_effective(
3378 						    grp_only_mcip);
3379 					}
3380 				} else {
3381 					mac_ring_t	*ring;
3382 					int		cnt;
3383 					int		ringcnt;
3384 
3385 					/*
3386 					 * Stop all the rings except the
3387 					 * default ring.
3388 					 */
3389 					ringcnt = group->mrg_cur_count;
3390 					ring = group->mrg_rings;
3391 					for (cnt = 0; cnt < ringcnt; cnt++) {
3392 						if (ring->mr_state ==
3393 						    MR_INUSE && ring !=
3394 						    (mac_ring_t *)
3395 						    mip->mi_default_tx_ring) {
3396 							mac_stop_ring(ring);
3397 							ring->mr_flag = 0;
3398 						}
3399 						ring = ring->mr_next;
3400 					}
3401 				}
3402 			} else if (next_state == MAC_GROUP_STATE_RESERVED) {
3403 				mac_set_rings_effective(grp_only_mcip);
3404 			}
3405 			flent->fe_tx_ring_group = NULL;
3406 			group->mrg_state = next_state;
3407 		}
3408 		break;
3409 	default:
3410 		ASSERT(B_FALSE);
3411 		break;
3412 	}
3413 
3414 	/*
3415 	 * The mac client using the default group gets exclusive access to the
3416 	 * default group if and only if it is the sole client on the entire
3417 	 * mip. If so set the group state to reserved, and set up the SRSes
3418 	 * over the default group.
3419 	 */
3420 	if (check_default_group) {
3421 		default_group = MAC_DEFAULT_RX_GROUP(mip);
3422 		VERIFY3S(default_group->mrg_state, ==, MAC_GROUP_STATE_SHARED);
3423 		next_state = mac_group_next_state(default_group,
3424 		    &grp_only_mcip, default_group, B_TRUE);
3425 		if (next_state == MAC_GROUP_STATE_RESERVED) {
3426 			VERIFY(grp_only_mcip != NULL);
3427 			VERIFY3U(mip->mi_nactiveclients, ==, 1);
3428 			mac_set_group_state(default_group,
3429 			    MAC_GROUP_STATE_RESERVED);
3430 			mac_rx_srs_group_setup(grp_only_mcip,
3431 			    grp_only_mcip->mci_flent, SRST_LINK);
3432 			mac_fanout_setup(grp_only_mcip,
3433 			    grp_only_mcip->mci_flent,
3434 			    MCIP_RESOURCE_PROPS(grp_only_mcip), mac_rx_deliver,
3435 			    grp_only_mcip, NULL);
3436 			mac_rx_group_unmark(default_group, MR_INCIPIENT);
3437 			mac_set_rings_effective(grp_only_mcip);
3438 		}
3439 	}
3440 
3441 	/*
3442 	 * If the primary is the only one left and the MAC supports
3443 	 * dynamic grouping, we need to see if the primary needs to
3444 	 * be moved to the default group so that it can use all the
3445 	 * H/W rings.
3446 	 */
3447 	if (!(flent->fe_type & FLOW_PRIMARY_MAC) &&
3448 	    mip->mi_nactiveclients == 1 &&
3449 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
3450 		default_group = MAC_DEFAULT_RX_GROUP(mip);
3451 		grp_only_mcip = mac_primary_client_handle(mip);
3452 		if (grp_only_mcip == NULL)
3453 			return;
3454 		group_only_flent = grp_only_mcip->mci_flent;
3455 		mrp = MCIP_RESOURCE_PROPS(grp_only_mcip);
3456 		/*
3457 		 * If the primary has an explicit property set, leave it
3458 		 * alone.
3459 		 */
3460 		if (mrp->mrp_mask & MRP_RX_RINGS)
3461 			return;
3462 		/*
3463 		 * Switch the primary to the default group.
3464 		 */
3465 		(void) mac_rx_switch_group(grp_only_mcip,
3466 		    group_only_flent->fe_rx_ring_group, default_group);
3467 	}
3468 }
3469 
3470 /* DATAPATH TEAR DOWN ROUTINES (SRS and FANOUT teardown) */
3471 
3472 static void
3473 mac_srs_fanout_list_free(mac_soft_ring_set_t *mac_srs)
3474 {
3475 	if (mac_srs->srs_type & SRST_TX) {
3476 		mac_srs_tx_t *tx;
3477 
3478 		ASSERT(mac_srs->srs_tcp_soft_rings == NULL);
3479 		ASSERT(mac_srs->srs_udp_soft_rings == NULL);
3480 		ASSERT(mac_srs->srs_tcp6_soft_rings == NULL);
3481 		ASSERT(mac_srs->srs_udp6_soft_rings == NULL);
3482 		ASSERT(mac_srs->srs_oth_soft_rings == NULL);
3483 		ASSERT(mac_srs->srs_tx_soft_rings != NULL);
3484 		kmem_free(mac_srs->srs_tx_soft_rings,
3485 		    sizeof (mac_soft_ring_t *) * MAX_RINGS_PER_GROUP);
3486 		mac_srs->srs_tx_soft_rings = NULL;
3487 		tx = &mac_srs->srs_tx;
3488 		if (tx->st_soft_rings != NULL) {
3489 			kmem_free(tx->st_soft_rings,
3490 			    sizeof (mac_soft_ring_t *) * MAX_RINGS_PER_GROUP);
3491 		}
3492 	} else {
3493 		ASSERT(mac_srs->srs_tx_soft_rings == NULL);
3494 
3495 		ASSERT(mac_srs->srs_tcp_soft_rings != NULL);
3496 		kmem_free(mac_srs->srs_tcp_soft_rings,
3497 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3498 		mac_srs->srs_tcp_soft_rings = NULL;
3499 
3500 		ASSERT(mac_srs->srs_udp_soft_rings != NULL);
3501 		kmem_free(mac_srs->srs_udp_soft_rings,
3502 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3503 		mac_srs->srs_udp_soft_rings = NULL;
3504 
3505 		ASSERT(mac_srs->srs_tcp6_soft_rings != NULL);
3506 		kmem_free(mac_srs->srs_tcp6_soft_rings,
3507 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3508 		mac_srs->srs_tcp6_soft_rings = NULL;
3509 
3510 		ASSERT(mac_srs->srs_udp6_soft_rings != NULL);
3511 		kmem_free(mac_srs->srs_udp6_soft_rings,
3512 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3513 		mac_srs->srs_udp6_soft_rings = NULL;
3514 
3515 		ASSERT(mac_srs->srs_oth_soft_rings != NULL);
3516 		kmem_free(mac_srs->srs_oth_soft_rings,
3517 		    sizeof (mac_soft_ring_t *) * MAX_SR_FANOUT);
3518 		mac_srs->srs_oth_soft_rings = NULL;
3519 	}
3520 }
3521 
3522 /*
3523  * An RX SRS is attached to at most one mac_ring.
3524  * A TX SRS  has no  rings.
3525  */
3526 static void
3527 mac_srs_ring_free(mac_soft_ring_set_t *mac_srs)
3528 {
3529 	mac_client_impl_t	*mcip;
3530 	mac_ring_t		*ring;
3531 	flow_entry_t		*flent;
3532 
3533 	ring = mac_srs->srs_ring;
3534 	if (mac_srs->srs_type & SRST_TX) {
3535 		ASSERT(ring == NULL);
3536 		return;
3537 	}
3538 
3539 	if (ring == NULL)
3540 		return;
3541 
3542 	/*
3543 	 * Broadcast flows don't have a client impl association, but they
3544 	 * use only soft rings.
3545 	 */
3546 	flent = mac_srs->srs_flent;
3547 	mcip = flent->fe_mcip;
3548 	ASSERT(mcip != NULL);
3549 
3550 	ring->mr_classify_type = MAC_NO_CLASSIFIER;
3551 	ring->mr_srs = NULL;
3552 }
3553 
3554 /*
3555  * Physical unlink and free of the data structures happen below. This is
3556  * driven from mac_flow_destroy(), on the last refrele of a flow.
3557  *
3558  * Assumes Rx srs is 1-1 mapped with an ring.
3559  */
3560 void
3561 mac_srs_free(mac_soft_ring_set_t *mac_srs)
3562 {
3563 	ASSERT(mac_srs->srs_mcip == NULL ||
3564 	    MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
3565 	ASSERT((mac_srs->srs_state & (SRS_CONDEMNED | SRS_CONDEMNED_DONE |
3566 	    SRS_PROC | SRS_PROC_FAST)) == (SRS_CONDEMNED | SRS_CONDEMNED_DONE));
3567 
3568 	mac_drop_chain(mac_srs->srs_first, "SRS free");
3569 	mac_srs_ring_free(mac_srs);
3570 	mac_srs_soft_rings_free(mac_srs);
3571 	mac_srs_fanout_list_free(mac_srs);
3572 
3573 	mac_srs->srs_bw = NULL;
3574 	mac_srs_stat_delete(mac_srs);
3575 	kmem_cache_free(mac_srs_cache, mac_srs);
3576 }
3577 
3578 static void
3579 mac_srs_soft_rings_quiesce(mac_soft_ring_set_t *mac_srs,
3580     const mac_soft_ring_state_t s_ring_flag)
3581 {
3582 	mac_soft_ring_t	*softring;
3583 
3584 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
3585 
3586 	mac_srs_soft_rings_signal(mac_srs, s_ring_flag);
3587 	if (s_ring_flag == S_RING_CONDEMNED) {
3588 		while (mac_srs->srs_soft_ring_condemned_count !=
3589 		    mac_srs->srs_soft_ring_count)
3590 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3591 	} else {
3592 		while (mac_srs->srs_soft_ring_quiesced_count !=
3593 		    mac_srs->srs_soft_ring_count)
3594 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3595 	}
3596 	mutex_exit(&mac_srs->srs_lock);
3597 
3598 	for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
3599 	    softring = softring->s_ring_next) {
3600 		(void) untimeout(softring->s_ring_tid);
3601 		softring->s_ring_tid = NULL;
3602 	}
3603 
3604 	(void) untimeout(mac_srs->srs_tid);
3605 	mac_srs->srs_tid = NULL;
3606 
3607 	mutex_enter(&mac_srs->srs_lock);
3608 }
3609 
3610 /*
3611  * The block comment above mac_rx_classify_flow_state_change explains the
3612  * background. At this point upcalls from the driver (both hardware classified
3613  * and software classified) have been cut off. We now need to quiesce the
3614  * SRS worker, poll, and softring threads. The SRS worker thread serves as
3615  * the master controller. The steps involved are described below in the function
3616  */
3617 void
3618 mac_srs_worker_quiesce(mac_soft_ring_set_t *mac_srs)
3619 {
3620 	VERIFY(MUTEX_HELD(&mac_srs->srs_lock));
3621 
3622 	VERIFY((mac_srs->srs_state & (SRS_CONDEMNED | SRS_QUIESCE)) != 0);
3623 	const boolean_t condemn = (mac_srs->srs_state & SRS_CONDEMNED) != 0;
3624 	const mac_soft_ring_state_t s_ring_flag = condemn ?
3625 	    S_RING_CONDEMNED : S_RING_QUIESCE;
3626 	const mac_soft_ring_set_state_t srs_poll_wait_flag = condemn ?
3627 	    SRS_POLL_THR_EXITED : SRS_POLL_THR_QUIESCED;
3628 
3629 	/*
3630 	 * In the case of Rx SRS wait till the poll thread is done.
3631 	 */
3632 	if ((mac_srs->srs_type & SRST_TX) == 0 &&
3633 	    mac_srs->srs_poll_thr != NULL) {
3634 		while (!(mac_srs->srs_state & srs_poll_wait_flag))
3635 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3636 
3637 		/*
3638 		 * Turn off polling as part of the quiesce operation.
3639 		 */
3640 		MAC_SRS_POLLING_OFF(mac_srs);
3641 		mac_srs->srs_state &= ~(SRS_POLLING | SRS_GET_PKTS);
3642 	}
3643 
3644 	/*
3645 	 * Then signal the soft ring worker threads to quiesce or quit
3646 	 * as needed and then wait till that happens.
3647 	 */
3648 	mac_srs_soft_rings_quiesce(mac_srs, s_ring_flag);
3649 
3650 	if (condemn)
3651 		mac_srs->srs_state |= (SRS_QUIESCE_DONE | SRS_CONDEMNED_DONE);
3652 	else
3653 		mac_srs->srs_state |= SRS_QUIESCE_DONE;
3654 	cv_signal(&mac_srs->srs_quiesce_done_cv);
3655 }
3656 
3657 /*
3658  * Signal an SRS to start a temporary quiesce, or permanent removal, or restart
3659  * a quiesced SRS by setting the appropriate flags and signaling the SRS worker
3660  * or poll thread. This function is internal to the quiescing logic and is
3661  * called internally from the SRS quiesce or flow quiesce or client quiesce
3662  * higher level functions.
3663  */
3664 void
3665 mac_srs_signal(mac_soft_ring_set_t *mac_srs,
3666     const mac_soft_ring_set_state_t srs_flag)
3667 {
3668 	mac_ring_t	*ring;
3669 
3670 	ring = mac_srs->srs_ring;
3671 	ASSERT(ring == NULL || ring->mr_refcnt == 0);
3672 
3673 	if (srs_flag == SRS_CONDEMNED) {
3674 		/*
3675 		 * The SRS is going away. We need to unbind the SRS and SR
3676 		 * threads before removing from the global SRS list. Otherwise
3677 		 * there is a small window where the cpu reconfig callbacks
3678 		 * may miss the SRS in the list walk and DR could fail since
3679 		 * there are still bound threads.
3680 		 */
3681 		mac_srs_threads_unbind(mac_srs);
3682 		mac_srs_remove_glist(mac_srs);
3683 	}
3684 	/*
3685 	 * Wakeup the SRS worker and poll threads.
3686 	 */
3687 	mutex_enter(&mac_srs->srs_lock);
3688 	mac_srs->srs_state |= srs_flag;
3689 	cv_signal(&mac_srs->srs_async);
3690 	cv_signal(&mac_srs->srs_cv);
3691 	mutex_exit(&mac_srs->srs_lock);
3692 }
3693 
3694 /*
3695  * In the Rx side, the quiescing is done bottom up. After the Rx upcalls
3696  * from the driver are done, then the Rx SRS is quiesced and only then can
3697  * we signal the soft rings. Thus this function can't be called arbitrarily
3698  * without satisfying the prerequisites. On the Tx side, the threads from
3699  * top need to quiesced, then the Tx SRS and only then can we signal the
3700  * Tx soft rings.
3701  */
3702 static void
3703 mac_srs_soft_rings_signal(mac_soft_ring_set_t *mac_srs,
3704     const mac_soft_ring_state_t sr_flag)
3705 {
3706 	mac_soft_ring_t		*softring;
3707 
3708 	for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
3709 	    softring = softring->s_ring_next)
3710 		mac_soft_ring_signal(softring, sr_flag);
3711 }
3712 
3713 /*
3714  * The block comment above mac_rx_classify_flow_state_change explains the
3715  * background. At this point the SRS is quiesced and we need to restart the
3716  * SRS worker, poll, and softring threads. The SRS worker thread serves as
3717  * the master controller. The steps involved are described below in the function
3718  */
3719 void
3720 mac_srs_worker_restart(mac_soft_ring_set_t *mac_srs)
3721 {
3722 	boolean_t	iam_rx_srs;
3723 	mac_soft_ring_t	*softring;
3724 
3725 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
3726 	if ((mac_srs->srs_type & SRST_TX) != 0) {
3727 		iam_rx_srs = B_FALSE;
3728 		ASSERT((mac_srs->srs_state &
3729 		    (SRS_POLL_THR_QUIESCED | SRS_QUIESCE_DONE | SRS_QUIESCE)) ==
3730 		    (SRS_QUIESCE_DONE | SRS_QUIESCE));
3731 	} else {
3732 		iam_rx_srs = B_TRUE;
3733 		ASSERT((mac_srs->srs_state &
3734 		    (SRS_QUIESCE_DONE | SRS_QUIESCE)) ==
3735 		    (SRS_QUIESCE_DONE | SRS_QUIESCE));
3736 		if (mac_srs->srs_poll_thr != NULL) {
3737 			ASSERT((mac_srs->srs_state & SRS_POLL_THR_QUIESCED) ==
3738 			    SRS_POLL_THR_QUIESCED);
3739 		}
3740 	}
3741 
3742 	/*
3743 	 * Signal any quiesced soft ring workers to restart and wait for the
3744 	 * soft ring down count to come down to zero.
3745 	 */
3746 	if (mac_srs->srs_soft_ring_quiesced_count != 0) {
3747 		for (softring = mac_srs->srs_soft_ring_head; softring != NULL;
3748 		    softring = softring->s_ring_next) {
3749 			if (!(softring->s_ring_state & S_RING_QUIESCE))
3750 				continue;
3751 			mac_soft_ring_signal(softring, S_RING_RESTART);
3752 		}
3753 		while (mac_srs->srs_soft_ring_quiesced_count != 0)
3754 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3755 	}
3756 
3757 	mac_srs->srs_state &= ~(SRS_QUIESCE_DONE | SRS_QUIESCE | SRS_RESTART);
3758 	if (iam_rx_srs && mac_srs->srs_poll_thr != NULL) {
3759 		/*
3760 		 * Signal the poll thread and ask it to restart. Wait till it
3761 		 * actually restarts and the SRS_POLL_THR_QUIESCED flag gets
3762 		 * cleared.
3763 		 */
3764 		mac_srs->srs_state |= SRS_POLL_THR_RESTART;
3765 		cv_signal(&mac_srs->srs_cv);
3766 		while (mac_srs->srs_state & SRS_POLL_THR_QUIESCED)
3767 			cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3768 		ASSERT(!(mac_srs->srs_state & SRS_POLL_THR_RESTART));
3769 	}
3770 	/* Wake up any waiter waiting for the restart to complete */
3771 	mac_srs->srs_state |= SRS_RESTART_DONE;
3772 	cv_signal(&mac_srs->srs_quiesce_done_cv);
3773 }
3774 
3775 static void
3776 mac_srs_worker_unbind(mac_soft_ring_set_t *mac_srs)
3777 {
3778 	mutex_enter(&mac_srs->srs_lock);
3779 	if (!(mac_srs->srs_state & SRS_WORKER_BOUND)) {
3780 		ASSERT(mac_srs->srs_worker_cpuid == -1);
3781 		mutex_exit(&mac_srs->srs_lock);
3782 		return;
3783 	}
3784 
3785 	mac_srs->srs_worker_cpuid = -1;
3786 	mac_srs->srs_state &= ~SRS_WORKER_BOUND;
3787 	thread_affinity_clear(mac_srs->srs_worker);
3788 	mutex_exit(&mac_srs->srs_lock);
3789 }
3790 
3791 static void
3792 mac_srs_poll_unbind(mac_soft_ring_set_t *mac_srs)
3793 {
3794 	mutex_enter(&mac_srs->srs_lock);
3795 	if (mac_srs->srs_poll_thr == NULL ||
3796 	    (mac_srs->srs_state & SRS_POLL_BOUND) == 0) {
3797 		ASSERT(mac_srs->srs_poll_cpuid == -1);
3798 		mutex_exit(&mac_srs->srs_lock);
3799 		return;
3800 	}
3801 
3802 	mac_srs->srs_poll_cpuid = -1;
3803 	mac_srs->srs_state &= ~SRS_POLL_BOUND;
3804 	thread_affinity_clear(mac_srs->srs_poll_thr);
3805 	mutex_exit(&mac_srs->srs_lock);
3806 }
3807 
3808 static void
3809 mac_srs_threads_unbind(mac_soft_ring_set_t *mac_srs)
3810 {
3811 	mac_soft_ring_t	*soft_ring;
3812 
3813 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mac_srs->srs_mcip->mci_mip));
3814 
3815 	mutex_enter(&cpu_lock);
3816 	mac_srs_worker_unbind(mac_srs);
3817 	if (!(mac_srs->srs_type & SRST_TX))
3818 		mac_srs_poll_unbind(mac_srs);
3819 
3820 	for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
3821 	    soft_ring = soft_ring->s_ring_next) {
3822 		mac_soft_ring_unbind(soft_ring);
3823 	}
3824 	mutex_exit(&cpu_lock);
3825 }
3826 
3827 /*
3828  * When a CPU is going away, unbind all MAC threads which are bound
3829  * to that CPU. The affinity of the thread to the CPU is saved to allow
3830  * the thread to be rebound to the CPU if it comes back online.
3831  */
3832 static void
3833 mac_walk_srs_and_unbind(int cpuid)
3834 {
3835 	mac_soft_ring_set_t *mac_srs;
3836 	mac_soft_ring_t *soft_ring;
3837 
3838 	rw_enter(&mac_srs_g_lock, RW_READER);
3839 
3840 	if ((mac_srs = mac_srs_g_list) == NULL)
3841 		goto done;
3842 
3843 	for (; mac_srs != NULL; mac_srs = mac_srs->srs_next) {
3844 		if (mac_srs->srs_worker_cpuid == cpuid) {
3845 			mac_srs->srs_worker_cpuid_save = cpuid;
3846 			mac_srs_worker_unbind(mac_srs);
3847 		}
3848 
3849 		if (!(mac_srs->srs_type & SRST_TX)) {
3850 			if (mac_srs->srs_poll_cpuid == cpuid) {
3851 				mac_srs->srs_poll_cpuid_save = cpuid;
3852 				mac_srs_poll_unbind(mac_srs);
3853 			}
3854 		}
3855 
3856 		/* Next tackle the soft rings associated with the srs */
3857 		mutex_enter(&mac_srs->srs_lock);
3858 		for (soft_ring = mac_srs->srs_soft_ring_head; soft_ring != NULL;
3859 		    soft_ring = soft_ring->s_ring_next) {
3860 			if (soft_ring->s_ring_cpuid == cpuid) {
3861 				soft_ring->s_ring_cpuid_save = cpuid;
3862 				mac_soft_ring_unbind(soft_ring);
3863 			}
3864 		}
3865 		mutex_exit(&mac_srs->srs_lock);
3866 	}
3867 done:
3868 	rw_exit(&mac_srs_g_lock);
3869 }
3870 
3871 /* TX SETUP and TEARDOWN ROUTINES */
3872 
3873 /*
3874  * XXXHIO need to make sure the two mac_tx_srs_{add,del}_ring()
3875  * handle the case where the number of rings is one. I.e. there is
3876  * a ring pointed to by mac_srs->srs_tx_arg2.
3877  */
3878 void
3879 mac_tx_srs_add_ring(mac_soft_ring_set_t *mac_srs, mac_ring_t *tx_ring)
3880 {
3881 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
3882 	mac_soft_ring_t *soft_ring;
3883 	int count = mac_srs->srs_tx_ring_count;
3884 	mac_soft_ring_state_t soft_ring_type = 0;
3885 	uint_t ring_info;
3886 
3887 	ASSERT(mac_srs->srs_state & SRS_QUIESCE);
3888 	ring_info = mac_hwring_getinfo((mac_ring_handle_t)tx_ring);
3889 	if (mac_tx_serialize || (ring_info & MAC_RING_TX_SERIALIZE))
3890 		soft_ring_type |= ST_RING_WORKER_ONLY;
3891 	soft_ring = mac_soft_ring_create_tx(count, 0, soft_ring_type,
3892 	    maxclsyspri, mcip, mac_srs, -1, tx_ring);
3893 	mac_srs->srs_tx_ring_count++;
3894 	mac_srs_update_fanout_list(mac_srs);
3895 	/*
3896 	 * put this soft ring in quiesce mode too so when we restart
3897 	 * all soft rings in the srs are in the same state.
3898 	 */
3899 	mac_soft_ring_signal(soft_ring, S_RING_QUIESCE);
3900 }
3901 
3902 static void
3903 mac_soft_ring_remove(mac_soft_ring_set_t *mac_srs, mac_soft_ring_t *softring)
3904 {
3905 	int sringcnt;
3906 
3907 	mutex_enter(&mac_srs->srs_lock);
3908 	sringcnt = mac_srs->srs_soft_ring_count;
3909 	ASSERT(sringcnt > 0);
3910 	mac_soft_ring_signal(softring, S_RING_CONDEMNED);
3911 
3912 	ASSERT(mac_srs->srs_soft_ring_condemned_count == 0);
3913 	while (mac_srs->srs_soft_ring_condemned_count != 1)
3914 		cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
3915 
3916 	if (softring == mac_srs->srs_soft_ring_head) {
3917 		mac_srs->srs_soft_ring_head = softring->s_ring_next;
3918 		if (mac_srs->srs_soft_ring_head != NULL) {
3919 			mac_srs->srs_soft_ring_head->s_ring_prev = NULL;
3920 		} else {
3921 			mac_srs->srs_soft_ring_tail = NULL;
3922 		}
3923 	} else {
3924 		softring->s_ring_prev->s_ring_next =
3925 		    softring->s_ring_next;
3926 		if (softring->s_ring_next != NULL) {
3927 			softring->s_ring_next->s_ring_prev =
3928 			    softring->s_ring_prev;
3929 		} else {
3930 			mac_srs->srs_soft_ring_tail =
3931 			    softring->s_ring_prev;
3932 		}
3933 	}
3934 	mac_srs->srs_soft_ring_count--;
3935 
3936 	mac_srs->srs_soft_ring_condemned_count--;
3937 	mutex_exit(&mac_srs->srs_lock);
3938 
3939 	mac_soft_ring_free(softring);
3940 }
3941 
3942 void
3943 mac_tx_srs_del_ring(mac_soft_ring_set_t *mac_srs, mac_ring_t *tx_ring)
3944 {
3945 	int i;
3946 	mac_soft_ring_t *soft_ring, *remove_sring;
3947 	mac_client_impl_t *mcip = mac_srs->srs_mcip;
3948 
3949 	mutex_enter(&mac_srs->srs_lock);
3950 	for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
3951 		soft_ring =  mac_srs->srs_tx_soft_rings[i];
3952 		if (soft_ring->s_ring_tx_arg2 == tx_ring)
3953 			break;
3954 	}
3955 	mutex_exit(&mac_srs->srs_lock);
3956 	ASSERT(i < mac_srs->srs_tx_ring_count);
3957 	remove_sring = soft_ring;
3958 	/*
3959 	 * In the case of aggr, the soft ring associated with a Tx ring
3960 	 * is also stored in st_soft_rings[] array. That entry should
3961 	 * be removed.
3962 	 */
3963 	if (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT) {
3964 		mac_srs_tx_t *tx = &mac_srs->srs_tx;
3965 
3966 		ASSERT(tx->st_soft_rings[tx_ring->mr_index] == remove_sring);
3967 		tx->st_soft_rings[tx_ring->mr_index] = NULL;
3968 	}
3969 	mac_soft_ring_remove(mac_srs, remove_sring);
3970 	mac_srs_update_fanout_list(mac_srs);
3971 }
3972 
3973 /*
3974  * mac_tx_srs_setup():
3975  * Used to setup Tx rings. If no free Tx ring is available, then default
3976  * Tx ring is used.
3977  */
3978 void
3979 mac_tx_srs_setup(mac_client_impl_t *mcip, flow_entry_t *flent)
3980 {
3981 	mac_impl_t		*mip = mcip->mci_mip;
3982 	mac_soft_ring_set_t	*tx_srs = flent->fe_tx_srs;
3983 	int			i;
3984 	int			tx_ring_count = 0;
3985 	mac_group_t		*grp = NULL;
3986 	mac_ring_t		*ring;
3987 	mac_srs_tx_t		*tx = &tx_srs->srs_tx;
3988 	boolean_t		is_aggr;
3989 	uint_t			ring_info = 0;
3990 
3991 	is_aggr = (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT) != 0;
3992 	grp = flent->fe_tx_ring_group;
3993 	if (grp == NULL) {
3994 		ring = (mac_ring_t *)mip->mi_default_tx_ring;
3995 		goto no_group;
3996 	}
3997 	tx_ring_count = grp->mrg_cur_count;
3998 	ring = grp->mrg_rings;
3999 	/*
4000 	 * An attempt is made to reserve 'tx_ring_count' number
4001 	 * of Tx rings. If tx_ring_count is 0, default Tx ring
4002 	 * is used. If it is 1, an attempt is made to reserve one
4003 	 * Tx ring. In both the cases, the ring information is
4004 	 * stored in Tx SRS. If multiple Tx rings are specified,
4005 	 * then each Tx ring will have a Tx-side soft ring. All
4006 	 * these soft rings will be hang off Tx SRS.
4007 	 */
4008 	switch (grp->mrg_state) {
4009 		case MAC_GROUP_STATE_SHARED:
4010 		case MAC_GROUP_STATE_RESERVED:
4011 			if (tx_ring_count <= 1 && !is_aggr) {
4012 no_group:
4013 				if (ring != NULL &&
4014 				    ring->mr_state != MR_INUSE) {
4015 					(void) mac_start_ring(ring);
4016 					ring_info = mac_hwring_getinfo(
4017 					    (mac_ring_handle_t)ring);
4018 				}
4019 				tx->st_arg2 = (void *)ring;
4020 				mac_tx_srs_stat_recreate(tx_srs, B_FALSE);
4021 				if (tx_srs->srs_type & SRST_BW_CONTROL) {
4022 					tx->st_mode = SRS_TX_BW;
4023 				} else if (mac_tx_serialize ||
4024 				    (ring_info & MAC_RING_TX_SERIALIZE)) {
4025 					tx->st_mode = SRS_TX_SERIALIZE;
4026 				} else {
4027 					tx->st_mode = SRS_TX_DEFAULT;
4028 				}
4029 				break;
4030 			}
4031 			if (tx_srs->srs_type & SRST_BW_CONTROL) {
4032 				tx->st_mode = is_aggr ?
4033 				    SRS_TX_BW_AGGR : SRS_TX_BW_FANOUT;
4034 			} else {
4035 				tx->st_mode = is_aggr ? SRS_TX_AGGR :
4036 				    SRS_TX_FANOUT;
4037 			}
4038 			for (i = 0; i < tx_ring_count; i++) {
4039 				VERIFY(ring != NULL);
4040 				mac_soft_ring_state_t soft_ring_type = 0;
4041 
4042 				switch (ring->mr_state) {
4043 				case MR_INUSE:
4044 				case MR_FREE:
4045 					VERIFY3P(ring->mr_srs, ==, NULL);
4046 
4047 					if (ring->mr_state != MR_INUSE)
4048 						(void) mac_start_ring(ring);
4049 					ring_info = mac_hwring_getinfo(
4050 					    (mac_ring_handle_t)ring);
4051 					if (mac_tx_serialize || (ring_info &
4052 					    MAC_RING_TX_SERIALIZE)) {
4053 						soft_ring_type |=
4054 						    ST_RING_WORKER_ONLY;
4055 					}
4056 					(void) mac_soft_ring_create_tx(i, 0,
4057 					    soft_ring_type, maxclsyspri,
4058 					    mcip, tx_srs, -1, ring);
4059 					break;
4060 				default:
4061 					cmn_err(CE_PANIC,
4062 					    "srs_setup: mcip = %p "
4063 					    "trying to add UNKNOWN ring = %p\n",
4064 					    (void *)mcip, (void *)ring);
4065 					break;
4066 				}
4067 				ring = ring->mr_next;
4068 			}
4069 			mac_srs_update_fanout_list(tx_srs);
4070 			break;
4071 		default:
4072 			ASSERT(B_FALSE);
4073 			break;
4074 	}
4075 	tx->st_func = mac_tx_get_func(tx->st_mode);
4076 	if (is_aggr) {
4077 		VERIFY(i_mac_capab_get((mac_handle_t)mip,
4078 		    MAC_CAPAB_AGGR, &tx->st_capab_aggr));
4079 	}
4080 	DTRACE_PROBE3(tx__srs___setup__return, mac_soft_ring_set_t *, tx_srs,
4081 	    mac_tx_srs_mode_t, tx->st_mode, int, tx_srs->srs_tx_ring_count);
4082 }
4083 
4084 /*
4085  * Update the fanout of a client if its recorded link speed doesn't match
4086  * its current link speed.
4087  */
4088 void
4089 mac_fanout_recompute_client(mac_client_impl_t *mcip, cpupart_t *cpupart)
4090 {
4091 	uint64_t link_speed;
4092 	mac_resource_props_t *mcip_mrp;
4093 	flow_entry_t *flent = mcip->mci_flent;
4094 	mac_soft_ring_set_t *rx_srs;
4095 	mac_cpus_t *srs_cpu;
4096 	int soft_ring_count, maxcpus;
4097 
4098 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4099 
4100 	link_speed = mac_client_stat_get(
4101 	    (mac_client_handle_t)mcip->mci_flent->fe_mcip, MAC_STAT_IFSPEED);
4102 
4103 	if ((link_speed != 0) &&
4104 	    (link_speed != mcip->mci_flent->fe_nic_speed)) {
4105 		mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
4106 		/*
4107 		 * Before calling mac_fanout_setup(), check to see if
4108 		 * the SRSes already have the right number of soft
4109 		 * rings. mac_fanout_setup() is a heavy duty operation
4110 		 * where new cpu bindings are done for SRS and soft
4111 		 * ring threads and interrupts re-targeted.
4112 		 */
4113 		maxcpus = (cpupart != NULL) ? cpupart->cp_ncpus : ncpus;
4114 		soft_ring_count = mac_compute_soft_ring_count(flent,
4115 		    flent->fe_rx_srs_cnt - 1, maxcpus);
4116 		/*
4117 		 * If soft_ring_count returned by
4118 		 * mac_compute_soft_ring_count() is 0, bump it
4119 		 * up by 1 because we always have atleast one
4120 		 * TCP, UDP, and OTH soft ring associated with
4121 		 * an SRS.
4122 		 */
4123 		soft_ring_count = (soft_ring_count == 0) ?
4124 		    1 : soft_ring_count;
4125 		rx_srs = flent->fe_rx_srs[0];
4126 		srs_cpu = &rx_srs->srs_cpu;
4127 		if (soft_ring_count != srs_cpu->mc_rx_fanout_cnt) {
4128 			mac_fanout_setup(mcip, flent, mcip_mrp,
4129 			    mac_rx_deliver, mcip, cpupart);
4130 		}
4131 	}
4132 }
4133 
4134 /*
4135  * Walk through the list of MAC clients for the MAC.
4136  * For each active MAC client, recompute the number of soft rings
4137  * associated with every client, only if current speed is different
4138  * from the speed that was previously used for soft ring computation.
4139  * If the cable is disconnected whlie the NIC is started, we would get
4140  * notification with speed set to 0. We do not recompute in that case.
4141  */
4142 void
4143 mac_fanout_recompute(mac_impl_t *mip)
4144 {
4145 	mac_client_impl_t	*mcip;
4146 	cpupart_t		*cpupart;
4147 	boolean_t		use_default;
4148 	mac_resource_props_t	*mrp, *emrp;
4149 
4150 	i_mac_perim_enter(mip);
4151 	if ((mip->mi_state_flags & MIS_IS_VNIC) != 0 ||
4152 	    mip->mi_linkstate != LINK_STATE_UP) {
4153 		i_mac_perim_exit(mip);
4154 		return;
4155 	}
4156 
4157 	for (mcip = mip->mi_clients_list; mcip != NULL;
4158 	    mcip = mcip->mci_client_next) {
4159 		/* Aggr port clients don't have SRSes. */
4160 		if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) != 0)
4161 			continue;
4162 
4163 		if ((mcip->mci_state_flags & MCIS_SHARE_BOUND) != 0 ||
4164 		    !MCIP_DATAPATH_SETUP(mcip))
4165 			continue;
4166 		mrp = MCIP_RESOURCE_PROPS(mcip);
4167 		emrp = MCIP_EFFECTIVE_PROPS(mcip);
4168 		use_default = B_FALSE;
4169 		pool_lock();
4170 		cpupart = mac_pset_find(mrp, &use_default);
4171 		mac_fanout_recompute_client(mcip, cpupart);
4172 		mac_set_pool_effective(use_default, cpupart, mrp, emrp);
4173 		pool_unlock();
4174 	}
4175 
4176 	i_mac_perim_exit(mip);
4177 }
4178 
4179 /*
4180  * Given a MAC, change the polling state for all its MAC clients.  'enable' is
4181  * B_TRUE to enable polling or B_FALSE to disable.  Polling is enabled by
4182  * default.
4183  */
4184 void
4185 mac_poll_state_change(mac_handle_t mh, boolean_t enable)
4186 {
4187 	mac_impl_t *mip = (mac_impl_t *)mh;
4188 	mac_client_impl_t *mcip;
4189 
4190 	i_mac_perim_enter(mip);
4191 	if (enable)
4192 		mip->mi_state_flags &= ~MIS_POLL_DISABLE;
4193 	else
4194 		mip->mi_state_flags |= MIS_POLL_DISABLE;
4195 	for (mcip = mip->mi_clients_list; mcip != NULL;
4196 	    mcip = mcip->mci_client_next)
4197 		mac_client_update_classifier(mcip, B_TRUE);
4198 	i_mac_perim_exit(mip);
4199 }
4200