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