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