xref: /titanic_51/usr/src/uts/common/io/mac/mac_sched.c (revision 5d460eafffba936e81c4dd5ebe0f59b238f09121)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/callb.h>
28 #include <sys/sdt.h>
29 #include <sys/strsubr.h>
30 #include <sys/strsun.h>
31 #include <sys/vlan.h>
32 #include <inet/ipsec_impl.h>
33 #include <inet/ip_impl.h>
34 #include <inet/sadb.h>
35 #include <inet/ipsecesp.h>
36 #include <inet/ipsecah.h>
37 #include <inet/ip6.h>
38 
39 #include <sys/mac_impl.h>
40 #include <sys/mac_client_impl.h>
41 #include <sys/mac_client_priv.h>
42 #include <sys/mac_soft_ring.h>
43 #include <sys/mac_flow_impl.h>
44 
45 static mac_tx_cookie_t mac_tx_single_ring_mode(mac_soft_ring_set_t *, mblk_t *,
46     uintptr_t, uint16_t, mblk_t **);
47 static mac_tx_cookie_t mac_tx_serializer_mode(mac_soft_ring_set_t *, mblk_t *,
48     uintptr_t, uint16_t, mblk_t **);
49 static mac_tx_cookie_t mac_tx_fanout_mode(mac_soft_ring_set_t *, mblk_t *,
50     uintptr_t, uint16_t, mblk_t **);
51 static mac_tx_cookie_t mac_tx_bw_mode(mac_soft_ring_set_t *, mblk_t *,
52     uintptr_t, uint16_t, mblk_t **);
53 
54 typedef struct mac_tx_mode_s {
55 	mac_tx_srs_mode_t	mac_tx_mode;
56 	mac_tx_func_t		mac_tx_func;
57 } mac_tx_mode_t;
58 
59 /*
60  * There are five modes of operation on the Tx side. These modes get set
61  * in mac_tx_srs_setup(). Except for the experimental TX_SERIALIZE mode,
62  * none of the other modes are user configurable. They get selected by
63  * the system depending upon whether the link (or flow) has multiple Tx
64  * rings or a bandwidth configured, etc.
65  */
66 mac_tx_mode_t mac_tx_mode_list[] = {
67 	{SRS_TX_DEFAULT,	mac_tx_single_ring_mode},
68 	{SRS_TX_SERIALIZE,	mac_tx_serializer_mode},
69 	{SRS_TX_FANOUT,		mac_tx_fanout_mode},
70 	{SRS_TX_BW,		mac_tx_bw_mode},
71 	{SRS_TX_BW_FANOUT,	mac_tx_bw_mode}
72 };
73 
74 /*
75  * Soft Ring Set (SRS) - The Run time code that deals with
76  * dynamic polling from the hardware, bandwidth enforcement,
77  * fanout etc.
78  *
79  * We try to use H/W classification on NIC and assign traffic for
80  * a MAC address to a particular Rx ring or ring group. There is a
81  * 1-1 mapping between a SRS and a Rx ring. The SRS dynamically
82  * switches the underlying Rx ring between interrupt and
83  * polling mode and enforces any specified B/W control.
84  *
85  * There is always a SRS created and tied to each H/W and S/W rule.
86  * Whenever we create a H/W rule, we always add the the same rule to
87  * S/W classifier and tie a SRS to it.
88  *
89  * In case a B/W control is specified, it is broken into bytes
90  * per ticks and as soon as the quota for a tick is exhausted,
91  * the underlying Rx ring is forced into poll mode for remainder of
92  * the tick. The SRS poll thread only polls for bytes that are
93  * allowed to come in the SRS. We typically let 4x the configured
94  * B/W worth of packets to come in the SRS (to prevent unnecessary
95  * drops due to bursts) but only process the specified amount.
96  *
97  * A MAC client (e.g. a VNIC or aggr) can have 1 or more
98  * Rx rings (and corresponding SRSs) assigned to it. The SRS
99  * in turn can have softrings to do protocol level fanout or
100  * softrings to do S/W based fanout or both. In case the NIC
101  * has no Rx rings, we do S/W classification to respective SRS.
102  * The S/W classification rule is always setup and ready. This
103  * allows the MAC layer to reassign Rx rings whenever needed
104  * but packets still continue to flow via the default path and
105  * getting S/W classified to correct SRS.
106  *
107  * The SRS's are used on both Tx and Rx side. They use the same
108  * data structure but the processing routines have slightly different
109  * semantics due to the fact that Rx side needs to do dynamic
110  * polling etc.
111  *
112  * Dynamic Polling Notes
113  * =====================
114  *
115  * Each Soft ring set is capable of switching its Rx ring between
116  * interrupt and poll mode and actively 'polls' for packets in
117  * poll mode. If the SRS is implementing a B/W limit, it makes
118  * sure that only Max allowed packets are pulled in poll mode
119  * and goes to poll mode as soon as B/W limit is exceeded. As
120  * such, there are no overheads to implement B/W limits.
121  *
122  * In poll mode, its better to keep the pipeline going where the
123  * SRS worker thread keeps processing packets and poll thread
124  * keeps bringing more packets (specially if they get to run
125  * on different CPUs). This also prevents the overheads associated
126  * by excessive signalling (on NUMA machines, this can be
127  * pretty devastating). The exception is latency optimized case
128  * where worker thread does no work and interrupt and poll thread
129  * are allowed to do their own drain.
130  *
131  * We use the following policy to control Dynamic Polling:
132  * 1) We switch to poll mode anytime the processing
133  *    thread causes a backlog to build up in SRS and
134  *    its associated Soft Rings (sr_poll_pkt_cnt > 0).
135  * 2) As long as the backlog stays under the low water
136  *    mark (sr_lowat), we poll the H/W for more packets.
137  * 3) If the backlog (sr_poll_pkt_cnt) exceeds low
138  *    water mark, we stay in poll mode but don't poll
139  *    the H/W for more packets.
140  * 4) Anytime in polling mode, if we poll the H/W for
141  *    packets and find nothing plus we have an existing
142  *    backlog (sr_poll_pkt_cnt > 0), we stay in polling
143  *    mode but don't poll the H/W for packets anymore
144  *    (let the polling thread go to sleep).
145  * 5) Once the backlog is relived (packets are processed)
146  *    we reenable polling (by signalling the poll thread)
147  *    only when the backlog dips below sr_poll_thres.
148  * 6) sr_hiwat is used exclusively when we are not
149  *    polling capable and is used to decide when to
150  *    drop packets so the SRS queue length doesn't grow
151  *    infinitely.
152  *
153  * NOTE: Also see the block level comment on top of mac_soft_ring.c
154  */
155 
156 /*
157  * mac_latency_optimize
158  *
159  * Controls whether the poll thread can process the packets inline
160  * or let the SRS worker thread do the processing. This applies if
161  * the SRS was not being processed. For latency sensitive traffic,
162  * this needs to be true to allow inline processing. For throughput
163  * under load, this should be false.
164  *
165  * This (and other similar) tunable should be rolled into a link
166  * or flow specific workload hint that can be set using dladm
167  * linkprop (instead of multiple such tunables).
168  */
169 boolean_t mac_latency_optimize = B_TRUE;
170 
171 /*
172  * MAC_RX_SRS_ENQUEUE_CHAIN and MAC_TX_SRS_ENQUEUE_CHAIN
173  *
174  * queue a mp or chain in soft ring set and increment the
175  * local count (srs_count) for the SRS and the shared counter
176  * (srs_poll_pkt_cnt - shared between SRS and its soft rings
177  * to track the total unprocessed packets for polling to work
178  * correctly).
179  *
180  * The size (total bytes queued) counters are incremented only
181  * if we are doing B/W control.
182  */
183 #define	MAC_SRS_ENQUEUE_CHAIN(mac_srs, head, tail, count, sz) {		\
184 	ASSERT(MUTEX_HELD(&(mac_srs)->srs_lock));			\
185 	if ((mac_srs)->srs_last != NULL)				\
186 		(mac_srs)->srs_last->b_next = (head);			\
187 	else								\
188 		(mac_srs)->srs_first = (head);				\
189 	(mac_srs)->srs_last = (tail);					\
190 	(mac_srs)->srs_count += count;					\
191 }
192 
193 #define	MAC_RX_SRS_ENQUEUE_CHAIN(mac_srs, head, tail, count, sz) {	\
194 	mac_srs_rx_t	*srs_rx = &(mac_srs)->srs_rx;			\
195 									\
196 	MAC_SRS_ENQUEUE_CHAIN(mac_srs, head, tail, count, sz);		\
197 	srs_rx->sr_poll_pkt_cnt += count;				\
198 	ASSERT(srs_rx->sr_poll_pkt_cnt > 0);				\
199 	if ((mac_srs)->srs_type & SRST_BW_CONTROL) {			\
200 		(mac_srs)->srs_size += (sz);				\
201 		mutex_enter(&(mac_srs)->srs_bw->mac_bw_lock);		\
202 		(mac_srs)->srs_bw->mac_bw_sz += (sz);			\
203 		mutex_exit(&(mac_srs)->srs_bw->mac_bw_lock);		\
204 	}								\
205 }
206 
207 #define	MAC_TX_SRS_ENQUEUE_CHAIN(mac_srs, head, tail, count, sz) {	\
208 	mac_srs->srs_state |= SRS_ENQUEUED;				\
209 	MAC_SRS_ENQUEUE_CHAIN(mac_srs, head, tail, count, sz);		\
210 	if ((mac_srs)->srs_type & SRST_BW_CONTROL) {			\
211 		(mac_srs)->srs_size += (sz);				\
212 		(mac_srs)->srs_bw->mac_bw_sz += (sz);			\
213 	}								\
214 }
215 
216 /*
217  * Turn polling on routines
218  */
219 #define	MAC_SRS_POLLING_ON(mac_srs) {					\
220 	ASSERT(MUTEX_HELD(&(mac_srs)->srs_lock));			\
221 	if (((mac_srs)->srs_state &					\
222 	    (SRS_POLLING_CAPAB|SRS_POLLING)) == SRS_POLLING_CAPAB) {	\
223 		(mac_srs)->srs_state |= SRS_POLLING;			\
224 		(void) mac_hwring_disable_intr((mac_ring_handle_t)	\
225 		    (mac_srs)->srs_ring);				\
226 		(mac_srs)->srs_rx.sr_poll_on++;				\
227 	}								\
228 }
229 
230 #define	MAC_SRS_WORKER_POLLING_ON(mac_srs) {				\
231 	ASSERT(MUTEX_HELD(&(mac_srs)->srs_lock));			\
232 	if (((mac_srs)->srs_state &					\
233 	    (SRS_POLLING_CAPAB|SRS_WORKER|SRS_POLLING)) == 		\
234 	    (SRS_POLLING_CAPAB|SRS_WORKER)) {				\
235 		(mac_srs)->srs_state |= SRS_POLLING;			\
236 		(void) mac_hwring_disable_intr((mac_ring_handle_t)	\
237 		    (mac_srs)->srs_ring);				\
238 		(mac_srs)->srs_rx.sr_worker_poll_on++;			\
239 	}								\
240 }
241 
242 /*
243  * MAC_SRS_POLL_RING
244  *
245  * Signal the SRS poll thread to poll the underlying H/W ring
246  * provided it wasn't already polling (SRS_GET_PKTS was set).
247  *
248  * Poll thread gets to run only from mac_rx_srs_drain() and only
249  * if the drain was being done by the worker thread.
250  */
251 #define	MAC_SRS_POLL_RING(mac_srs) {					\
252 	mac_srs_rx_t	*srs_rx = &(mac_srs)->srs_rx;			\
253 									\
254 	ASSERT(MUTEX_HELD(&(mac_srs)->srs_lock));			\
255 	srs_rx->sr_poll_thr_sig++;					\
256 	if (((mac_srs)->srs_state & 					\
257 	    (SRS_POLLING_CAPAB|SRS_WORKER|SRS_GET_PKTS)) ==		\
258 		(SRS_WORKER|SRS_POLLING_CAPAB)) {			\
259 		(mac_srs)->srs_state |= SRS_GET_PKTS;			\
260 		cv_signal(&(mac_srs)->srs_cv);   			\
261 	} else {							\
262 		srs_rx->sr_poll_thr_busy++;				\
263 	}								\
264 }
265 
266 /*
267  * MAC_SRS_CHECK_BW_CONTROL
268  *
269  * Check to see if next tick has started so we can reset the
270  * SRS_BW_ENFORCED flag and allow more packets to come in the
271  * system.
272  */
273 #define	MAC_SRS_CHECK_BW_CONTROL(mac_srs) {				\
274 	ASSERT(MUTEX_HELD(&(mac_srs)->srs_lock));			\
275 	ASSERT(((mac_srs)->srs_type & SRST_TX) ||			\
276 	    MUTEX_HELD(&(mac_srs)->srs_bw->mac_bw_lock));		\
277 	if ((mac_srs)->srs_bw->mac_bw_curr_time != lbolt) {    		\
278 		(mac_srs)->srs_bw->mac_bw_curr_time = lbolt;   		\
279 		(mac_srs)->srs_bw->mac_bw_used = 0;	       		\
280 		if ((mac_srs)->srs_bw->mac_bw_state & SRS_BW_ENFORCED)	\
281 			(mac_srs)->srs_bw->mac_bw_state &= ~SRS_BW_ENFORCED; \
282 	}								\
283 }
284 
285 /*
286  * MAC_SRS_WORKER_WAKEUP
287  *
288  * Wake up the SRS worker thread to process the queue as long as
289  * no one else is processing the queue. If we are optimizing for
290  * latency, we wake up the worker thread immediately or else we
291  * wait mac_srs_worker_wakeup_ticks before worker thread gets
292  * woken up.
293  */
294 int mac_srs_worker_wakeup_ticks = 0;
295 #define	MAC_SRS_WORKER_WAKEUP(mac_srs) {				\
296 	ASSERT(MUTEX_HELD(&(mac_srs)->srs_lock));			\
297 	if (!((mac_srs)->srs_state & SRS_PROC) &&			\
298 		(mac_srs)->srs_tid == NULL) {				\
299 		if (mac_latency_optimize ||				\
300 			(mac_srs_worker_wakeup_ticks == 0))		\
301 			cv_signal(&(mac_srs)->srs_async);		\
302 		else							\
303 			(mac_srs)->srs_tid =				\
304 				timeout(mac_srs_fire, (mac_srs),	\
305 					mac_srs_worker_wakeup_ticks);	\
306 	}								\
307 }
308 
309 #define	TX_SINGLE_RING_MODE(mac_srs)				\
310 	((mac_srs)->srs_tx.st_mode == SRS_TX_DEFAULT || 	\
311 	    (mac_srs)->srs_tx.st_mode == SRS_TX_SERIALIZE ||	\
312 	    (mac_srs)->srs_tx.st_mode == SRS_TX_BW)
313 
314 #define	TX_BANDWIDTH_MODE(mac_srs)				\
315 	((mac_srs)->srs_tx.st_mode == SRS_TX_BW ||		\
316 	    (mac_srs)->srs_tx.st_mode == SRS_TX_BW_FANOUT)
317 
318 #define	TX_SRS_TO_SOFT_RING(mac_srs, head, hint) {			\
319 	uint_t hash, indx;						\
320 	hash = HASH_HINT(hint);					\
321 	indx = COMPUTE_INDEX(hash, mac_srs->srs_oth_ring_count);	\
322 	softring = mac_srs->srs_oth_soft_rings[indx];			\
323 	(void) (mac_tx_soft_ring_process(softring, head, 0, NULL));	\
324 }
325 
326 /*
327  * MAC_TX_SRS_BLOCK
328  *
329  * Always called from mac_tx_srs_drain() function. SRS_TX_BLOCKED
330  * will be set only if srs_tx_woken_up is FALSE. If
331  * srs_tx_woken_up is TRUE, it indicates that the wakeup arrived
332  * before we grabbed srs_lock to set SRS_TX_BLOCKED. We need to
333  * attempt to transmit again and not setting SRS_TX_BLOCKED does
334  * that.
335  */
336 #define	MAC_TX_SRS_BLOCK(srs, mp)	{			\
337 	ASSERT(MUTEX_HELD(&(srs)->srs_lock));			\
338 	if ((srs)->srs_tx.st_woken_up) {			\
339 		(srs)->srs_tx.st_woken_up = B_FALSE;		\
340 	} else {						\
341 		ASSERT(!((srs)->srs_state & SRS_TX_BLOCKED));	\
342 		(srs)->srs_state |= SRS_TX_BLOCKED;		\
343 		(srs)->srs_tx.st_blocked_cnt++;			\
344 	}							\
345 }
346 
347 /*
348  * MAC_TX_SRS_TEST_HIWAT
349  *
350  * Called before queueing a packet onto Tx SRS to test and set
351  * SRS_TX_HIWAT if srs_count exceeds srs_tx_hiwat.
352  */
353 #define	MAC_TX_SRS_TEST_HIWAT(srs, mp, tail, cnt, sz, cookie) {		\
354 	boolean_t enqueue = 1;						\
355 									\
356 	if ((srs)->srs_count > (srs)->srs_tx.st_hiwat) {		\
357 		/*							\
358 		 * flow-controlled. Store srs in cookie so that it	\
359 		 * can be returned as mac_tx_cookie_t to client		\
360 		 */							\
361 		(srs)->srs_state |= SRS_TX_HIWAT;			\
362 		cookie = (mac_tx_cookie_t)srs;				\
363 		(srs)->srs_tx.st_hiwat_cnt++;				\
364 		if ((srs)->srs_count > (srs)->srs_tx.st_max_q_cnt) {	\
365 			/* increment freed stats */			\
366 			(srs)->srs_tx.st_drop_count += cnt;		\
367 			/*						\
368 			 * b_prev may be set to the fanout hint		\
369 			 * hence can't use freemsg directly		\
370 			 */						\
371 			mac_pkt_drop(NULL, NULL, mp_chain, B_FALSE);	\
372 			DTRACE_PROBE1(tx_queued_hiwat,			\
373 			    mac_soft_ring_set_t *, srs);		\
374 			enqueue = 0;					\
375 		}							\
376 	}								\
377 	if (enqueue)							\
378 		MAC_TX_SRS_ENQUEUE_CHAIN(srs, mp, tail, cnt, sz);	\
379 }
380 
381 /* Some utility macros */
382 #define	MAC_SRS_BW_LOCK(srs)						\
383 	if (!(srs->srs_type & SRST_TX))					\
384 		mutex_enter(&srs->srs_bw->mac_bw_lock);
385 
386 #define	MAC_SRS_BW_UNLOCK(srs)						\
387 	if (!(srs->srs_type & SRST_TX))					\
388 		mutex_exit(&srs->srs_bw->mac_bw_lock);
389 
390 #define	MAC_TX_SRS_DROP_MESSAGE(srs, mp, cookie) {		\
391 	mac_pkt_drop(NULL, NULL, mp, B_FALSE);			\
392 	/* increment freed stats */				\
393 	mac_srs->srs_tx.st_drop_count++;			\
394 	cookie = (mac_tx_cookie_t)srs;				\
395 }
396 
397 #define	MAC_TX_SET_NO_ENQUEUE(srs, mp_chain, ret_mp, cookie) {		\
398 	mac_srs->srs_state |= SRS_TX_WAKEUP_CLIENT;			\
399 	cookie = (mac_tx_cookie_t)srs;					\
400 	*ret_mp = mp_chain;						\
401 }
402 
403 /*
404  * Drop the rx packet and advance to the next one in the chain.
405  */
406 static void
407 mac_rx_drop_pkt(mac_soft_ring_set_t *srs, mblk_t *mp)
408 {
409 	mac_srs_rx_t	*srs_rx = &srs->srs_rx;
410 
411 	ASSERT(mp->b_next == NULL);
412 	mutex_enter(&srs->srs_lock);
413 	MAC_UPDATE_SRS_COUNT_LOCKED(srs, 1);
414 	MAC_UPDATE_SRS_SIZE_LOCKED(srs, msgdsize(mp));
415 	mutex_exit(&srs->srs_lock);
416 
417 	srs_rx->sr_drop_count++;
418 	freemsg(mp);
419 }
420 
421 /* DATAPATH RUNTIME ROUTINES */
422 
423 /*
424  * mac_srs_fire
425  *
426  * Timer callback routine for waking up the SRS worker thread.
427  */
428 static void
429 mac_srs_fire(void *arg)
430 {
431 	mac_soft_ring_set_t *mac_srs = (mac_soft_ring_set_t *)arg;
432 
433 	mutex_enter(&mac_srs->srs_lock);
434 	if (mac_srs->srs_tid == 0) {
435 		mutex_exit(&mac_srs->srs_lock);
436 		return;
437 	}
438 
439 	mac_srs->srs_tid = 0;
440 	if (!(mac_srs->srs_state & SRS_PROC))
441 		cv_signal(&mac_srs->srs_async);
442 
443 	mutex_exit(&mac_srs->srs_lock);
444 }
445 
446 /*
447  * 'hint' is fanout_hint (type of uint64_t) which is given by the TCP/IP stack,
448  * and it is used on the TX path.
449  */
450 #define	HASH_HINT(hint)	(((hint) << 17) | ((hint) >> 16))
451 
452 /*
453  * hash based on the src address and the port information.
454  */
455 #define	HASH_ADDR(src, ports)					\
456 	(ntohl((src)) ^ ((ports) >> 24) ^ ((ports) >> 16) ^	\
457 	((ports) >> 8) ^ (ports))
458 
459 #define	COMPUTE_INDEX(key, sz)	(key % sz)
460 
461 #define	FANOUT_ENQUEUE_MP(head, tail, cnt, bw_ctl, sz, sz0, mp) {	\
462 	if ((tail) != NULL) {						\
463 		ASSERT((tail)->b_next == NULL);				\
464 		(tail)->b_next = (mp);					\
465 	} else {							\
466 		ASSERT((head) == NULL);					\
467 		(head) = (mp);						\
468 	}								\
469 	(tail) = (mp);							\
470 	(cnt)++;							\
471 	if ((bw_ctl))							\
472 		(sz) += (sz0);						\
473 }
474 
475 #define	MAC_FANOUT_DEFAULT	0
476 #define	MAC_FANOUT_RND_ROBIN	1
477 int mac_fanout_type = MAC_FANOUT_DEFAULT;
478 
479 #define	MAX_SR_TYPES	3
480 /* fanout types for port based hashing */
481 enum pkt_type {
482 	V4_TCP = 0,
483 	V4_UDP,
484 	OTH,
485 	UNDEF
486 };
487 
488 /*
489  * In general we do port based hashing to spread traffic over different
490  * softrings. The below tunable allows to override that behavior. Setting it
491  * to B_TRUE allows to do a fanout based on src ipv6 address. This behavior
492  * is also the applicable to ipv6 packets carrying multiple optional headers
493  * and other uncommon packet types.
494  */
495 boolean_t mac_src_ipv6_fanout = B_FALSE;
496 
497 /*
498  * Pair of local and remote ports in the transport header
499  */
500 #define	PORTS_SIZE 4
501 
502 /*
503  * mac_rx_srs_proto_fanout
504  *
505  * This routine delivers packets destined to an SRS into one of the
506  * protocol soft rings.
507  *
508  * Given a chain of packets we need to split it up into multiple sub chains
509  * destined into TCP, UDP or OTH soft ring. Instead of entering
510  * the soft ring one packet at a time, we want to enter it in the form of a
511  * chain otherwise we get this start/stop behaviour where the worker thread
512  * goes to sleep and then next packets comes in forcing it to wake up etc.
513  */
514 static void
515 mac_rx_srs_proto_fanout(mac_soft_ring_set_t *mac_srs, mblk_t *head)
516 {
517 	struct ether_header		*ehp;
518 	struct ether_vlan_header	*evhp;
519 	uint32_t			sap;
520 	ipha_t				*ipha;
521 	uint8_t				*dstaddr;
522 	size_t				hdrsize;
523 	mblk_t				*mp;
524 	mblk_t				*headmp[MAX_SR_TYPES];
525 	mblk_t				*tailmp[MAX_SR_TYPES];
526 	int				cnt[MAX_SR_TYPES];
527 	size_t				sz[MAX_SR_TYPES];
528 	size_t				sz1;
529 	boolean_t			bw_ctl;
530 	boolean_t			hw_classified;
531 	boolean_t			dls_bypass;
532 	boolean_t			is_ether;
533 	boolean_t			is_unicast;
534 	enum pkt_type			type;
535 	mac_client_impl_t		*mcip = mac_srs->srs_mcip;
536 
537 	is_ether = (mcip->mci_mip->mi_info.mi_nativemedia == DL_ETHER);
538 	bw_ctl = ((mac_srs->srs_type & SRST_BW_CONTROL) != 0);
539 
540 	/*
541 	 * If we don't have a Rx ring, S/W classification would have done
542 	 * its job and its a packet meant for us. If we were polling on
543 	 * the default ring (i.e. there was a ring assigned to this SRS),
544 	 * then we need to make sure that the mac address really belongs
545 	 * to us.
546 	 */
547 	hw_classified = mac_srs->srs_ring != NULL &&
548 	    mac_srs->srs_ring->mr_classify_type == MAC_HW_CLASSIFIER;
549 
550 	/*
551 	 * Special clients (eg. VLAN, non ether, etc) need DLS
552 	 * processing in the Rx path. SRST_DLS_BYPASS will be clear for
553 	 * such SRSs.
554 	 */
555 	dls_bypass = ((mac_srs->srs_type & SRST_DLS_BYPASS) != 0);
556 
557 	bzero(headmp, MAX_SR_TYPES * sizeof (mblk_t *));
558 	bzero(tailmp, MAX_SR_TYPES * sizeof (mblk_t *));
559 	bzero(cnt, MAX_SR_TYPES * sizeof (int));
560 	bzero(sz, MAX_SR_TYPES * sizeof (size_t));
561 
562 	/*
563 	 * We got a chain from SRS that we need to send to the soft rings.
564 	 * Since squeues for TCP & IPv4 sap poll their soft rings (for
565 	 * performance reasons), we need to separate out v4_tcp, v4_udp
566 	 * and the rest goes in other.
567 	 */
568 	while (head != NULL) {
569 		mp = head;
570 		head = head->b_next;
571 		mp->b_next = NULL;
572 
573 		type = OTH;
574 		sz1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp);
575 
576 		if (is_ether) {
577 			/*
578 			 * At this point we can be sure the packet at least
579 			 * has an ether header.
580 			 */
581 			if (sz1 < sizeof (struct ether_header)) {
582 				mac_rx_drop_pkt(mac_srs, mp);
583 				continue;
584 			}
585 			ehp = (struct ether_header *)mp->b_rptr;
586 
587 			/*
588 			 * Determine if this is a VLAN or non-VLAN packet.
589 			 */
590 			if ((sap = ntohs(ehp->ether_type)) == VLAN_TPID) {
591 				evhp = (struct ether_vlan_header *)mp->b_rptr;
592 				sap = ntohs(evhp->ether_type);
593 				hdrsize = sizeof (struct ether_vlan_header);
594 				/*
595 				 * Check if the VID of the packet, if any,
596 				 * belongs to this client.
597 				 */
598 				if (!mac_client_check_flow_vid(mcip,
599 				    VLAN_ID(ntohs(evhp->ether_tci)))) {
600 					mac_rx_drop_pkt(mac_srs, mp);
601 					continue;
602 				}
603 			} else {
604 				hdrsize = sizeof (struct ether_header);
605 			}
606 			is_unicast =
607 			    ((((uint8_t *)&ehp->ether_dhost)[0] & 0x01) == 0);
608 			dstaddr = (uint8_t *)&ehp->ether_dhost;
609 		} else {
610 			mac_header_info_t		mhi;
611 
612 			if (mac_header_info((mac_handle_t)mcip->mci_mip,
613 			    mp, &mhi) != 0) {
614 				mac_rx_drop_pkt(mac_srs, mp);
615 				continue;
616 			}
617 			hdrsize = mhi.mhi_hdrsize;
618 			sap = mhi.mhi_bindsap;
619 			is_unicast = (mhi.mhi_dsttype == MAC_ADDRTYPE_UNICAST);
620 			dstaddr = (uint8_t *)mhi.mhi_daddr;
621 		}
622 
623 		if (!dls_bypass) {
624 			FANOUT_ENQUEUE_MP(headmp[type], tailmp[type],
625 			    cnt[type], bw_ctl, sz[type], sz1, mp);
626 			continue;
627 		}
628 
629 		if (sap == ETHERTYPE_IP) {
630 			/*
631 			 * If we are H/W classified, but we have promisc
632 			 * on, then we need to check for the unicast address.
633 			 */
634 			if (hw_classified && mcip->mci_promisc_list != NULL) {
635 				mac_address_t		*map;
636 
637 				rw_enter(&mcip->mci_rw_lock, RW_READER);
638 				map = mcip->mci_unicast;
639 				if (bcmp(dstaddr, map->ma_addr,
640 				    map->ma_len) == 0)
641 					type = UNDEF;
642 				rw_exit(&mcip->mci_rw_lock);
643 			} else if (is_unicast) {
644 				type = UNDEF;
645 			}
646 		}
647 
648 		/*
649 		 * This needs to become a contract with the driver for
650 		 * the fast path.
651 		 *
652 		 * In the normal case the packet will have at least the L2
653 		 * header and the IP + Transport header in the same mblk.
654 		 * This is usually the case when the NIC driver sends up
655 		 * the packet. This is also true when the stack generates
656 		 * a packet that is looped back and when the stack uses the
657 		 * fastpath mechanism. The normal case is optimized for
658 		 * performance and may bypass DLS. All other cases go through
659 		 * the 'OTH' type path without DLS bypass.
660 		 */
661 
662 		ipha = (ipha_t *)(mp->b_rptr + hdrsize);
663 		if ((type != OTH) && MBLK_RX_FANOUT_SLOWPATH(mp, ipha))
664 			type = OTH;
665 
666 		if (type == OTH) {
667 			FANOUT_ENQUEUE_MP(headmp[type], tailmp[type],
668 			    cnt[type], bw_ctl, sz[type], sz1, mp);
669 			continue;
670 		}
671 
672 		ASSERT(type == UNDEF);
673 		/*
674 		 * We look for at least 4 bytes past the IP header to get
675 		 * the port information. If we get an IP fragment, we don't
676 		 * have the port information, and we use just the protocol
677 		 * information.
678 		 */
679 		switch (ipha->ipha_protocol) {
680 		case IPPROTO_TCP:
681 			type = V4_TCP;
682 			mp->b_rptr += hdrsize;
683 			break;
684 		case IPPROTO_UDP:
685 			type = V4_UDP;
686 			mp->b_rptr += hdrsize;
687 			break;
688 		default:
689 			type = OTH;
690 			break;
691 		}
692 
693 		FANOUT_ENQUEUE_MP(headmp[type], tailmp[type], cnt[type],
694 		    bw_ctl, sz[type], sz1, mp);
695 	}
696 
697 	for (type = V4_TCP; type < UNDEF; type++) {
698 		if (headmp[type] != NULL) {
699 			mac_soft_ring_t			*softring;
700 
701 			ASSERT(tailmp[type]->b_next == NULL);
702 			switch (type) {
703 			case V4_TCP:
704 				softring = mac_srs->srs_tcp_soft_rings[0];
705 				break;
706 			case V4_UDP:
707 				softring = mac_srs->srs_udp_soft_rings[0];
708 				break;
709 			case OTH:
710 				softring = mac_srs->srs_oth_soft_rings[0];
711 			}
712 			mac_rx_soft_ring_process(mcip, softring,
713 			    headmp[type], tailmp[type], cnt[type], sz[type]);
714 		}
715 	}
716 }
717 
718 int	fanout_unalligned = 0;
719 
720 /*
721  * mac_rx_srs_long_fanout
722  *
723  * The fanout routine for IPv6
724  */
725 static int
726 mac_rx_srs_long_fanout(mac_soft_ring_set_t *mac_srs, mblk_t *mp,
727     uint32_t sap, size_t hdrsize, enum pkt_type *type, uint_t *indx)
728 {
729 	ip6_t		*ip6h;
730 	uint8_t		*whereptr;
731 	uint_t		hash;
732 	uint16_t	remlen;
733 	uint8_t		nexthdr;
734 	uint16_t	hdr_len;
735 
736 	if (sap == ETHERTYPE_IPV6) {
737 		boolean_t	modifiable = B_TRUE;
738 
739 		ASSERT(MBLKL(mp) >= hdrsize);
740 
741 		ip6h = (ip6_t *)(mp->b_rptr + hdrsize);
742 		if ((unsigned char *)ip6h == mp->b_wptr) {
743 			/*
744 			 * The first mblk_t only includes the mac header.
745 			 * Note that it is safe to change the mp pointer here,
746 			 * as the subsequent operation does not assume mp
747 			 * points to the start of the mac header.
748 			 */
749 			mp = mp->b_cont;
750 
751 			/*
752 			 * Make sure ip6h holds the full ip6_t structure.
753 			 */
754 			if (mp == NULL)
755 				return (-1);
756 
757 			if (MBLKL(mp) < IPV6_HDR_LEN) {
758 				modifiable = (DB_REF(mp) == 1);
759 
760 				if (modifiable &&
761 				    !pullupmsg(mp, IPV6_HDR_LEN)) {
762 					return (-1);
763 				}
764 			}
765 
766 			ip6h = (ip6_t *)mp->b_rptr;
767 		}
768 
769 		if (!modifiable || !(OK_32PTR((char *)ip6h)) ||
770 		    ((unsigned char *)ip6h + IPV6_HDR_LEN > mp->b_wptr)) {
771 			/*
772 			 * If either ip6h is not alligned, or ip6h does not
773 			 * hold the complete ip6_t structure (a pullupmsg()
774 			 * is not an option since it would result in an
775 			 * unalligned ip6h), fanout to the default ring. Note
776 			 * that this may cause packets reordering.
777 			 */
778 			*indx = 0;
779 			*type = OTH;
780 			fanout_unalligned++;
781 			return (0);
782 		}
783 
784 		remlen = ntohs(ip6h->ip6_plen);
785 		nexthdr = ip6h->ip6_nxt;
786 
787 		if (remlen < MIN_EHDR_LEN)
788 			return (-1);
789 		/*
790 		 * Do src based fanout if below tunable is set to B_TRUE or
791 		 * when mac_ip_hdr_length_v6() fails because of malformed
792 		 * packets or because mblk's need to be concatenated using
793 		 * pullupmsg().
794 		 */
795 		if (mac_src_ipv6_fanout || !mac_ip_hdr_length_v6(mp, ip6h,
796 		    &hdr_len, &nexthdr)) {
797 			goto src_based_fanout;
798 		}
799 		whereptr = (uint8_t *)ip6h + hdr_len;
800 
801 		/* If the transport is one of below, we do port based fanout */
802 		switch (nexthdr) {
803 		case IPPROTO_TCP:
804 		case IPPROTO_UDP:
805 		case IPPROTO_SCTP:
806 		case IPPROTO_ESP:
807 			/*
808 			 * If the ports in the transport header is not part of
809 			 * the mblk, do src_based_fanout, instead of calling
810 			 * pullupmsg().
811 			 */
812 			if (mp->b_cont != NULL &&
813 			    whereptr + PORTS_SIZE > mp->b_wptr) {
814 				goto src_based_fanout;
815 			}
816 			break;
817 		default:
818 			break;
819 		}
820 
821 		switch (nexthdr) {
822 		case IPPROTO_TCP:
823 			hash = HASH_ADDR(V4_PART_OF_V6(ip6h->ip6_src),
824 			    *(uint32_t *)whereptr);
825 			*indx = COMPUTE_INDEX(hash,
826 			    mac_srs->srs_tcp_ring_count);
827 			*type = OTH;
828 			break;
829 
830 		case IPPROTO_UDP:
831 		case IPPROTO_SCTP:
832 		case IPPROTO_ESP:
833 			if (mac_fanout_type == MAC_FANOUT_DEFAULT) {
834 				hash = HASH_ADDR(V4_PART_OF_V6(ip6h->ip6_src),
835 				    *(uint32_t *)whereptr);
836 				*indx = COMPUTE_INDEX(hash,
837 				    mac_srs->srs_udp_ring_count);
838 			} else {
839 				*indx = mac_srs->srs_ind %
840 				    mac_srs->srs_udp_ring_count;
841 				mac_srs->srs_ind++;
842 			}
843 			*type = OTH;
844 			break;
845 
846 			/* For all other protocol, do source based fanout */
847 		default:
848 			goto src_based_fanout;
849 		}
850 	} else {
851 		*indx = 0;
852 		*type = OTH;
853 	}
854 	return (0);
855 
856 src_based_fanout:
857 	hash = HASH_ADDR(V4_PART_OF_V6(ip6h->ip6_src), (uint32_t)0);
858 	*indx = COMPUTE_INDEX(hash, mac_srs->srs_oth_ring_count);
859 	*type = OTH;
860 	return (0);
861 }
862 
863 /*
864  * mac_rx_srs_fanout
865  *
866  * This routine delivers packets destined to an SRS into a soft ring member
867  * of the set.
868  *
869  * Given a chain of packets we need to split it up into multiple sub chains
870  * destined for one of the TCP, UDP or OTH soft rings. Instead of entering
871  * the soft ring one packet at a time, we want to enter it in the form of a
872  * chain otherwise we get this start/stop behaviour where the worker thread
873  * goes to sleep and then next packets comes in forcing it to wake up etc.
874  *
875  * Note:
876  * Since we know what is the maximum fanout possible, we create a 2D array
877  * of 'softring types * MAX_SR_FANOUT' for the head, tail, cnt and sz
878  * variables so that we can enter the softrings with chain. We need the
879  * MAX_SR_FANOUT so we can allocate the arrays on the stack (a kmem_alloc
880  * for each packet would be expensive). If we ever want to have the
881  * ability to have unlimited fanout, we should probably declare a head,
882  * tail, cnt, sz with each soft ring (a data struct which contains a softring
883  * along with these members) and create an array of this uber struct so we
884  * don't have to do kmem_alloc.
885  */
886 int	fanout_oth1 = 0;
887 int	fanout_oth2 = 0;
888 int	fanout_oth3 = 0;
889 int	fanout_oth4 = 0;
890 int	fanout_oth5 = 0;
891 
892 static void
893 mac_rx_srs_fanout(mac_soft_ring_set_t *mac_srs, mblk_t *head)
894 {
895 	struct ether_header		*ehp;
896 	struct ether_vlan_header	*evhp;
897 	uint32_t			sap;
898 	ipha_t				*ipha;
899 	uint8_t				*dstaddr;
900 	uint_t				indx;
901 	size_t				ports_offset;
902 	size_t				ipha_len;
903 	size_t				hdrsize;
904 	uint_t				hash;
905 	mblk_t				*mp;
906 	mblk_t				*headmp[MAX_SR_TYPES][MAX_SR_FANOUT];
907 	mblk_t				*tailmp[MAX_SR_TYPES][MAX_SR_FANOUT];
908 	int				cnt[MAX_SR_TYPES][MAX_SR_FANOUT];
909 	size_t				sz[MAX_SR_TYPES][MAX_SR_FANOUT];
910 	size_t				sz1;
911 	boolean_t			bw_ctl;
912 	boolean_t			hw_classified;
913 	boolean_t			dls_bypass;
914 	boolean_t			is_ether;
915 	boolean_t			is_unicast;
916 	int				fanout_cnt;
917 	enum pkt_type			type;
918 	mac_client_impl_t		*mcip = mac_srs->srs_mcip;
919 
920 	is_ether = (mcip->mci_mip->mi_info.mi_nativemedia == DL_ETHER);
921 	bw_ctl = ((mac_srs->srs_type & SRST_BW_CONTROL) != 0);
922 
923 	/*
924 	 * If we don't have a Rx ring, S/W classification would have done
925 	 * its job and its a packet meant for us. If we were polling on
926 	 * the default ring (i.e. there was a ring assigned to this SRS),
927 	 * then we need to make sure that the mac address really belongs
928 	 * to us.
929 	 */
930 	hw_classified = mac_srs->srs_ring != NULL &&
931 	    mac_srs->srs_ring->mr_classify_type == MAC_HW_CLASSIFIER;
932 
933 	/*
934 	 * Special clients (eg. VLAN, non ether, etc) need DLS
935 	 * processing in the Rx path. SRST_DLS_BYPASS will be clear for
936 	 * such SRSs.
937 	 */
938 	dls_bypass = ((mac_srs->srs_type & SRST_DLS_BYPASS) != 0);
939 
940 	/*
941 	 * Since the softrings are never destroyed and we always
942 	 * create equal number of softrings for TCP, UDP and rest,
943 	 * its OK to check one of them for count and use it without
944 	 * any lock. In future, if soft rings get destroyed because
945 	 * of reduction in fanout, we will need to ensure that happens
946 	 * behind the SRS_PROC.
947 	 */
948 	fanout_cnt = mac_srs->srs_tcp_ring_count;
949 
950 	bzero(headmp, MAX_SR_TYPES * MAX_SR_FANOUT * sizeof (mblk_t *));
951 	bzero(tailmp, MAX_SR_TYPES * MAX_SR_FANOUT * sizeof (mblk_t *));
952 	bzero(cnt, MAX_SR_TYPES * MAX_SR_FANOUT * sizeof (int));
953 	bzero(sz, MAX_SR_TYPES * MAX_SR_FANOUT * sizeof (size_t));
954 
955 	/*
956 	 * We got a chain from SRS that we need to send to the soft rings.
957 	 * Since squeues for TCP & IPv4 sap poll their soft rings (for
958 	 * performance reasons), we need to separate out v4_tcp, v4_udp
959 	 * and the rest goes in other.
960 	 */
961 	while (head != NULL) {
962 		mp = head;
963 		head = head->b_next;
964 		mp->b_next = NULL;
965 
966 		type = OTH;
967 		sz1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp);
968 
969 		if (is_ether) {
970 			/*
971 			 * At this point we can be sure the packet at least
972 			 * has an ether header.
973 			 */
974 			if (sz1 < sizeof (struct ether_header)) {
975 				mac_rx_drop_pkt(mac_srs, mp);
976 				continue;
977 			}
978 			ehp = (struct ether_header *)mp->b_rptr;
979 
980 			/*
981 			 * Determine if this is a VLAN or non-VLAN packet.
982 			 */
983 			if ((sap = ntohs(ehp->ether_type)) == VLAN_TPID) {
984 				evhp = (struct ether_vlan_header *)mp->b_rptr;
985 				sap = ntohs(evhp->ether_type);
986 				hdrsize = sizeof (struct ether_vlan_header);
987 				/*
988 				 * Check if the VID of the packet, if any,
989 				 * belongs to this client.
990 				 */
991 				if (!mac_client_check_flow_vid(mcip,
992 				    VLAN_ID(ntohs(evhp->ether_tci)))) {
993 					mac_rx_drop_pkt(mac_srs, mp);
994 					continue;
995 				}
996 			} else {
997 				hdrsize = sizeof (struct ether_header);
998 			}
999 			is_unicast =
1000 			    ((((uint8_t *)&ehp->ether_dhost)[0] & 0x01) == 0);
1001 			dstaddr = (uint8_t *)&ehp->ether_dhost;
1002 		} else {
1003 			mac_header_info_t		mhi;
1004 
1005 			if (mac_header_info((mac_handle_t)mcip->mci_mip,
1006 			    mp, &mhi) != 0) {
1007 				mac_rx_drop_pkt(mac_srs, mp);
1008 				continue;
1009 			}
1010 			hdrsize = mhi.mhi_hdrsize;
1011 			sap = mhi.mhi_bindsap;
1012 			is_unicast = (mhi.mhi_dsttype == MAC_ADDRTYPE_UNICAST);
1013 			dstaddr = (uint8_t *)mhi.mhi_daddr;
1014 		}
1015 
1016 		if (!dls_bypass) {
1017 			if (mac_rx_srs_long_fanout(mac_srs, mp, sap,
1018 			    hdrsize, &type, &indx) == -1) {
1019 				mac_rx_drop_pkt(mac_srs, mp);
1020 				continue;
1021 			}
1022 
1023 			FANOUT_ENQUEUE_MP(headmp[type][indx],
1024 			    tailmp[type][indx], cnt[type][indx], bw_ctl,
1025 			    sz[type][indx], sz1, mp);
1026 			continue;
1027 		}
1028 
1029 
1030 		/*
1031 		 * If we are using the default Rx ring where H/W or S/W
1032 		 * classification has not happened, we need to verify if
1033 		 * this unicast packet really belongs to us.
1034 		 */
1035 		if (sap == ETHERTYPE_IP) {
1036 			/*
1037 			 * If we are H/W classified, but we have promisc
1038 			 * on, then we need to check for the unicast address.
1039 			 */
1040 			if (hw_classified && mcip->mci_promisc_list != NULL) {
1041 				mac_address_t		*map;
1042 
1043 				rw_enter(&mcip->mci_rw_lock, RW_READER);
1044 				map = mcip->mci_unicast;
1045 				if (bcmp(dstaddr, map->ma_addr,
1046 				    map->ma_len) == 0)
1047 					type = UNDEF;
1048 				rw_exit(&mcip->mci_rw_lock);
1049 			} else if (is_unicast) {
1050 				type = UNDEF;
1051 			}
1052 		}
1053 
1054 		/*
1055 		 * This needs to become a contract with the driver for
1056 		 * the fast path.
1057 		 */
1058 
1059 		ipha = (ipha_t *)(mp->b_rptr + hdrsize);
1060 		if ((type != OTH) && MBLK_RX_FANOUT_SLOWPATH(mp, ipha)) {
1061 			type = OTH;
1062 			fanout_oth1++;
1063 		}
1064 
1065 		if (type != OTH) {
1066 			uint16_t	frag_offset_flags;
1067 
1068 			switch (ipha->ipha_protocol) {
1069 			case IPPROTO_TCP:
1070 			case IPPROTO_UDP:
1071 			case IPPROTO_SCTP:
1072 			case IPPROTO_ESP:
1073 				ipha_len = IPH_HDR_LENGTH(ipha);
1074 				if ((uchar_t *)ipha + ipha_len + PORTS_SIZE >
1075 				    mp->b_wptr) {
1076 					type = OTH;
1077 					break;
1078 				}
1079 				frag_offset_flags =
1080 				    ntohs(ipha->ipha_fragment_offset_and_flags);
1081 				if ((frag_offset_flags &
1082 				    (IPH_MF | IPH_OFFSET)) != 0) {
1083 					type = OTH;
1084 					fanout_oth3++;
1085 					break;
1086 				}
1087 				ports_offset = hdrsize + ipha_len;
1088 				break;
1089 			default:
1090 				type = OTH;
1091 				fanout_oth4++;
1092 				break;
1093 			}
1094 		}
1095 
1096 		if (type == OTH) {
1097 			if (mac_rx_srs_long_fanout(mac_srs, mp, sap,
1098 			    hdrsize, &type, &indx) == -1) {
1099 				mac_rx_drop_pkt(mac_srs, mp);
1100 				continue;
1101 			}
1102 
1103 			FANOUT_ENQUEUE_MP(headmp[type][indx],
1104 			    tailmp[type][indx], cnt[type][indx], bw_ctl,
1105 			    sz[type][indx], sz1, mp);
1106 			continue;
1107 		}
1108 
1109 		ASSERT(type == UNDEF);
1110 
1111 		/*
1112 		 * XXX-Sunay: We should hold srs_lock since ring_count
1113 		 * below can change. But if we are always called from
1114 		 * mac_rx_srs_drain and SRS_PROC is set, then we can
1115 		 * enforce that ring_count can't be changed i.e.
1116 		 * to change fanout type or ring count, the calling
1117 		 * thread needs to be behind SRS_PROC.
1118 		 */
1119 		switch (ipha->ipha_protocol) {
1120 		case IPPROTO_TCP:
1121 			/*
1122 			 * Note that for ESP, we fanout on SPI and it is at the
1123 			 * same offset as the 2x16-bit ports. So it is clumped
1124 			 * along with TCP, UDP and SCTP.
1125 			 */
1126 			hash = HASH_ADDR(ipha->ipha_src,
1127 			    *(uint32_t *)(mp->b_rptr + ports_offset));
1128 			indx = COMPUTE_INDEX(hash, mac_srs->srs_tcp_ring_count);
1129 			type = V4_TCP;
1130 			mp->b_rptr += hdrsize;
1131 			break;
1132 		case IPPROTO_UDP:
1133 		case IPPROTO_SCTP:
1134 		case IPPROTO_ESP:
1135 			if (mac_fanout_type == MAC_FANOUT_DEFAULT) {
1136 				hash = HASH_ADDR(ipha->ipha_src,
1137 				    *(uint32_t *)(mp->b_rptr + ports_offset));
1138 				indx = COMPUTE_INDEX(hash,
1139 				    mac_srs->srs_udp_ring_count);
1140 			} else {
1141 				indx = mac_srs->srs_ind %
1142 				    mac_srs->srs_udp_ring_count;
1143 				mac_srs->srs_ind++;
1144 			}
1145 			type = V4_UDP;
1146 			mp->b_rptr += hdrsize;
1147 			break;
1148 		default:
1149 			indx = 0;
1150 			type = OTH;
1151 		}
1152 
1153 		FANOUT_ENQUEUE_MP(headmp[type][indx], tailmp[type][indx],
1154 		    cnt[type][indx], bw_ctl, sz[type][indx], sz1, mp);
1155 	}
1156 
1157 	for (type = V4_TCP; type < UNDEF; type++) {
1158 		int	i;
1159 
1160 		for (i = 0; i < fanout_cnt; i++) {
1161 			if (headmp[type][i] != NULL) {
1162 				mac_soft_ring_t	*softring;
1163 
1164 				ASSERT(tailmp[type][i]->b_next == NULL);
1165 				switch (type) {
1166 				case V4_TCP:
1167 					softring =
1168 					    mac_srs->srs_tcp_soft_rings[i];
1169 					break;
1170 				case V4_UDP:
1171 					softring =
1172 					    mac_srs->srs_udp_soft_rings[i];
1173 					break;
1174 				case OTH:
1175 					softring =
1176 					    mac_srs->srs_oth_soft_rings[i];
1177 					break;
1178 				}
1179 				mac_rx_soft_ring_process(mcip,
1180 				    softring, headmp[type][i], tailmp[type][i],
1181 				    cnt[type][i], sz[type][i]);
1182 			}
1183 		}
1184 	}
1185 }
1186 
1187 #define	SRS_BYTES_TO_PICKUP	150000
1188 ssize_t	max_bytes_to_pickup = SRS_BYTES_TO_PICKUP;
1189 
1190 /*
1191  * mac_rx_srs_poll_ring
1192  *
1193  * This SRS Poll thread uses this routine to poll the underlying hardware
1194  * Rx ring to get a chain of packets. It can inline process that chain
1195  * if mac_latency_optimize is set (default) or signal the SRS worker thread
1196  * to do the remaining processing.
1197  *
1198  * Since packets come in the system via interrupt or poll path, we also
1199  * update the stats and deal with promiscous clients here.
1200  */
1201 void
1202 mac_rx_srs_poll_ring(mac_soft_ring_set_t *mac_srs)
1203 {
1204 	kmutex_t 		*lock = &mac_srs->srs_lock;
1205 	kcondvar_t 		*async = &mac_srs->srs_cv;
1206 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
1207 	mblk_t 			*head, *tail, *mp;
1208 	callb_cpr_t 		cprinfo;
1209 	ssize_t 		bytes_to_pickup;
1210 	size_t 			sz;
1211 	int			count;
1212 	mac_client_impl_t	*smcip;
1213 
1214 	CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, "mac_srs_poll");
1215 	mutex_enter(lock);
1216 
1217 start:
1218 	for (;;) {
1219 		if (mac_srs->srs_state & SRS_PAUSE)
1220 			goto done;
1221 
1222 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
1223 		cv_wait(async, lock);
1224 		CALLB_CPR_SAFE_END(&cprinfo, lock);
1225 
1226 		if (mac_srs->srs_state & SRS_PAUSE)
1227 			goto done;
1228 
1229 check_again:
1230 		if (mac_srs->srs_type & SRST_BW_CONTROL) {
1231 			/*
1232 			 * We pick as many bytes as we are allowed to queue.
1233 			 * Its possible that we will exceed the total
1234 			 * packets queued in case this SRS is part of the
1235 			 * Rx ring group since > 1 poll thread can be pulling
1236 			 * upto the max allowed packets at the same time
1237 			 * but that should be OK.
1238 			 */
1239 			mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1240 			bytes_to_pickup =
1241 			    mac_srs->srs_bw->mac_bw_drop_threshold -
1242 			    mac_srs->srs_bw->mac_bw_sz;
1243 			/*
1244 			 * We shouldn't have been signalled if we
1245 			 * have 0 or less bytes to pick but since
1246 			 * some of the bytes accounting is driver
1247 			 * dependant, we do the safety check.
1248 			 */
1249 			if (bytes_to_pickup < 0)
1250 				bytes_to_pickup = 0;
1251 			mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1252 		} else {
1253 			/*
1254 			 * ToDO: Need to change the polling API
1255 			 * to add a packet count and a flag which
1256 			 * tells the driver whether we want packets
1257 			 * based on a count, or bytes, or all the
1258 			 * packets queued in the driver/HW. This
1259 			 * way, we never have to check the limits
1260 			 * on poll path. We truly let only as many
1261 			 * packets enter the system as we are willing
1262 			 * to process or queue.
1263 			 *
1264 			 * Something along the lines of
1265 			 * pkts_to_pickup = mac_soft_ring_max_q_cnt -
1266 			 *	mac_srs->srs_poll_pkt_cnt
1267 			 */
1268 
1269 			/*
1270 			 * Since we are not doing B/W control, pick
1271 			 * as many packets as allowed.
1272 			 */
1273 			bytes_to_pickup = max_bytes_to_pickup;
1274 		}
1275 
1276 		/* Poll the underlying Hardware */
1277 		mutex_exit(lock);
1278 		head = MAC_HWRING_POLL(mac_srs->srs_ring, (int)bytes_to_pickup);
1279 		mutex_enter(lock);
1280 
1281 		ASSERT((mac_srs->srs_state & SRS_POLL_THR_OWNER) ==
1282 		    SRS_POLL_THR_OWNER);
1283 
1284 		mp = tail = head;
1285 		count = 0;
1286 		sz = 0;
1287 		while (mp != NULL) {
1288 			tail = mp;
1289 			sz += msgdsize(mp);
1290 			mp = mp->b_next;
1291 			count++;
1292 		}
1293 
1294 		if (head != NULL) {
1295 			tail->b_next = NULL;
1296 			smcip = mac_srs->srs_mcip;
1297 
1298 			if ((mac_srs->srs_type & SRST_FLOW) ||
1299 			    (smcip == NULL)) {
1300 				FLOW_STAT_UPDATE(mac_srs->srs_flent,
1301 				    rbytes, sz);
1302 				FLOW_STAT_UPDATE(mac_srs->srs_flent,
1303 				    ipackets, count);
1304 			}
1305 
1306 			/*
1307 			 * If there are any promiscuous mode callbacks
1308 			 * defined for this MAC client, pass them a copy
1309 			 * if appropriate and also update the counters.
1310 			 */
1311 			if (smcip != NULL) {
1312 				smcip->mci_stat_ibytes += sz;
1313 				smcip->mci_stat_ipackets += count;
1314 
1315 				if (smcip->mci_mip->mi_promisc_list != NULL) {
1316 					mutex_exit(lock);
1317 					mac_promisc_dispatch(smcip->mci_mip,
1318 					    head, NULL);
1319 					mutex_enter(lock);
1320 				}
1321 			}
1322 			if (mac_srs->srs_type & SRST_BW_CONTROL) {
1323 				mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1324 				mac_srs->srs_bw->mac_bw_polled += sz;
1325 				mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1326 			}
1327 			srs_rx->sr_poll_count += count;
1328 			MAC_RX_SRS_ENQUEUE_CHAIN(mac_srs, head, tail,
1329 			    count, sz);
1330 			if (count <= 10)
1331 				srs_rx->sr_chain_cnt_undr10++;
1332 			else if (count > 10 && count <= 50)
1333 				srs_rx->sr_chain_cnt_10to50++;
1334 			else
1335 				srs_rx->sr_chain_cnt_over50++;
1336 		}
1337 
1338 		/*
1339 		 * We are guaranteed that SRS_PROC will be set if we
1340 		 * are here. Also, poll thread gets to run only if
1341 		 * the drain was being done by a worker thread although
1342 		 * its possible that worker thread is still running
1343 		 * and poll thread was sent down to keep the pipeline
1344 		 * going instead of doing a complete drain and then
1345 		 * trying to poll the NIC.
1346 		 *
1347 		 * So we need to check SRS_WORKER flag to make sure
1348 		 * that the worker thread is not processing the queue
1349 		 * in parallel to us. The flags and conditions are
1350 		 * protected by the srs_lock to prevent any race. We
1351 		 * ensure that we don't drop the srs_lock from now
1352 		 * till the end and similarly we don't drop the srs_lock
1353 		 * in mac_rx_srs_drain() till similar condition check
1354 		 * are complete. The mac_rx_srs_drain() needs to ensure
1355 		 * that SRS_WORKER flag remains set as long as its
1356 		 * processing the queue.
1357 		 */
1358 		if (!(mac_srs->srs_state & SRS_WORKER) &&
1359 		    (mac_srs->srs_first != NULL)) {
1360 			/*
1361 			 * We have packets to process and worker thread
1362 			 * is not running. Check to see if poll thread is
1363 			 * allowed to process.
1364 			 */
1365 			if (mac_srs->srs_state & SRS_LATENCY_OPT) {
1366 				mac_srs->srs_drain_func(mac_srs, SRS_POLL_PROC);
1367 				if (srs_rx->sr_poll_pkt_cnt <=
1368 				    srs_rx->sr_lowat) {
1369 					srs_rx->sr_poll_again++;
1370 					goto check_again;
1371 				}
1372 				/*
1373 				 * We are already above low water mark
1374 				 * so stay in the polling mode but no
1375 				 * need to poll. Once we dip below
1376 				 * the polling threshold, the processing
1377 				 * thread (soft ring) will signal us
1378 				 * to poll again (MAC_UPDATE_SRS_COUNT)
1379 				 */
1380 				srs_rx->sr_poll_drain_no_poll++;
1381 				mac_srs->srs_state &= ~(SRS_PROC|SRS_GET_PKTS);
1382 				/*
1383 				 * In B/W control case, its possible
1384 				 * that the backlog built up due to
1385 				 * B/W limit being reached and packets
1386 				 * are queued only in SRS. In this case,
1387 				 * we should schedule worker thread
1388 				 * since no one else will wake us up.
1389 				 */
1390 				if ((mac_srs->srs_type & SRST_BW_CONTROL) &&
1391 				    (mac_srs->srs_tid == NULL)) {
1392 					mac_srs->srs_tid =
1393 					    timeout(mac_srs_fire, mac_srs, 1);
1394 					srs_rx->sr_poll_worker_wakeup++;
1395 				}
1396 			} else {
1397 				/*
1398 				 * Wakeup the worker thread for more processing.
1399 				 * We optimize for throughput in this case.
1400 				 */
1401 				mac_srs->srs_state &= ~(SRS_PROC|SRS_GET_PKTS);
1402 				MAC_SRS_WORKER_WAKEUP(mac_srs);
1403 				srs_rx->sr_poll_sig_worker++;
1404 			}
1405 		} else if ((mac_srs->srs_first == NULL) &&
1406 		    !(mac_srs->srs_state & SRS_WORKER)) {
1407 			/*
1408 			 * There is nothing queued in SRS and
1409 			 * no worker thread running. Plus we
1410 			 * didn't get anything from the H/W
1411 			 * as well (head == NULL);
1412 			 */
1413 			ASSERT(head == NULL);
1414 			mac_srs->srs_state &=
1415 			    ~(SRS_PROC|SRS_GET_PKTS);
1416 
1417 			/*
1418 			 * If we have a packets in soft ring, don't allow
1419 			 * more packets to come into this SRS by keeping the
1420 			 * interrupts off but not polling the H/W. The
1421 			 * poll thread will get signaled as soon as
1422 			 * srs_poll_pkt_cnt dips below poll threshold.
1423 			 */
1424 			if (srs_rx->sr_poll_pkt_cnt == 0) {
1425 				srs_rx->sr_poll_intr_enable++;
1426 				MAC_SRS_POLLING_OFF(mac_srs);
1427 			} else {
1428 				/*
1429 				 * We know nothing is queued in SRS
1430 				 * since we are here after checking
1431 				 * srs_first is NULL. The backlog
1432 				 * is entirely due to packets queued
1433 				 * in Soft ring which will wake us up
1434 				 * and get the interface out of polling
1435 				 * mode once the backlog dips below
1436 				 * sr_poll_thres.
1437 				 */
1438 				srs_rx->sr_poll_no_poll++;
1439 			}
1440 		} else {
1441 			/*
1442 			 * Worker thread is already running.
1443 			 * Nothing much to do. If the polling
1444 			 * was enabled, worker thread will deal
1445 			 * with that.
1446 			 */
1447 			mac_srs->srs_state &= ~SRS_GET_PKTS;
1448 			srs_rx->sr_poll_goto_sleep++;
1449 		}
1450 	}
1451 done:
1452 	mac_srs->srs_state |= SRS_POLL_THR_QUIESCED;
1453 	cv_signal(&mac_srs->srs_async);
1454 	/*
1455 	 * If this is a temporary quiesce then wait for the restart signal
1456 	 * from the srs worker. Then clear the flags and signal the srs worker
1457 	 * to ensure a positive handshake and go back to start.
1458 	 */
1459 	while (!(mac_srs->srs_state & (SRS_CONDEMNED | SRS_POLL_THR_RESTART)))
1460 		cv_wait(async, lock);
1461 	if (mac_srs->srs_state & SRS_POLL_THR_RESTART) {
1462 		ASSERT(!(mac_srs->srs_state & SRS_CONDEMNED));
1463 		mac_srs->srs_state &=
1464 		    ~(SRS_POLL_THR_QUIESCED | SRS_POLL_THR_RESTART);
1465 		cv_signal(&mac_srs->srs_async);
1466 		goto start;
1467 	} else {
1468 		mac_srs->srs_state |= SRS_POLL_THR_EXITED;
1469 		cv_signal(&mac_srs->srs_async);
1470 		CALLB_CPR_EXIT(&cprinfo);
1471 		thread_exit();
1472 	}
1473 }
1474 
1475 /*
1476  * mac_srs_pick_chain
1477  *
1478  * In Bandwidth control case, checks how many packets can be processed
1479  * and return them in a sub chain.
1480  */
1481 static mblk_t *
1482 mac_srs_pick_chain(mac_soft_ring_set_t *mac_srs, mblk_t **chain_tail,
1483     size_t *chain_sz, int *chain_cnt)
1484 {
1485 	mblk_t 			*head = NULL;
1486 	mblk_t 			*tail = NULL;
1487 	size_t			sz;
1488 	size_t 			tsz = 0;
1489 	int			cnt = 0;
1490 	mblk_t 			*mp;
1491 
1492 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
1493 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1494 	if (((mac_srs->srs_bw->mac_bw_used + mac_srs->srs_size) <=
1495 	    mac_srs->srs_bw->mac_bw_limit) ||
1496 	    (mac_srs->srs_bw->mac_bw_limit == 0)) {
1497 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1498 		head = mac_srs->srs_first;
1499 		mac_srs->srs_first = NULL;
1500 		*chain_tail = mac_srs->srs_last;
1501 		mac_srs->srs_last = NULL;
1502 		*chain_sz = mac_srs->srs_size;
1503 		*chain_cnt = mac_srs->srs_count;
1504 		mac_srs->srs_count = 0;
1505 		mac_srs->srs_size = 0;
1506 		return (head);
1507 	}
1508 
1509 	/*
1510 	 * Can't clear the entire backlog.
1511 	 * Need to find how many packets to pick
1512 	 */
1513 	ASSERT(MUTEX_HELD(&mac_srs->srs_bw->mac_bw_lock));
1514 	while ((mp = mac_srs->srs_first) != NULL) {
1515 		sz = msgdsize(mp);
1516 		if ((tsz + sz + mac_srs->srs_bw->mac_bw_used) >
1517 		    mac_srs->srs_bw->mac_bw_limit) {
1518 			if (!(mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED))
1519 				mac_srs->srs_bw->mac_bw_state |=
1520 				    SRS_BW_ENFORCED;
1521 			break;
1522 		}
1523 
1524 		/*
1525 		 * The _size & cnt is  decremented from the softrings
1526 		 * when they send up the packet for polling to work
1527 		 * properly.
1528 		 */
1529 		tsz += sz;
1530 		cnt++;
1531 		mac_srs->srs_count--;
1532 		mac_srs->srs_size -= sz;
1533 		if (tail != NULL)
1534 			tail->b_next = mp;
1535 		else
1536 			head = mp;
1537 		tail = mp;
1538 		mac_srs->srs_first = mac_srs->srs_first->b_next;
1539 	}
1540 	mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1541 	if (mac_srs->srs_first == NULL)
1542 		mac_srs->srs_last = NULL;
1543 
1544 	if (tail != NULL)
1545 		tail->b_next = NULL;
1546 	*chain_tail = tail;
1547 	*chain_cnt = cnt;
1548 	*chain_sz = tsz;
1549 
1550 	return (head);
1551 }
1552 
1553 /*
1554  * mac_rx_srs_drain
1555  *
1556  * The SRS drain routine. Gets to run to clear the queue. Any thread
1557  * (worker, interrupt, poll) can call this based on processing model.
1558  * The first thing we do is disable interrupts if possible and then
1559  * drain the queue. we also try to poll the underlying hardware if
1560  * there is a dedicated hardware Rx ring assigned to this SRS.
1561  *
1562  * There is a equivalent drain routine in bandwidth control mode
1563  * mac_rx_srs_drain_bw. There is some code duplication between the two
1564  * routines but they are highly performance sensitive and are easier
1565  * to read/debug if they stay separate. Any code changes here might
1566  * also apply to mac_rx_srs_drain_bw as well.
1567  */
1568 void
1569 mac_rx_srs_drain(mac_soft_ring_set_t *mac_srs, uint_t proc_type)
1570 {
1571 	mblk_t 			*head;
1572 	mblk_t			*tail;
1573 	timeout_id_t 		tid;
1574 	int			cnt = 0;
1575 	mac_client_impl_t	*mcip = mac_srs->srs_mcip;
1576 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
1577 
1578 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
1579 	ASSERT(!(mac_srs->srs_type & SRST_BW_CONTROL));
1580 
1581 	/* If we are blanked i.e. can't do upcalls, then we are done */
1582 	if (mac_srs->srs_state & (SRS_BLANK | SRS_PAUSE)) {
1583 		ASSERT((mac_srs->srs_type & SRST_NO_SOFT_RINGS) ||
1584 		    (mac_srs->srs_state & SRS_PAUSE));
1585 		goto out;
1586 	}
1587 
1588 	if (mac_srs->srs_first == NULL)
1589 		goto out;
1590 
1591 	if (!(mac_srs->srs_state & SRS_LATENCY_OPT) &&
1592 	    (srs_rx->sr_poll_pkt_cnt <= srs_rx->sr_lowat)) {
1593 		/*
1594 		 * In the normal case, the SRS worker thread does no
1595 		 * work and we wait for a backlog to build up before
1596 		 * we switch into polling mode. In case we are
1597 		 * optimizing for throughput, we use the worker thread
1598 		 * as well. The goal is to let worker thread process
1599 		 * the queue and poll thread to feed packets into
1600 		 * the queue. As such, we should signal the poll
1601 		 * thread to try and get more packets.
1602 		 *
1603 		 * We could have pulled this check in the POLL_RING
1604 		 * macro itself but keeping it explicit here makes
1605 		 * the architecture more human understandable.
1606 		 */
1607 		MAC_SRS_POLL_RING(mac_srs);
1608 	}
1609 
1610 again:
1611 	head = mac_srs->srs_first;
1612 	mac_srs->srs_first = NULL;
1613 	tail = mac_srs->srs_last;
1614 	mac_srs->srs_last = NULL;
1615 	cnt = mac_srs->srs_count;
1616 	mac_srs->srs_count = 0;
1617 
1618 	ASSERT(head != NULL);
1619 	ASSERT(tail != NULL);
1620 
1621 	if ((tid = mac_srs->srs_tid) != 0)
1622 		mac_srs->srs_tid = 0;
1623 
1624 	mac_srs->srs_state |= (SRS_PROC|proc_type);
1625 
1626 
1627 	/*
1628 	 * mcip is NULL for broadcast and multicast flows. The promisc
1629 	 * callbacks for broadcast and multicast packets are delivered from
1630 	 * mac_rx() and we don't need to worry about that case in this path
1631 	 */
1632 	if (mcip != NULL && mcip->mci_promisc_list != NULL) {
1633 		mutex_exit(&mac_srs->srs_lock);
1634 		mac_promisc_client_dispatch(mcip, head);
1635 		mutex_enter(&mac_srs->srs_lock);
1636 	}
1637 
1638 	/*
1639 	 * Check if SRS itself is doing the processing
1640 	 * This direct path does not apply when subflows are present. In this
1641 	 * case, packets need to be dispatched to a soft ring according to the
1642 	 * flow's bandwidth and other resources contraints.
1643 	 */
1644 	if (mac_srs->srs_type & SRST_NO_SOFT_RINGS) {
1645 		mac_direct_rx_t		proc;
1646 		void			*arg1;
1647 		mac_resource_handle_t	arg2;
1648 
1649 		/*
1650 		 * This is the case when a Rx is directly
1651 		 * assigned and we have a fully classified
1652 		 * protocol chain. We can deal with it in
1653 		 * one shot.
1654 		 */
1655 		proc = srs_rx->sr_func;
1656 		arg1 = srs_rx->sr_arg1;
1657 		arg2 = srs_rx->sr_arg2;
1658 
1659 		mac_srs->srs_state |= SRS_CLIENT_PROC;
1660 		mutex_exit(&mac_srs->srs_lock);
1661 		if (tid != 0) {
1662 			(void) untimeout(tid);
1663 			tid = 0;
1664 		}
1665 
1666 		proc(arg1, arg2, head, NULL);
1667 		/*
1668 		 * Decrement the size and count here itelf
1669 		 * since the packet has been processed.
1670 		 */
1671 		mutex_enter(&mac_srs->srs_lock);
1672 		MAC_UPDATE_SRS_COUNT_LOCKED(mac_srs, cnt);
1673 		if (mac_srs->srs_state & SRS_CLIENT_WAIT)
1674 			cv_signal(&mac_srs->srs_client_cv);
1675 		mac_srs->srs_state &= ~SRS_CLIENT_PROC;
1676 	} else {
1677 		/* Some kind of softrings based fanout is required */
1678 		mutex_exit(&mac_srs->srs_lock);
1679 		if (tid != 0) {
1680 			(void) untimeout(tid);
1681 			tid = 0;
1682 		}
1683 
1684 		/*
1685 		 * Since the fanout routines can deal with chains,
1686 		 * shoot the entire chain up.
1687 		 */
1688 		if (mac_srs->srs_type & SRST_FANOUT_SRC_IP)
1689 			mac_rx_srs_fanout(mac_srs, head);
1690 		else
1691 			mac_rx_srs_proto_fanout(mac_srs, head);
1692 		mutex_enter(&mac_srs->srs_lock);
1693 	}
1694 
1695 	if (!(mac_srs->srs_state & (SRS_LATENCY_OPT|SRS_BLANK|SRS_PAUSE))) {
1696 		/*
1697 		 * In case we are optimizing for throughput, we
1698 		 * should try and keep the worker thread running
1699 		 * as much as possible. Send the poll thread down
1700 		 * to check one more time if something else
1701 		 * arrived. In the meanwhile, if poll thread had
1702 		 * collected something due to earlier signal,
1703 		 * process it now.
1704 		 */
1705 		if (srs_rx->sr_poll_pkt_cnt <= srs_rx->sr_lowat) {
1706 			srs_rx->sr_drain_poll_sig++;
1707 			MAC_SRS_POLL_RING(mac_srs);
1708 		}
1709 		if (mac_srs->srs_first != NULL) {
1710 			srs_rx->sr_drain_again++;
1711 			goto again;
1712 		}
1713 	}
1714 
1715 out:
1716 	if (mac_srs->srs_state & SRS_GET_PKTS) {
1717 		/*
1718 		 * Poll thread is already running. Leave the
1719 		 * SRS_RPOC set and hand over the control to
1720 		 * poll thread.
1721 		 */
1722 		mac_srs->srs_state &= ~proc_type;
1723 		srs_rx->sr_drain_poll_running++;
1724 		return;
1725 	}
1726 
1727 	/*
1728 	 * Even if there are no packets queued in SRS, we
1729 	 * need to make sure that the shared counter is
1730 	 * clear and any associated softrings have cleared
1731 	 * all the backlog. Otherwise, leave the interface
1732 	 * in polling mode and the poll thread will get
1733 	 * signalled once the count goes down to zero.
1734 	 *
1735 	 * If someone is already draining the queue (SRS_PROC is
1736 	 * set) when the srs_poll_pkt_cnt goes down to zero,
1737 	 * then it means that drain is already running and we
1738 	 * will turn off polling at that time if there is
1739 	 * no backlog.
1740 	 *
1741 	 * As long as there are packets queued either
1742 	 * in soft ring set or its soft rings, we will leave
1743 	 * the interface in polling mode (even if the drain
1744 	 * was done being the interrupt thread). We signal
1745 	 * the poll thread as well if we have dipped below
1746 	 * low water mark.
1747 	 *
1748 	 * NOTE: We can't use the MAC_SRS_POLLING_ON macro
1749 	 * since that turn polling on only for worker thread.
1750 	 * Its not worth turning polling on for interrupt
1751 	 * thread (since NIC will not issue another interrupt)
1752 	 * unless a backlog builds up.
1753 	 */
1754 	if ((srs_rx->sr_poll_pkt_cnt > 0) &&
1755 	    (mac_srs->srs_state & SRS_POLLING_CAPAB)) {
1756 		mac_srs->srs_state &= ~(SRS_PROC|proc_type);
1757 		srs_rx->sr_drain_keep_polling++;
1758 		MAC_SRS_POLLING_ON(mac_srs);
1759 		if (srs_rx->sr_poll_pkt_cnt <= srs_rx->sr_lowat)
1760 			MAC_SRS_POLL_RING(mac_srs);
1761 		return;
1762 	}
1763 
1764 	/* Nothing else to do. Get out of poll mode */
1765 	MAC_SRS_POLLING_OFF(mac_srs);
1766 	mac_srs->srs_state &= ~(SRS_PROC|proc_type);
1767 	srs_rx->sr_drain_finish_intr++;
1768 }
1769 
1770 /*
1771  * mac_rx_srs_drain_bw
1772  *
1773  * The SRS BW drain routine. Gets to run to clear the queue. Any thread
1774  * (worker, interrupt, poll) can call this based on processing model.
1775  * The first thing we do is disable interrupts if possible and then
1776  * drain the queue. we also try to poll the underlying hardware if
1777  * there is a dedicated hardware Rx ring assigned to this SRS.
1778  *
1779  * There is a equivalent drain routine in non bandwidth control mode
1780  * mac_rx_srs_drain. There is some code duplication between the two
1781  * routines but they are highly performance sensitive and are easier
1782  * to read/debug if they stay separate. Any code changes here might
1783  * also apply to mac_rx_srs_drain as well.
1784  */
1785 void
1786 mac_rx_srs_drain_bw(mac_soft_ring_set_t *mac_srs, uint_t proc_type)
1787 {
1788 	mblk_t 			*head;
1789 	mblk_t			*tail;
1790 	timeout_id_t 		tid;
1791 	size_t			sz = 0;
1792 	int			cnt = 0;
1793 	mac_client_impl_t	*mcip = mac_srs->srs_mcip;
1794 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
1795 
1796 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
1797 	ASSERT(mac_srs->srs_type & SRST_BW_CONTROL);
1798 again:
1799 	/* Check if we are doing B/W control */
1800 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1801 	if (mac_srs->srs_bw->mac_bw_curr_time != lbolt) {
1802 		mac_srs->srs_bw->mac_bw_curr_time = lbolt;
1803 		mac_srs->srs_bw->mac_bw_used = 0;
1804 		if (mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)
1805 			mac_srs->srs_bw->mac_bw_state &= ~SRS_BW_ENFORCED;
1806 	} else if (mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED) {
1807 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1808 		goto done;
1809 	} else if (mac_srs->srs_bw->mac_bw_used >
1810 	    mac_srs->srs_bw->mac_bw_limit) {
1811 		mac_srs->srs_bw->mac_bw_state |= SRS_BW_ENFORCED;
1812 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1813 		goto done;
1814 	}
1815 	mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1816 
1817 	/* If we are blanked i.e. can't do upcalls, then we are done */
1818 	if (mac_srs->srs_state & (SRS_BLANK | SRS_PAUSE)) {
1819 		ASSERT((mac_srs->srs_type & SRST_NO_SOFT_RINGS) ||
1820 		    (mac_srs->srs_state & SRS_PAUSE));
1821 		goto done;
1822 	}
1823 
1824 	sz = 0;
1825 	cnt = 0;
1826 	if ((head = mac_srs_pick_chain(mac_srs, &tail, &sz, &cnt)) == NULL) {
1827 		/*
1828 		 * We couldn't pick up a single packet.
1829 		 */
1830 		mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1831 		if ((mac_srs->srs_bw->mac_bw_used == 0) &&
1832 		    (mac_srs->srs_size != 0) &&
1833 		    !(mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)) {
1834 			/*
1835 			 * Seems like configured B/W doesn't
1836 			 * even allow processing of 1 packet
1837 			 * per tick.
1838 			 *
1839 			 * XXX: raise the limit to processing
1840 			 * at least 1 packet per tick.
1841 			 */
1842 			mac_srs->srs_bw->mac_bw_limit +=
1843 			    mac_srs->srs_bw->mac_bw_limit;
1844 			mac_srs->srs_bw->mac_bw_drop_threshold +=
1845 			    mac_srs->srs_bw->mac_bw_drop_threshold;
1846 			cmn_err(CE_NOTE, "mac_rx_srs_drain: srs(%p) "
1847 			    "raised B/W limit to %d since not even a "
1848 			    "single packet can be processed per "
1849 			    "tick %d\n", (void *)mac_srs,
1850 			    (int)mac_srs->srs_bw->mac_bw_limit,
1851 			    (int)msgdsize(mac_srs->srs_first));
1852 		}
1853 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1854 		goto done;
1855 	}
1856 
1857 	ASSERT(head != NULL);
1858 	ASSERT(tail != NULL);
1859 
1860 	/* zero bandwidth: drop all and return to interrupt mode */
1861 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1862 	if (mac_srs->srs_bw->mac_bw_limit == 0) {
1863 		srs_rx->sr_drop_count += cnt;
1864 		ASSERT(mac_srs->srs_bw->mac_bw_sz >= sz);
1865 		mac_srs->srs_bw->mac_bw_sz -= sz;
1866 		mac_srs->srs_bw->mac_bw_drop_bytes += sz;
1867 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1868 		mac_pkt_drop(NULL, NULL, head, B_FALSE);
1869 		goto leave_poll;
1870 	} else {
1871 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1872 	}
1873 
1874 	if ((tid = mac_srs->srs_tid) != 0)
1875 		mac_srs->srs_tid = 0;
1876 
1877 	mac_srs->srs_state |= (SRS_PROC|proc_type);
1878 	MAC_SRS_WORKER_POLLING_ON(mac_srs);
1879 
1880 	/*
1881 	 * mcip is NULL for broadcast and multicast flows. The promisc
1882 	 * callbacks for broadcast and multicast packets are delivered from
1883 	 * mac_rx() and we don't need to worry about that case in this path
1884 	 */
1885 	if (mcip != NULL && mcip->mci_promisc_list != NULL) {
1886 		mutex_exit(&mac_srs->srs_lock);
1887 		mac_promisc_client_dispatch(mcip, head);
1888 		mutex_enter(&mac_srs->srs_lock);
1889 	}
1890 
1891 	/*
1892 	 * Check if SRS itself is doing the processing
1893 	 * This direct path does not apply when subflows are present. In this
1894 	 * case, packets need to be dispatched to a soft ring according to the
1895 	 * flow's bandwidth and other resources contraints.
1896 	 */
1897 	if (mac_srs->srs_type & SRST_NO_SOFT_RINGS) {
1898 		mac_direct_rx_t		proc;
1899 		void			*arg1;
1900 		mac_resource_handle_t	arg2;
1901 
1902 		/*
1903 		 * This is the case when a Rx is directly
1904 		 * assigned and we have a fully classified
1905 		 * protocol chain. We can deal with it in
1906 		 * one shot.
1907 		 */
1908 		proc = srs_rx->sr_func;
1909 		arg1 = srs_rx->sr_arg1;
1910 		arg2 = srs_rx->sr_arg2;
1911 
1912 		mac_srs->srs_state |= SRS_CLIENT_PROC;
1913 		mutex_exit(&mac_srs->srs_lock);
1914 		if (tid != 0) {
1915 			(void) untimeout(tid);
1916 			tid = 0;
1917 		}
1918 
1919 		proc(arg1, arg2, head, NULL);
1920 		/*
1921 		 * Decrement the size and count here itelf
1922 		 * since the packet has been processed.
1923 		 */
1924 		mutex_enter(&mac_srs->srs_lock);
1925 		MAC_UPDATE_SRS_COUNT_LOCKED(mac_srs, cnt);
1926 		MAC_UPDATE_SRS_SIZE_LOCKED(mac_srs, sz);
1927 
1928 		if (mac_srs->srs_state & SRS_CLIENT_WAIT)
1929 			cv_signal(&mac_srs->srs_client_cv);
1930 		mac_srs->srs_state &= ~SRS_CLIENT_PROC;
1931 	} else {
1932 		/* Some kind of softrings based fanout is required */
1933 		mutex_exit(&mac_srs->srs_lock);
1934 		if (tid != 0) {
1935 			(void) untimeout(tid);
1936 			tid = 0;
1937 		}
1938 
1939 		/*
1940 		 * Since the fanout routines can deal with chains,
1941 		 * shoot the entire chain up.
1942 		 */
1943 		if (mac_srs->srs_type & SRST_FANOUT_SRC_IP)
1944 			mac_rx_srs_fanout(mac_srs, head);
1945 		else
1946 			mac_rx_srs_proto_fanout(mac_srs, head);
1947 		mutex_enter(&mac_srs->srs_lock);
1948 	}
1949 
1950 	/*
1951 	 * Send the poll thread to pick up any packets arrived
1952 	 * so far. This also serves as the last check in case
1953 	 * nothing else is queued in the SRS. The poll thread
1954 	 * is signalled only in the case the drain was done
1955 	 * by the worker thread and SRS_WORKER is set. The
1956 	 * worker thread can run in parallel as long as the
1957 	 * SRS_WORKER flag is set. We we have nothing else to
1958 	 * process, we can exit while leaving SRS_PROC set
1959 	 * which gives the poll thread control to process and
1960 	 * cleanup once it returns from the NIC.
1961 	 *
1962 	 * If we have nothing else to process, we need to
1963 	 * ensure that we keep holding the srs_lock till
1964 	 * all the checks below are done and control is
1965 	 * handed to the poll thread if it was running.
1966 	 */
1967 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1968 	if (!(mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)) {
1969 		if (mac_srs->srs_first != NULL) {
1970 			if (proc_type == SRS_WORKER) {
1971 				mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1972 				if (srs_rx->sr_poll_pkt_cnt <=
1973 				    srs_rx->sr_lowat)
1974 					MAC_SRS_POLL_RING(mac_srs);
1975 				goto again;
1976 			} else {
1977 				cv_signal(&mac_srs->srs_async);
1978 			}
1979 		}
1980 	}
1981 	mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1982 
1983 done:
1984 
1985 	if (mac_srs->srs_state & SRS_GET_PKTS) {
1986 		/*
1987 		 * Poll thread is already running. Leave the
1988 		 * SRS_RPOC set and hand over the control to
1989 		 * poll thread.
1990 		 */
1991 		mac_srs->srs_state &= ~proc_type;
1992 		return;
1993 	}
1994 
1995 	/*
1996 	 * If we can't process packets because we have exceeded
1997 	 * B/W limit for this tick, just set the timeout
1998 	 * and leave.
1999 	 *
2000 	 * Even if there are no packets queued in SRS, we
2001 	 * need to make sure that the shared counter is
2002 	 * clear and any associated softrings have cleared
2003 	 * all the backlog. Otherwise, leave the interface
2004 	 * in polling mode and the poll thread will get
2005 	 * signalled once the count goes down to zero.
2006 	 *
2007 	 * If someone is already draining the queue (SRS_PROC is
2008 	 * set) when the srs_poll_pkt_cnt goes down to zero,
2009 	 * then it means that drain is already running and we
2010 	 * will turn off polling at that time if there is
2011 	 * no backlog. As long as there are packets queued either
2012 	 * is soft ring set or its soft rings, we will leave
2013 	 * the interface in polling mode.
2014 	 */
2015 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
2016 	if ((mac_srs->srs_state & SRS_POLLING_CAPAB) &&
2017 	    ((mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED) ||
2018 	    (srs_rx->sr_poll_pkt_cnt > 0))) {
2019 		MAC_SRS_POLLING_ON(mac_srs);
2020 		mac_srs->srs_state &= ~(SRS_PROC|proc_type);
2021 		if ((mac_srs->srs_first != NULL) &&
2022 		    (mac_srs->srs_tid == NULL))
2023 			mac_srs->srs_tid = timeout(mac_srs_fire,
2024 			    mac_srs, 1);
2025 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
2026 		return;
2027 	}
2028 	mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
2029 
2030 leave_poll:
2031 
2032 	/* Nothing else to do. Get out of poll mode */
2033 	MAC_SRS_POLLING_OFF(mac_srs);
2034 	mac_srs->srs_state &= ~(SRS_PROC|proc_type);
2035 }
2036 
2037 /*
2038  * mac_srs_worker
2039  *
2040  * The SRS worker routine. Drains the queue when no one else is
2041  * processing it.
2042  */
2043 void
2044 mac_srs_worker(mac_soft_ring_set_t *mac_srs)
2045 {
2046 	kmutex_t 		*lock = &mac_srs->srs_lock;
2047 	kcondvar_t 		*async = &mac_srs->srs_async;
2048 	callb_cpr_t		cprinfo;
2049 	boolean_t		bw_ctl_flag;
2050 
2051 	CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, "srs_worker");
2052 	mutex_enter(lock);
2053 
2054 start:
2055 	for (;;) {
2056 		bw_ctl_flag = B_FALSE;
2057 		if (mac_srs->srs_type & SRST_BW_CONTROL) {
2058 			MAC_SRS_BW_LOCK(mac_srs);
2059 			MAC_SRS_CHECK_BW_CONTROL(mac_srs);
2060 			if (mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)
2061 				bw_ctl_flag = B_TRUE;
2062 			MAC_SRS_BW_UNLOCK(mac_srs);
2063 		}
2064 		/*
2065 		 * The SRS_BW_ENFORCED flag may change since we have dropped
2066 		 * the mac_bw_lock. However the drain function can handle both
2067 		 * a drainable SRS or a bandwidth controlled SRS, and the
2068 		 * effect of scheduling a timeout is to wakeup the worker
2069 		 * thread which in turn will call the drain function. Since
2070 		 * we release the srs_lock atomically only in the cv_wait there
2071 		 * isn't a fear of waiting for ever.
2072 		 */
2073 		while (((mac_srs->srs_state & SRS_PROC) ||
2074 		    (mac_srs->srs_first == NULL) || bw_ctl_flag ||
2075 		    (mac_srs->srs_state & SRS_TX_BLOCKED)) &&
2076 		    !(mac_srs->srs_state & SRS_PAUSE)) {
2077 			/*
2078 			 * If we have packets queued and we are here
2079 			 * because B/W control is in place, we better
2080 			 * schedule the worker wakeup after 1 tick
2081 			 * to see if bandwidth control can be relaxed.
2082 			 */
2083 			if (bw_ctl_flag && mac_srs->srs_tid == NULL) {
2084 				/*
2085 				 * We need to ensure that a timer  is already
2086 				 * scheduled or we force  schedule one for
2087 				 * later so that we can continue processing
2088 				 * after this  quanta is over.
2089 				 */
2090 				mac_srs->srs_tid = timeout(mac_srs_fire,
2091 				    mac_srs, 1);
2092 			}
2093 wait:
2094 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
2095 			cv_wait(async, lock);
2096 			CALLB_CPR_SAFE_END(&cprinfo, lock);
2097 
2098 			if (mac_srs->srs_state & SRS_PAUSE)
2099 				goto done;
2100 			if (mac_srs->srs_state & SRS_PROC)
2101 				goto wait;
2102 
2103 			if (mac_srs->srs_first != NULL &&
2104 			    mac_srs->srs_type & SRST_BW_CONTROL) {
2105 				MAC_SRS_BW_LOCK(mac_srs);
2106 				if (mac_srs->srs_bw->mac_bw_state &
2107 				    SRS_BW_ENFORCED) {
2108 					MAC_SRS_CHECK_BW_CONTROL(mac_srs);
2109 				}
2110 				bw_ctl_flag = mac_srs->srs_bw->mac_bw_state &
2111 				    SRS_BW_ENFORCED;
2112 				MAC_SRS_BW_UNLOCK(mac_srs);
2113 			}
2114 		}
2115 
2116 		if (mac_srs->srs_state & SRS_PAUSE)
2117 			goto done;
2118 		mac_srs->srs_drain_func(mac_srs, SRS_WORKER);
2119 	}
2120 done:
2121 	/*
2122 	 * The Rx SRS quiesce logic first cuts off packet supply to the SRS
2123 	 * from both hard and soft classifications and waits for such threads
2124 	 * to finish before signaling the worker. So at this point the only
2125 	 * thread left that could be competing with the worker is the poll
2126 	 * thread. In the case of Tx, there shouldn't be any thread holding
2127 	 * SRS_PROC at this point.
2128 	 */
2129 	if (!(mac_srs->srs_state & SRS_PROC)) {
2130 		mac_srs->srs_state |= SRS_PROC;
2131 	} else {
2132 		ASSERT((mac_srs->srs_type & SRST_TX) == 0);
2133 		/*
2134 		 * Poll thread still owns the SRS and is still running
2135 		 */
2136 		ASSERT((mac_srs->srs_poll_thr == NULL) ||
2137 		    ((mac_srs->srs_state & SRS_POLL_THR_OWNER) ==
2138 		    SRS_POLL_THR_OWNER));
2139 	}
2140 	mac_srs_worker_quiesce(mac_srs);
2141 	/*
2142 	 * Wait for the SRS_RESTART or SRS_CONDEMNED signal from the initiator
2143 	 * of the quiesce operation
2144 	 */
2145 	while (!(mac_srs->srs_state & (SRS_CONDEMNED | SRS_RESTART)))
2146 		cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
2147 
2148 	if (mac_srs->srs_state & SRS_RESTART) {
2149 		ASSERT(!(mac_srs->srs_state & SRS_CONDEMNED));
2150 		mac_srs_worker_restart(mac_srs);
2151 		mac_srs->srs_state &= ~SRS_PROC;
2152 		goto start;
2153 	}
2154 
2155 	if (!(mac_srs->srs_state & SRS_CONDEMNED_DONE))
2156 		mac_srs_worker_quiesce(mac_srs);
2157 
2158 	mac_srs->srs_state &= ~SRS_PROC;
2159 	/* The macro drops the srs_lock */
2160 	CALLB_CPR_EXIT(&cprinfo);
2161 	thread_exit();
2162 }
2163 
2164 /*
2165  * mac_rx_srs_subflow_process
2166  *
2167  * Receive side routine called from interrupt path when there are
2168  * sub flows present on this SRS.
2169  */
2170 /* ARGSUSED */
2171 void
2172 mac_rx_srs_subflow_process(void *arg, mac_resource_handle_t srs,
2173     mblk_t *mp_chain, boolean_t loopback)
2174 {
2175 	flow_entry_t		*flent = NULL;
2176 	flow_entry_t		*prev_flent = NULL;
2177 	mblk_t			*mp = NULL;
2178 	mblk_t			*tail = NULL;
2179 	mac_soft_ring_set_t	*mac_srs = (mac_soft_ring_set_t *)srs;
2180 	mac_client_impl_t	*mcip;
2181 
2182 	mcip = mac_srs->srs_mcip;
2183 	ASSERT(mcip != NULL);
2184 
2185 	/*
2186 	 * We need to determine the SRS for every packet
2187 	 * by walking the flow table, if we don't get any,
2188 	 * then we proceed using the SRS we came with.
2189 	 */
2190 	mp = tail = mp_chain;
2191 	while (mp != NULL) {
2192 
2193 		/*
2194 		 * We will increment the stats for the mactching subflow.
2195 		 * when we get the bytes/pkt count for the classified packets
2196 		 * later in mac_rx_srs_process.
2197 		 */
2198 		(void) mac_flow_lookup(mcip->mci_subflow_tab, mp,
2199 		    FLOW_INBOUND, &flent);
2200 
2201 		if (mp == mp_chain || flent == prev_flent) {
2202 			if (prev_flent != NULL)
2203 				FLOW_REFRELE(prev_flent);
2204 			prev_flent = flent;
2205 			flent = NULL;
2206 			tail = mp;
2207 			mp = mp->b_next;
2208 			continue;
2209 		}
2210 		tail->b_next = NULL;
2211 		/*
2212 		 * A null indicates, this is for the mac_srs itself.
2213 		 * XXX-venu : probably assert for fe_rx_srs_cnt == 0.
2214 		 */
2215 		if (prev_flent == NULL || prev_flent->fe_rx_srs_cnt == 0) {
2216 			mac_rx_srs_process(arg,
2217 			    (mac_resource_handle_t)mac_srs, mp_chain,
2218 			    loopback);
2219 		} else {
2220 			(prev_flent->fe_cb_fn)(prev_flent->fe_cb_arg1,
2221 			    prev_flent->fe_cb_arg2, mp_chain, loopback);
2222 			FLOW_REFRELE(prev_flent);
2223 		}
2224 		prev_flent = flent;
2225 		flent = NULL;
2226 		mp_chain = mp;
2227 		tail = mp;
2228 		mp = mp->b_next;
2229 	}
2230 	/* Last chain */
2231 	ASSERT(mp_chain != NULL);
2232 	if (prev_flent == NULL || prev_flent->fe_rx_srs_cnt == 0) {
2233 		mac_rx_srs_process(arg,
2234 		    (mac_resource_handle_t)mac_srs, mp_chain, loopback);
2235 	} else {
2236 		(prev_flent->fe_cb_fn)(prev_flent->fe_cb_arg1,
2237 		    prev_flent->fe_cb_arg2, mp_chain, loopback);
2238 		FLOW_REFRELE(prev_flent);
2239 	}
2240 }
2241 
2242 /*
2243  * mac_rx_srs_process
2244  *
2245  * Receive side routine called from the interrupt path.
2246  *
2247  * loopback is set to force a context switch on the loopback
2248  * path between MAC clients.
2249  */
2250 /* ARGSUSED */
2251 void
2252 mac_rx_srs_process(void *arg, mac_resource_handle_t srs, mblk_t *mp_chain,
2253     boolean_t loopback)
2254 {
2255 	mac_soft_ring_set_t	*mac_srs = (mac_soft_ring_set_t *)srs;
2256 	mblk_t			*mp, *tail, *head;
2257 	int			count = 0;
2258 	int			count1;
2259 	size_t			sz = 0;
2260 	size_t			chain_sz, sz1;
2261 	mac_bw_ctl_t		*mac_bw;
2262 	mac_client_impl_t	*smcip;
2263 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
2264 
2265 	/*
2266 	 * Set the tail, count and sz. We set the sz irrespective
2267 	 * of whether we are doing B/W control or not for the
2268 	 * purpose of updating the stats.
2269 	 */
2270 	mp = tail = mp_chain;
2271 	while (mp != NULL) {
2272 		tail = mp;
2273 		count++;
2274 		sz += msgdsize(mp);
2275 		mp = mp->b_next;
2276 	}
2277 
2278 	mutex_enter(&mac_srs->srs_lock);
2279 	smcip = mac_srs->srs_mcip;
2280 
2281 	if (mac_srs->srs_type & SRST_FLOW || smcip == NULL) {
2282 		FLOW_STAT_UPDATE(mac_srs->srs_flent, rbytes, sz);
2283 		FLOW_STAT_UPDATE(mac_srs->srs_flent, ipackets, count);
2284 	}
2285 	if (smcip != NULL) {
2286 		smcip->mci_stat_ibytes += sz;
2287 		smcip->mci_stat_ipackets += count;
2288 	}
2289 
2290 	/*
2291 	 * If the SRS in already being processed; has been blanked;
2292 	 * can be processed by worker thread only; or the B/W limit
2293 	 * has been reached, then queue the chain and check if
2294 	 * worker thread needs to be awakend.
2295 	 */
2296 	if (mac_srs->srs_type & SRST_BW_CONTROL) {
2297 		mac_bw = mac_srs->srs_bw;
2298 		ASSERT(mac_bw != NULL);
2299 		mutex_enter(&mac_bw->mac_bw_lock);
2300 		/* Count the packets and bytes via interrupt */
2301 		srs_rx->sr_intr_count += count;
2302 		mac_bw->mac_bw_intr += sz;
2303 		if (mac_bw->mac_bw_limit == 0) {
2304 			/* zero bandwidth: drop all */
2305 			srs_rx->sr_drop_count += count;
2306 			mac_bw->mac_bw_drop_bytes += sz;
2307 			mutex_exit(&mac_bw->mac_bw_lock);
2308 			mutex_exit(&mac_srs->srs_lock);
2309 			mac_pkt_drop(NULL, NULL, mp_chain, B_FALSE);
2310 			return;
2311 		} else {
2312 			if ((mac_bw->mac_bw_sz + sz) <=
2313 			    mac_bw->mac_bw_drop_threshold) {
2314 				mutex_exit(&mac_bw->mac_bw_lock);
2315 				MAC_RX_SRS_ENQUEUE_CHAIN(mac_srs, mp_chain,
2316 				    tail, count, sz);
2317 			} else {
2318 				mp = mp_chain;
2319 				chain_sz = 0;
2320 				count1 = 0;
2321 				tail = NULL;
2322 				head = NULL;
2323 				while (mp != NULL) {
2324 					sz1 = msgdsize(mp);
2325 					if (mac_bw->mac_bw_sz + chain_sz + sz1 >
2326 					    mac_bw->mac_bw_drop_threshold)
2327 						break;
2328 					chain_sz += sz1;
2329 					count1++;
2330 					tail = mp;
2331 					mp = mp->b_next;
2332 				}
2333 				mutex_exit(&mac_bw->mac_bw_lock);
2334 				if (tail != NULL) {
2335 					head = tail->b_next;
2336 					tail->b_next = NULL;
2337 					MAC_RX_SRS_ENQUEUE_CHAIN(mac_srs,
2338 					    mp_chain, tail, count1, chain_sz);
2339 					sz -= chain_sz;
2340 					count -= count1;
2341 				} else {
2342 					/* Can't pick up any */
2343 					head = mp_chain;
2344 				}
2345 				if (head != NULL) {
2346 					/* Drop any packet over the threshold */
2347 					srs_rx->sr_drop_count += count;
2348 					mutex_enter(&mac_bw->mac_bw_lock);
2349 					mac_bw->mac_bw_drop_bytes += sz;
2350 					mutex_exit(&mac_bw->mac_bw_lock);
2351 					freemsgchain(head);
2352 				}
2353 			}
2354 			MAC_SRS_WORKER_WAKEUP(mac_srs);
2355 			mutex_exit(&mac_srs->srs_lock);
2356 			return;
2357 		}
2358 	}
2359 
2360 	/*
2361 	 * If the total number of packets queued in the SRS and
2362 	 * its associated soft rings exceeds the max allowed,
2363 	 * then drop the chain. If we are polling capable, this
2364 	 * shouldn't be happening.
2365 	 */
2366 	if (!(mac_srs->srs_type & SRST_BW_CONTROL) &&
2367 	    (srs_rx->sr_poll_pkt_cnt > srs_rx->sr_hiwat)) {
2368 		mac_bw = mac_srs->srs_bw;
2369 		srs_rx->sr_drop_count += count;
2370 		mutex_enter(&mac_bw->mac_bw_lock);
2371 		mac_bw->mac_bw_drop_bytes += sz;
2372 		mutex_exit(&mac_bw->mac_bw_lock);
2373 		freemsgchain(mp_chain);
2374 		mutex_exit(&mac_srs->srs_lock);
2375 		return;
2376 	}
2377 
2378 	MAC_RX_SRS_ENQUEUE_CHAIN(mac_srs, mp_chain, tail, count, sz);
2379 	/* Count the packets entering via interrupt path */
2380 	srs_rx->sr_intr_count += count;
2381 
2382 	if (!(mac_srs->srs_state & SRS_PROC)) {
2383 		/*
2384 		 * If we are coming via loopback or if we are not
2385 		 * optimizing for latency, we should signal the
2386 		 * worker thread.
2387 		 */
2388 		if (loopback || !(mac_srs->srs_state & SRS_LATENCY_OPT)) {
2389 			/*
2390 			 * For loopback, We need to let the worker take
2391 			 * over as we don't want to continue in the same
2392 			 * thread even if we can. This could lead to stack
2393 			 * overflows and may also end up using
2394 			 * resources (cpu) incorrectly.
2395 			 */
2396 			cv_signal(&mac_srs->srs_async);
2397 		} else {
2398 			/*
2399 			 * Seems like no one is processing the SRS and
2400 			 * there is no backlog. We also inline process
2401 			 * our packet if its a single packet in non
2402 			 * latency optimized case (in latency optimized
2403 			 * case, we inline process chains of any size).
2404 			 */
2405 			mac_srs->srs_drain_func(mac_srs, SRS_PROC_FAST);
2406 		}
2407 	}
2408 	mutex_exit(&mac_srs->srs_lock);
2409 }
2410 
2411 /* TX SIDE ROUTINES (RUNTIME) */
2412 
2413 /*
2414  * mac_tx_srs_no_desc
2415  *
2416  * This routine is called by Tx single ring default mode
2417  * when Tx ring runs out of descs.
2418  */
2419 mac_tx_cookie_t
2420 mac_tx_srs_no_desc(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2421     uint16_t flag, mblk_t **ret_mp)
2422 {
2423 	mac_tx_cookie_t cookie = NULL;
2424 	mac_srs_tx_t *srs_tx = &mac_srs->srs_tx;
2425 	boolean_t wakeup_worker = B_TRUE;
2426 	uint32_t tx_mode = srs_tx->st_mode;
2427 	int cnt, sz;
2428 	mblk_t *tail;
2429 
2430 	ASSERT(tx_mode == SRS_TX_DEFAULT || tx_mode == SRS_TX_BW);
2431 	if (flag & MAC_DROP_ON_NO_DESC) {
2432 		MAC_TX_SRS_DROP_MESSAGE(mac_srs, mp_chain, cookie);
2433 	} else {
2434 		if (mac_srs->srs_first != NULL)
2435 			wakeup_worker = B_FALSE;
2436 		MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
2437 		if (flag & MAC_TX_NO_ENQUEUE) {
2438 			/*
2439 			 * If TX_QUEUED is not set, queue the
2440 			 * packet and let mac_tx_srs_drain()
2441 			 * set the TX_BLOCKED bit for the
2442 			 * reasons explained above. Otherwise,
2443 			 * return the mblks.
2444 			 */
2445 			if (wakeup_worker) {
2446 				MAC_TX_SRS_ENQUEUE_CHAIN(mac_srs,
2447 				    mp_chain, tail, cnt, sz);
2448 			} else {
2449 				MAC_TX_SET_NO_ENQUEUE(mac_srs,
2450 				    mp_chain, ret_mp, cookie);
2451 			}
2452 		} else {
2453 			MAC_TX_SRS_TEST_HIWAT(mac_srs, mp_chain,
2454 			    tail, cnt, sz, cookie);
2455 		}
2456 		if (wakeup_worker)
2457 			cv_signal(&mac_srs->srs_async);
2458 	}
2459 	return (cookie);
2460 }
2461 
2462 /*
2463  * mac_tx_srs_enqueue
2464  *
2465  * This routine is called when Tx SRS is operating in either serializer
2466  * or bandwidth mode. In serializer mode, a packet will get enqueued
2467  * when a thread cannot enter SRS exclusively. In bandwidth mode,
2468  * packets gets queued if allowed byte-count limit for a tick is
2469  * exceeded. The action that gets taken when MAC_DROP_ON_NO_DESC and
2470  * MAC_TX_NO_ENQUEUE is set is different than when operaing in either
2471  * the default mode or fanout mode. Here packets get dropped or
2472  * returned back to the caller only after hi-watermark worth of data
2473  * is queued.
2474  */
2475 static mac_tx_cookie_t
2476 mac_tx_srs_enqueue(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2477     uint16_t flag, uintptr_t fanout_hint, mblk_t **ret_mp)
2478 {
2479 	mac_tx_cookie_t cookie = NULL;
2480 	int cnt, sz;
2481 	mblk_t *tail;
2482 	boolean_t wakeup_worker = B_TRUE;
2483 
2484 	/*
2485 	 * Ignore fanout hint if we don't have multiple tx rings.
2486 	 */
2487 	if (!TX_MULTI_RING_MODE(mac_srs))
2488 		fanout_hint = 0;
2489 
2490 	if (mac_srs->srs_first != NULL)
2491 		wakeup_worker = B_FALSE;
2492 	MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
2493 	if (flag & MAC_DROP_ON_NO_DESC) {
2494 		if (mac_srs->srs_count > mac_srs->srs_tx.st_hiwat) {
2495 			MAC_TX_SRS_DROP_MESSAGE(mac_srs, mp_chain, cookie);
2496 		} else {
2497 			MAC_TX_SRS_ENQUEUE_CHAIN(mac_srs,
2498 			    mp_chain, tail, cnt, sz);
2499 		}
2500 	} else if (flag & MAC_TX_NO_ENQUEUE) {
2501 		if ((mac_srs->srs_count > mac_srs->srs_tx.st_hiwat) ||
2502 		    (mac_srs->srs_state & SRS_TX_WAKEUP_CLIENT)) {
2503 			MAC_TX_SET_NO_ENQUEUE(mac_srs, mp_chain,
2504 			    ret_mp, cookie);
2505 		} else {
2506 			mp_chain->b_prev = (mblk_t *)fanout_hint;
2507 			MAC_TX_SRS_ENQUEUE_CHAIN(mac_srs,
2508 			    mp_chain, tail, cnt, sz);
2509 		}
2510 	} else {
2511 		/*
2512 		 * If you are BW_ENFORCED, just enqueue the
2513 		 * packet. srs_worker will drain it at the
2514 		 * prescribed rate. Before enqueueing, save
2515 		 * the fanout hint.
2516 		 */
2517 		mp_chain->b_prev = (mblk_t *)fanout_hint;
2518 		MAC_TX_SRS_TEST_HIWAT(mac_srs, mp_chain,
2519 		    tail, cnt, sz, cookie);
2520 	}
2521 	if (wakeup_worker)
2522 		cv_signal(&mac_srs->srs_async);
2523 	return (cookie);
2524 }
2525 
2526 /*
2527  * There are five tx modes:
2528  *
2529  * 1) Default mode (SRS_TX_DEFAULT)
2530  * 2) Serialization mode (SRS_TX_SERIALIZE)
2531  * 3) Fanout mode (SRS_TX_FANOUT)
2532  * 4) Bandwdith mode (SRS_TX_BW)
2533  * 5) Fanout and Bandwidth mode (SRS_TX_BW_FANOUT)
2534  *
2535  * The tx mode in which an SRS operates is decided in mac_tx_srs_setup()
2536  * based on the number of Tx rings requested for an SRS and whether
2537  * bandwidth control is requested or not.
2538  *
2539  * In the default mode (i.e., no fanout/no bandwidth), the SRS acts as a
2540  * pass-thru. Packets will go directly to mac_tx_send(). When the underlying
2541  * Tx ring runs out of Tx descs, it starts queueing up packets in SRS.
2542  * When flow-control is relieved, the srs_worker drains the queued
2543  * packets and informs blocked clients to restart sending packets.
2544  *
2545  * In the SRS_TX_SERIALIZE mode, all calls to mac_tx() are serialized.
2546  *
2547  * In the SRS_TX_FANOUT mode, packets will be fanned out to multiple
2548  * Tx rings. Each Tx ring will have a soft ring associated with it.
2549  * These soft rings will be hung off the Tx SRS. Queueing if it happens
2550  * due to lack of Tx desc will be in individual soft ring (and not srs)
2551  * associated with Tx ring.
2552  *
2553  * In the TX_BW mode, tx srs will allow packets to go down to Tx ring
2554  * only if bw is available. Otherwise the packets will be queued in
2555  * SRS. If fanout to multiple Tx rings is configured, the packets will
2556  * be fanned out among the soft rings associated with the Tx rings.
2557  *
2558  * Four flags are used in srs_state for indicating flow control
2559  * conditions : SRS_TX_BLOCKED, SRS_TX_HIWAT, SRS_TX_WAKEUP_CLIENT.
2560  * SRS_TX_BLOCKED indicates out of Tx descs. SRS expects a wakeup from the
2561  * driver below.
2562  * SRS_TX_HIWAT indicates packet count enqueued in Tx SRS exceeded Tx hiwat
2563  * and flow-control pressure is applied back to clients. The clients expect
2564  * wakeup when flow-control is relieved.
2565  * SRS_TX_WAKEUP_CLIENT get set when (flag == MAC_TX_NO_ENQUEUE) and mblk
2566  * got returned back to client either due to lack of Tx descs or due to bw
2567  * control reasons. The clients expect a wakeup when condition is relieved.
2568  *
2569  * The fourth argument to mac_tx() is the flag. Normally it will be 0 but
2570  * some clients set the following values too: MAC_DROP_ON_NO_DESC,
2571  * MAC_TX_NO_ENQUEUE
2572  * Mac clients that do not want packets to be enqueued in the mac layer set
2573  * MAC_DROP_ON_NO_DESC value. The packets won't be queued in the Tx SRS or
2574  * Tx soft rings but instead get dropped when the NIC runs out of desc. The
2575  * behaviour of this flag is different when the Tx is running in serializer
2576  * or bandwidth mode. Under these (Serializer, bandwidth) modes, the packet
2577  * get dropped when Tx high watermark is reached.
2578  * There are some mac clients like vsw, aggr that want the mblks to be
2579  * returned back to clients instead of being queued in Tx SRS (or Tx soft
2580  * rings) under flow-control (i.e., out of desc or exceeding bw limits)
2581  * conditions. These clients call mac_tx() with MAC_TX_NO_ENQUEUE flag set.
2582  * In the default and Tx fanout mode, the un-transmitted mblks will be
2583  * returned back to the clients when the driver runs out of Tx descs.
2584  * SRS_TX_WAKEUP_CLIENT (or S_RING_WAKEUP_CLIENT) will be set in SRS (or
2585  * soft ring) so that the clients can be woken up when Tx desc become
2586  * available. When running in serializer or bandwidth mode mode,
2587  * SRS_TX_WAKEUP_CLIENT will be set when tx hi-watermark is reached.
2588  */
2589 
2590 mac_tx_func_t
2591 mac_tx_get_func(uint32_t mode)
2592 {
2593 	return (mac_tx_mode_list[mode].mac_tx_func);
2594 }
2595 
2596 /* ARGSUSED */
2597 static mac_tx_cookie_t
2598 mac_tx_single_ring_mode(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2599     uintptr_t fanout_hint, uint16_t flag, mblk_t **ret_mp)
2600 {
2601 	mac_srs_tx_t		*srs_tx = &mac_srs->srs_tx;
2602 	boolean_t		is_subflow;
2603 	mac_tx_stats_t		stats;
2604 	mac_tx_cookie_t		cookie = NULL;
2605 
2606 	ASSERT(srs_tx->st_mode == SRS_TX_DEFAULT);
2607 
2608 	/* Regular case with a single Tx ring */
2609 	/*
2610 	 * SRS_TX_BLOCKED is set when underlying NIC runs
2611 	 * out of Tx descs and messages start getting
2612 	 * queued. It won't get reset until
2613 	 * tx_srs_drain() completely drains out the
2614 	 * messages.
2615 	 */
2616 	if ((mac_srs->srs_state & SRS_ENQUEUED) != 0) {
2617 		/* Tx descs/resources not available */
2618 		mutex_enter(&mac_srs->srs_lock);
2619 		if ((mac_srs->srs_state & SRS_ENQUEUED) != 0) {
2620 			cookie = mac_tx_srs_no_desc(mac_srs, mp_chain,
2621 			    flag, ret_mp);
2622 			mutex_exit(&mac_srs->srs_lock);
2623 			return (cookie);
2624 		}
2625 		/*
2626 		 * While we were computing mblk count, the
2627 		 * flow control condition got relieved.
2628 		 * Continue with the transmission.
2629 		 */
2630 		mutex_exit(&mac_srs->srs_lock);
2631 	}
2632 
2633 	is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
2634 
2635 	mp_chain = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
2636 	    mp_chain, (is_subflow ? &stats : NULL));
2637 
2638 	/*
2639 	 * Multiple threads could be here sending packets.
2640 	 * Under such conditions, it is not possible to
2641 	 * automically set SRS_TX_BLOCKED bit to indicate
2642 	 * out of tx desc condition. To atomically set
2643 	 * this, we queue the returned packet and do
2644 	 * the setting of SRS_TX_BLOCKED in
2645 	 * mac_tx_srs_drain().
2646 	 */
2647 	if (mp_chain != NULL) {
2648 		mutex_enter(&mac_srs->srs_lock);
2649 		cookie = mac_tx_srs_no_desc(mac_srs, mp_chain, flag, ret_mp);
2650 		mutex_exit(&mac_srs->srs_lock);
2651 		return (cookie);
2652 	}
2653 
2654 	if (is_subflow)
2655 		FLOW_TX_STATS_UPDATE(mac_srs->srs_flent, &stats);
2656 
2657 	return (NULL);
2658 }
2659 
2660 /*
2661  * mac_tx_serialize_mode
2662  *
2663  * This is an experimental mode implemented as per the request of PAE.
2664  * In this mode, all callers attempting to send a packet to the NIC
2665  * will get serialized. Only one thread at any time will access the
2666  * NIC to send the packet out.
2667  */
2668 /* ARGSUSED */
2669 static mac_tx_cookie_t
2670 mac_tx_serializer_mode(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2671     uintptr_t fanout_hint, uint16_t flag, mblk_t **ret_mp)
2672 {
2673 	boolean_t		is_subflow;
2674 	mac_tx_stats_t		stats;
2675 	mac_tx_cookie_t		cookie = NULL;
2676 	mac_srs_tx_t		*srs_tx = &mac_srs->srs_tx;
2677 
2678 	/* Single ring, serialize below */
2679 	ASSERT(srs_tx->st_mode == SRS_TX_SERIALIZE);
2680 	mutex_enter(&mac_srs->srs_lock);
2681 	if ((mac_srs->srs_first != NULL) ||
2682 	    (mac_srs->srs_state & SRS_PROC)) {
2683 		/*
2684 		 * In serialization mode, queue all packets until
2685 		 * TX_HIWAT is set.
2686 		 * If drop bit is set, drop if TX_HIWAT is set.
2687 		 * If no_enqueue is set, still enqueue until hiwat
2688 		 * is set and return mblks after TX_HIWAT is set.
2689 		 */
2690 		cookie = mac_tx_srs_enqueue(mac_srs, mp_chain,
2691 		    flag, NULL, ret_mp);
2692 		mutex_exit(&mac_srs->srs_lock);
2693 		return (cookie);
2694 	}
2695 	/*
2696 	 * No packets queued, nothing on proc and no flow
2697 	 * control condition. Fast-path, ok. Do inline
2698 	 * processing.
2699 	 */
2700 	mac_srs->srs_state |= SRS_PROC;
2701 	mutex_exit(&mac_srs->srs_lock);
2702 
2703 	is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
2704 
2705 	mp_chain = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
2706 	    mp_chain, (is_subflow ? &stats : NULL));
2707 
2708 	mutex_enter(&mac_srs->srs_lock);
2709 	mac_srs->srs_state &= ~SRS_PROC;
2710 	if (mp_chain != NULL) {
2711 		cookie = mac_tx_srs_enqueue(mac_srs,
2712 		    mp_chain, flag, NULL, ret_mp);
2713 	}
2714 	if (mac_srs->srs_first != NULL) {
2715 		/*
2716 		 * We processed inline our packet and a new
2717 		 * packet/s got queued while we were
2718 		 * processing. Wakeup srs worker
2719 		 */
2720 		cv_signal(&mac_srs->srs_async);
2721 	}
2722 	mutex_exit(&mac_srs->srs_lock);
2723 
2724 	if (is_subflow && cookie == NULL)
2725 		FLOW_TX_STATS_UPDATE(mac_srs->srs_flent, &stats);
2726 
2727 	return (cookie);
2728 }
2729 
2730 /*
2731  * mac_tx_fanout_mode
2732  *
2733  * In this mode, the SRS will have access to multiple Tx rings to send
2734  * the packet out. The fanout hint that is passed as an argument is
2735  * used to find an appropriate ring to fanout the traffic. Each Tx
2736  * ring, in turn,  will have a soft ring associated with it. If a Tx
2737  * ring runs out of Tx desc's the returned packet will be queued in
2738  * the soft ring associated with that Tx ring. The srs itself will not
2739  * queue any packets.
2740  */
2741 
2742 #define	MAC_TX_SOFT_RING_PROCESS(chain) {		       		\
2743 	index = COMPUTE_INDEX(hash, mac_srs->srs_oth_ring_count),	\
2744 	softring = mac_srs->srs_oth_soft_rings[index];			\
2745 	cookie = mac_tx_soft_ring_process(softring, chain, flag, ret_mp); \
2746 	DTRACE_PROBE2(tx__fanout, uint64_t, hash, uint_t, index);	\
2747 }
2748 
2749 static mac_tx_cookie_t
2750 mac_tx_fanout_mode(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2751     uintptr_t fanout_hint, uint16_t flag, mblk_t **ret_mp)
2752 {
2753 	mac_soft_ring_t		*softring;
2754 	uint64_t		hash;
2755 	uint_t			index;
2756 	mac_tx_cookie_t		cookie = NULL;
2757 
2758 	ASSERT(mac_srs->srs_tx.st_mode == SRS_TX_FANOUT);
2759 	if (fanout_hint != 0) {
2760 		/*
2761 		 * The hint is specified by the caller, simply pass the
2762 		 * whole chain to the soft ring.
2763 		 */
2764 		hash = HASH_HINT(fanout_hint);
2765 		MAC_TX_SOFT_RING_PROCESS(mp_chain);
2766 	} else {
2767 		mblk_t *last_mp, *cur_mp, *sub_chain;
2768 		uint64_t last_hash = 0;
2769 		uint_t media = mac_srs->srs_mcip->mci_mip->mi_info.mi_media;
2770 
2771 		/*
2772 		 * Compute the hash from the contents (headers) of the
2773 		 * packets of the mblk chain. Split the chains into
2774 		 * subchains of the same conversation.
2775 		 *
2776 		 * Since there may be more than one ring used for
2777 		 * sub-chains of the same call, and since the caller
2778 		 * does not maintain per conversation state since it
2779 		 * passed a zero hint, unsent subchains will be
2780 		 * dropped.
2781 		 */
2782 
2783 		flag |= MAC_DROP_ON_NO_DESC;
2784 		ret_mp = NULL;
2785 
2786 		ASSERT(ret_mp == NULL);
2787 
2788 		sub_chain = NULL;
2789 		last_mp = NULL;
2790 
2791 		for (cur_mp = mp_chain; cur_mp != NULL;
2792 		    cur_mp = cur_mp->b_next) {
2793 			hash = mac_pkt_hash(media, cur_mp, MAC_PKT_HASH_L4,
2794 			    B_TRUE);
2795 			if (last_hash != 0 && hash != last_hash) {
2796 				/*
2797 				 * Starting a different subchain, send current
2798 				 * chain out.
2799 				 */
2800 				ASSERT(last_mp != NULL);
2801 				last_mp->b_next = NULL;
2802 				MAC_TX_SOFT_RING_PROCESS(sub_chain);
2803 				sub_chain = NULL;
2804 			}
2805 
2806 			/* add packet to subchain */
2807 			if (sub_chain == NULL)
2808 				sub_chain = cur_mp;
2809 			last_mp = cur_mp;
2810 			last_hash = hash;
2811 		}
2812 
2813 		if (sub_chain != NULL) {
2814 			/* send last subchain */
2815 			ASSERT(last_mp != NULL);
2816 			last_mp->b_next = NULL;
2817 			MAC_TX_SOFT_RING_PROCESS(sub_chain);
2818 		}
2819 
2820 		cookie = NULL;
2821 	}
2822 
2823 	return (cookie);
2824 }
2825 
2826 /*
2827  * mac_tx_bw_mode
2828  *
2829  * In the bandwidth mode, Tx srs will allow packets to go down to Tx ring
2830  * only if bw is available. Otherwise the packets will be queued in
2831  * SRS. If the SRS has multiple Tx rings, then packets will get fanned
2832  * out to a Tx rings.
2833  */
2834 static mac_tx_cookie_t
2835 mac_tx_bw_mode(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2836     uintptr_t fanout_hint, uint16_t flag, mblk_t **ret_mp)
2837 {
2838 	int			cnt, sz;
2839 	mblk_t			*tail;
2840 	mac_tx_cookie_t		cookie = NULL;
2841 	mac_srs_tx_t		*srs_tx = &mac_srs->srs_tx;
2842 
2843 	ASSERT(TX_BANDWIDTH_MODE(mac_srs));
2844 	ASSERT(mac_srs->srs_type & SRST_BW_CONTROL);
2845 	mutex_enter(&mac_srs->srs_lock);
2846 	if (mac_srs->srs_bw->mac_bw_limit == 0) {
2847 		/*
2848 		 * zero bandwidth, no traffic is sent: drop the packets,
2849 		 * or return the whole chain if the caller requests all
2850 		 * unsent packets back.
2851 		 */
2852 		if (flag & MAC_TX_NO_ENQUEUE) {
2853 			cookie = (mac_tx_cookie_t)mac_srs;
2854 			*ret_mp = mp_chain;
2855 		} else {
2856 			MAC_TX_SRS_DROP_MESSAGE(mac_srs, mp_chain, cookie);
2857 		}
2858 		mutex_exit(&mac_srs->srs_lock);
2859 		return (cookie);
2860 	} else if ((mac_srs->srs_first != NULL) ||
2861 	    (mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)) {
2862 		cookie = mac_tx_srs_enqueue(mac_srs, mp_chain, flag,
2863 		    fanout_hint, ret_mp);
2864 		mutex_exit(&mac_srs->srs_lock);
2865 		return (cookie);
2866 	}
2867 	MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
2868 	if (mac_srs->srs_bw->mac_bw_curr_time != lbolt) {
2869 		mac_srs->srs_bw->mac_bw_curr_time = lbolt;
2870 		mac_srs->srs_bw->mac_bw_used = 0;
2871 	} else if (mac_srs->srs_bw->mac_bw_used >
2872 	    mac_srs->srs_bw->mac_bw_limit) {
2873 		mac_srs->srs_bw->mac_bw_state |= SRS_BW_ENFORCED;
2874 		MAC_TX_SRS_ENQUEUE_CHAIN(mac_srs,
2875 		    mp_chain, tail, cnt, sz);
2876 		/*
2877 		 * Wakeup worker thread. Note that worker
2878 		 * thread has to be woken up so that it
2879 		 * can fire up the timer to be woken up
2880 		 * on the next tick. Also once
2881 		 * BW_ENFORCED is set, it can only be
2882 		 * reset by srs_worker thread. Until then
2883 		 * all packets will get queued up in SRS
2884 		 * and hence this this code path won't be
2885 		 * entered until BW_ENFORCED is reset.
2886 		 */
2887 		cv_signal(&mac_srs->srs_async);
2888 		mutex_exit(&mac_srs->srs_lock);
2889 		return (cookie);
2890 	}
2891 
2892 	mac_srs->srs_bw->mac_bw_used += sz;
2893 	mutex_exit(&mac_srs->srs_lock);
2894 
2895 	if (srs_tx->st_mode == SRS_TX_BW_FANOUT) {
2896 		mac_soft_ring_t *softring;
2897 		uint_t indx, hash;
2898 
2899 		hash = HASH_HINT(fanout_hint);
2900 		indx = COMPUTE_INDEX(hash,
2901 		    mac_srs->srs_oth_ring_count);
2902 		softring = mac_srs->srs_oth_soft_rings[indx];
2903 		return (mac_tx_soft_ring_process(softring, mp_chain, flag,
2904 		    ret_mp));
2905 	} else {
2906 		boolean_t		is_subflow;
2907 		mac_tx_stats_t		stats;
2908 
2909 		is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
2910 
2911 		mp_chain = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
2912 		    mp_chain, (is_subflow ? &stats : NULL));
2913 
2914 		if (mp_chain != NULL) {
2915 			mutex_enter(&mac_srs->srs_lock);
2916 			MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
2917 			if (mac_srs->srs_bw->mac_bw_used > sz)
2918 				mac_srs->srs_bw->mac_bw_used -= sz;
2919 			else
2920 				mac_srs->srs_bw->mac_bw_used = 0;
2921 			cookie = mac_tx_srs_enqueue(mac_srs, mp_chain, flag,
2922 			    fanout_hint, ret_mp);
2923 			mutex_exit(&mac_srs->srs_lock);
2924 			return (cookie);
2925 		}
2926 		if (is_subflow)
2927 			FLOW_TX_STATS_UPDATE(mac_srs->srs_flent, &stats);
2928 
2929 		return (NULL);
2930 	}
2931 }
2932 
2933 /* ARGSUSED */
2934 void
2935 mac_tx_srs_drain(mac_soft_ring_set_t *mac_srs, uint_t proc_type)
2936 {
2937 	mblk_t			*head, *tail;
2938 	size_t			sz;
2939 	uint32_t		tx_mode;
2940 	uint_t			saved_pkt_count;
2941 	boolean_t		is_subflow;
2942 	mac_tx_stats_t		stats;
2943 	mac_srs_tx_t		*srs_tx = &mac_srs->srs_tx;
2944 
2945 	saved_pkt_count = 0;
2946 	ASSERT(mutex_owned(&mac_srs->srs_lock));
2947 	ASSERT(!(mac_srs->srs_state & SRS_PROC));
2948 
2949 	mac_srs->srs_state |= SRS_PROC;
2950 
2951 	is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
2952 	tx_mode = srs_tx->st_mode;
2953 	if (tx_mode == SRS_TX_DEFAULT || tx_mode == SRS_TX_SERIALIZE) {
2954 		if (mac_srs->srs_first != NULL) {
2955 			head = mac_srs->srs_first;
2956 			tail = mac_srs->srs_last;
2957 			saved_pkt_count = mac_srs->srs_count;
2958 			mac_srs->srs_first = NULL;
2959 			mac_srs->srs_last = NULL;
2960 			mac_srs->srs_count = 0;
2961 			mutex_exit(&mac_srs->srs_lock);
2962 
2963 			head = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
2964 			    head, &stats);
2965 
2966 			mutex_enter(&mac_srs->srs_lock);
2967 			if (head != NULL) {
2968 				/* Device out of tx desc, set block */
2969 				if (head->b_next == NULL)
2970 					VERIFY(head == tail);
2971 				tail->b_next = mac_srs->srs_first;
2972 				mac_srs->srs_first = head;
2973 				mac_srs->srs_count +=
2974 				    (saved_pkt_count - stats.ts_opackets);
2975 				if (mac_srs->srs_last == NULL)
2976 					mac_srs->srs_last = tail;
2977 				MAC_TX_SRS_BLOCK(mac_srs, head);
2978 			} else {
2979 				srs_tx->st_woken_up = B_FALSE;
2980 				if (is_subflow) {
2981 					FLOW_TX_STATS_UPDATE(
2982 					    mac_srs->srs_flent, &stats);
2983 				}
2984 			}
2985 		}
2986 	} else if (tx_mode == SRS_TX_BW) {
2987 		/*
2988 		 * We are here because the timer fired and we have some data
2989 		 * to tranmit. Also mac_tx_srs_worker should have reset
2990 		 * SRS_BW_ENFORCED flag
2991 		 */
2992 		ASSERT(!(mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED));
2993 		head = tail = mac_srs->srs_first;
2994 		while (mac_srs->srs_first != NULL) {
2995 			tail = mac_srs->srs_first;
2996 			tail->b_prev = NULL;
2997 			mac_srs->srs_first = tail->b_next;
2998 			if (mac_srs->srs_first == NULL)
2999 				mac_srs->srs_last = NULL;
3000 			mac_srs->srs_count--;
3001 			sz = msgdsize(tail);
3002 			mac_srs->srs_size -= sz;
3003 			saved_pkt_count++;
3004 			MAC_TX_UPDATE_BW_INFO(mac_srs, sz);
3005 
3006 			if (mac_srs->srs_bw->mac_bw_used <
3007 			    mac_srs->srs_bw->mac_bw_limit)
3008 				continue;
3009 
3010 			if (mac_srs->srs_bw->mac_bw_curr_time != lbolt) {
3011 				mac_srs->srs_bw->mac_bw_curr_time = lbolt;
3012 				mac_srs->srs_bw->mac_bw_used = sz;
3013 				continue;
3014 			}
3015 			mac_srs->srs_bw->mac_bw_state |= SRS_BW_ENFORCED;
3016 			break;
3017 		}
3018 
3019 		ASSERT((head == NULL && tail == NULL) ||
3020 		    (head != NULL && tail != NULL));
3021 		if (tail != NULL) {
3022 			tail->b_next = NULL;
3023 			mutex_exit(&mac_srs->srs_lock);
3024 
3025 			head = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
3026 			    head, &stats);
3027 
3028 			mutex_enter(&mac_srs->srs_lock);
3029 			if (head != NULL) {
3030 				uint_t size_sent;
3031 
3032 				/* Device out of tx desc, set block */
3033 				if (head->b_next == NULL)
3034 					VERIFY(head == tail);
3035 				tail->b_next = mac_srs->srs_first;
3036 				mac_srs->srs_first = head;
3037 				mac_srs->srs_count +=
3038 				    (saved_pkt_count - stats.ts_opackets);
3039 				if (mac_srs->srs_last == NULL)
3040 					mac_srs->srs_last = tail;
3041 				size_sent = sz - stats.ts_obytes;
3042 				mac_srs->srs_size += size_sent;
3043 				mac_srs->srs_bw->mac_bw_sz += size_sent;
3044 				if (mac_srs->srs_bw->mac_bw_used > size_sent) {
3045 					mac_srs->srs_bw->mac_bw_used -=
3046 					    size_sent;
3047 				} else {
3048 					mac_srs->srs_bw->mac_bw_used = 0;
3049 				}
3050 				MAC_TX_SRS_BLOCK(mac_srs, head);
3051 			} else {
3052 				srs_tx->st_woken_up = B_FALSE;
3053 				if (is_subflow) {
3054 					FLOW_TX_STATS_UPDATE(
3055 					    mac_srs->srs_flent, &stats);
3056 				}
3057 			}
3058 		}
3059 	} else if (tx_mode == SRS_TX_BW_FANOUT) {
3060 		mblk_t *prev;
3061 		mac_soft_ring_t *softring;
3062 		uint64_t hint;
3063 
3064 		/*
3065 		 * We are here because the timer fired and we
3066 		 * have some quota to tranmit.
3067 		 */
3068 		prev = NULL;
3069 		head = tail = mac_srs->srs_first;
3070 		while (mac_srs->srs_first != NULL) {
3071 			tail = mac_srs->srs_first;
3072 			mac_srs->srs_first = tail->b_next;
3073 			if (mac_srs->srs_first == NULL)
3074 				mac_srs->srs_last = NULL;
3075 			mac_srs->srs_count--;
3076 			sz = msgdsize(tail);
3077 			mac_srs->srs_size -= sz;
3078 			mac_srs->srs_bw->mac_bw_used += sz;
3079 			if (prev == NULL)
3080 				hint = (ulong_t)tail->b_prev;
3081 			if (hint != (ulong_t)tail->b_prev) {
3082 				prev->b_next = NULL;
3083 				mutex_exit(&mac_srs->srs_lock);
3084 				TX_SRS_TO_SOFT_RING(mac_srs, head, hint);
3085 				head = tail;
3086 				hint = (ulong_t)tail->b_prev;
3087 				mutex_enter(&mac_srs->srs_lock);
3088 			}
3089 
3090 			prev = tail;
3091 			tail->b_prev = NULL;
3092 			if (mac_srs->srs_bw->mac_bw_used <
3093 			    mac_srs->srs_bw->mac_bw_limit)
3094 				continue;
3095 
3096 			if (mac_srs->srs_bw->mac_bw_curr_time != lbolt) {
3097 				mac_srs->srs_bw->mac_bw_curr_time = lbolt;
3098 				mac_srs->srs_bw->mac_bw_used = 0;
3099 				continue;
3100 			}
3101 			mac_srs->srs_bw->mac_bw_state |= SRS_BW_ENFORCED;
3102 			break;
3103 		}
3104 		ASSERT((head == NULL && tail == NULL) ||
3105 		    (head != NULL && tail != NULL));
3106 		if (tail != NULL) {
3107 			tail->b_next = NULL;
3108 			mutex_exit(&mac_srs->srs_lock);
3109 			TX_SRS_TO_SOFT_RING(mac_srs, head, hint);
3110 			mutex_enter(&mac_srs->srs_lock);
3111 		}
3112 	}
3113 	/*
3114 	 * SRS_TX_FANOUT case not considered here because packets
3115 	 * won't be queued in the SRS for this case. Packets will
3116 	 * be sent directly to soft rings underneath and if there
3117 	 * is any queueing at all, it would be in Tx side soft
3118 	 * rings.
3119 	 */
3120 
3121 	/*
3122 	 * When srs_count becomes 0, reset SRS_TX_HIWAT and
3123 	 * SRS_TX_WAKEUP_CLIENT and wakeup registered clients.
3124 	 */
3125 	if (mac_srs->srs_count == 0 && (mac_srs->srs_state &
3126 	    (SRS_TX_HIWAT | SRS_TX_WAKEUP_CLIENT | SRS_ENQUEUED))) {
3127 		mac_tx_notify_cb_t *mtnfp;
3128 		mac_cb_t *mcb;
3129 		mac_client_impl_t *mcip = mac_srs->srs_mcip;
3130 		boolean_t wakeup_required = B_FALSE;
3131 
3132 		if (mac_srs->srs_state &
3133 		    (SRS_TX_HIWAT|SRS_TX_WAKEUP_CLIENT)) {
3134 			wakeup_required = B_TRUE;
3135 		}
3136 		mac_srs->srs_state &= ~(SRS_TX_HIWAT |
3137 		    SRS_TX_WAKEUP_CLIENT | SRS_ENQUEUED);
3138 		mutex_exit(&mac_srs->srs_lock);
3139 		if (wakeup_required) {
3140 			/* Wakeup callback registered clients */
3141 			MAC_CALLBACK_WALKER_INC(&mcip->mci_tx_notify_cb_info);
3142 			for (mcb = mcip->mci_tx_notify_cb_list; mcb != NULL;
3143 			    mcb = mcb->mcb_nextp) {
3144 				mtnfp = (mac_tx_notify_cb_t *)mcb->mcb_objp;
3145 				mtnfp->mtnf_fn(mtnfp->mtnf_arg,
3146 				    (mac_tx_cookie_t)mac_srs);
3147 			}
3148 			MAC_CALLBACK_WALKER_DCR(&mcip->mci_tx_notify_cb_info,
3149 			    &mcip->mci_tx_notify_cb_list);
3150 			/*
3151 			 * If the client is not the primary MAC client, then we
3152 			 * need to send the notification to the clients upper
3153 			 * MAC, i.e. mci_upper_mip.
3154 			 */
3155 			mac_tx_notify(mcip->mci_upper_mip != NULL ?
3156 			    mcip->mci_upper_mip : mcip->mci_mip);
3157 		}
3158 		mutex_enter(&mac_srs->srs_lock);
3159 	}
3160 	mac_srs->srs_state &= ~SRS_PROC;
3161 }
3162 
3163 /*
3164  * Given a packet, get the flow_entry that identifies the flow
3165  * to which that packet belongs. The flow_entry will contain
3166  * the transmit function to be used to send the packet. If the
3167  * function returns NULL, the packet should be sent using the
3168  * underlying NIC.
3169  */
3170 static flow_entry_t *
3171 mac_tx_classify(mac_impl_t *mip, mblk_t *mp)
3172 {
3173 	flow_entry_t		*flent = NULL;
3174 	mac_client_impl_t	*mcip;
3175 	int	err;
3176 
3177 	/*
3178 	 * Do classification on the packet.
3179 	 */
3180 	err = mac_flow_lookup(mip->mi_flow_tab, mp, FLOW_OUTBOUND, &flent);
3181 	if (err != 0)
3182 		return (NULL);
3183 
3184 	/*
3185 	 * This flent might just be an additional one on the MAC client,
3186 	 * i.e. for classification purposes (different fdesc), however
3187 	 * the resources, SRS et. al., are in the mci_flent, so if
3188 	 * this isn't the mci_flent, we need to get it.
3189 	 */
3190 	if ((mcip = flent->fe_mcip) != NULL && mcip->mci_flent != flent) {
3191 		FLOW_REFRELE(flent);
3192 		flent = mcip->mci_flent;
3193 		FLOW_TRY_REFHOLD(flent, err);
3194 		if (err != 0)
3195 			return (NULL);
3196 	}
3197 
3198 	return (flent);
3199 }
3200 
3201 /*
3202  * This macro is only meant to be used by mac_tx_send().
3203  */
3204 #define	CHECK_VID_AND_ADD_TAG(mp) {			\
3205 	if (vid_check) {				\
3206 		int err = 0;				\
3207 							\
3208 		MAC_VID_CHECK(src_mcip, (mp), err);	\
3209 		if (err != 0) {				\
3210 			freemsg((mp));			\
3211 			(mp) = next;			\
3212 			oerrors++;			\
3213 			continue;			\
3214 		}					\
3215 	}						\
3216 	if (add_tag) {					\
3217 		(mp) = mac_add_vlan_tag((mp), 0, vid);	\
3218 		if ((mp) == NULL) {			\
3219 			(mp) = next;			\
3220 			oerrors++;			\
3221 			continue;			\
3222 		}					\
3223 	}						\
3224 }
3225 
3226 mblk_t *
3227 mac_tx_send(mac_client_handle_t mch, mac_ring_handle_t ring, mblk_t *mp_chain,
3228     mac_tx_stats_t *stats)
3229 {
3230 	mac_client_impl_t *src_mcip = (mac_client_impl_t *)mch;
3231 	mac_impl_t *mip = src_mcip->mci_mip;
3232 	uint_t obytes = 0, opackets = 0, oerrors = 0;
3233 	mblk_t *mp = NULL, *next;
3234 	boolean_t vid_check, add_tag;
3235 	uint16_t vid = 0;
3236 
3237 	if (mip->mi_nclients > 1) {
3238 		vid_check = MAC_VID_CHECK_NEEDED(src_mcip);
3239 		add_tag = MAC_TAG_NEEDED(src_mcip);
3240 		if (add_tag)
3241 			vid = mac_client_vid(mch);
3242 	} else {
3243 		ASSERT(mip->mi_nclients == 1);
3244 		vid_check = add_tag = B_FALSE;
3245 	}
3246 
3247 	/*
3248 	 * Fastpath: if there's only one client, and there's no
3249 	 * multicast listeners, we simply send the packet down to the
3250 	 * underlying NIC.
3251 	 */
3252 	if (mip->mi_nactiveclients == 1 && mip->mi_promisc_list == NULL)  {
3253 		DTRACE_PROBE2(fastpath,
3254 		    mac_client_impl_t *, src_mcip, mblk_t *, mp_chain);
3255 
3256 		mp = mp_chain;
3257 		while (mp != NULL) {
3258 			next = mp->b_next;
3259 			mp->b_next = NULL;
3260 			opackets++;
3261 			obytes += (mp->b_cont == NULL ? MBLKL(mp) :
3262 			    msgdsize(mp));
3263 
3264 			CHECK_VID_AND_ADD_TAG(mp);
3265 			MAC_TX(mip, ring, mp, src_mcip);
3266 
3267 			/*
3268 			 * If the driver is out of descriptors and does a
3269 			 * partial send it will return a chain of unsent
3270 			 * mblks. Adjust the accounting stats.
3271 			 */
3272 			if (mp != NULL) {
3273 				opackets--;
3274 				obytes -= msgdsize(mp);
3275 				mp->b_next = next;
3276 				break;
3277 			}
3278 			mp = next;
3279 		}
3280 		goto done;
3281 	}
3282 
3283 	/*
3284 	 * No fastpath, we either have more than one MAC client
3285 	 * defined on top of the same MAC, or one or more MAC
3286 	 * client promiscuous callbacks.
3287 	 */
3288 	DTRACE_PROBE3(slowpath, mac_client_impl_t *,
3289 	    src_mcip, int, mip->mi_nclients, mblk_t *, mp_chain);
3290 
3291 	mp = mp_chain;
3292 	while (mp != NULL) {
3293 		flow_entry_t *dst_flow_ent;
3294 		void *flow_cookie;
3295 		size_t	pkt_size;
3296 		mblk_t *mp1;
3297 
3298 		next = mp->b_next;
3299 		mp->b_next = NULL;
3300 		opackets++;
3301 		pkt_size = (mp->b_cont == NULL ? MBLKL(mp) : msgdsize(mp));
3302 		obytes += pkt_size;
3303 		CHECK_VID_AND_ADD_TAG(mp);
3304 
3305 		/*
3306 		 * Check if there are promiscuous mode callbacks defined.
3307 		 */
3308 		if (mip->mi_promisc_list != NULL)
3309 			mac_promisc_dispatch(mip, mp, src_mcip);
3310 
3311 		/*
3312 		 * Find the destination.
3313 		 */
3314 		dst_flow_ent = mac_tx_classify(mip, mp);
3315 
3316 		if (dst_flow_ent != NULL) {
3317 			size_t	hdrsize;
3318 			int	err = 0;
3319 
3320 			if (mip->mi_info.mi_nativemedia == DL_ETHER) {
3321 				struct ether_vlan_header *evhp =
3322 				    (struct ether_vlan_header *)mp->b_rptr;
3323 
3324 				if (ntohs(evhp->ether_tpid) == ETHERTYPE_VLAN)
3325 					hdrsize = sizeof (*evhp);
3326 				else
3327 					hdrsize = sizeof (struct ether_header);
3328 			} else {
3329 				mac_header_info_t	mhi;
3330 
3331 				err = mac_header_info((mac_handle_t)mip,
3332 				    mp, &mhi);
3333 				if (err == 0)
3334 					hdrsize = mhi.mhi_hdrsize;
3335 			}
3336 
3337 			/*
3338 			 * Got a matching flow. It's either another
3339 			 * MAC client, or a broadcast/multicast flow.
3340 			 * Make sure the packet size is within the
3341 			 * allowed size. If not drop the packet and
3342 			 * move to next packet.
3343 			 */
3344 			if (err != 0 ||
3345 			    (pkt_size - hdrsize) > mip->mi_sdu_max) {
3346 				oerrors++;
3347 				DTRACE_PROBE2(loopback__drop, size_t, pkt_size,
3348 				    mblk_t *, mp);
3349 				freemsg(mp);
3350 				mp = next;
3351 				FLOW_REFRELE(dst_flow_ent);
3352 				continue;
3353 			}
3354 			flow_cookie = mac_flow_get_client_cookie(dst_flow_ent);
3355 			if (flow_cookie != NULL) {
3356 				/*
3357 				 * The vnic_bcast_send function expects
3358 				 * to receive the sender MAC client
3359 				 * as value for arg2.
3360 				 */
3361 				mac_bcast_send(flow_cookie, src_mcip, mp,
3362 				    B_TRUE);
3363 			} else {
3364 				/*
3365 				 * loopback the packet to a
3366 				 * local MAC client. We force a context
3367 				 * switch if both source and destination
3368 				 * MAC clients are used by IP, i.e. bypass
3369 				 * is set.
3370 				 */
3371 				boolean_t do_switch;
3372 				mac_client_impl_t *dst_mcip =
3373 				    dst_flow_ent->fe_mcip;
3374 
3375 				do_switch = ((src_mcip->mci_state_flags &
3376 				    dst_mcip->mci_state_flags &
3377 				    MCIS_CLIENT_POLL_CAPABLE) != 0);
3378 
3379 				if ((mp1 = mac_fix_cksum(mp)) != NULL) {
3380 					(dst_flow_ent->fe_cb_fn)(
3381 					    dst_flow_ent->fe_cb_arg1,
3382 					    dst_flow_ent->fe_cb_arg2,
3383 					    mp1, do_switch);
3384 				}
3385 			}
3386 			FLOW_REFRELE(dst_flow_ent);
3387 		} else {
3388 			/*
3389 			 * Unknown destination, send via the underlying
3390 			 * NIC.
3391 			 */
3392 			MAC_TX(mip, ring, mp, src_mcip);
3393 			if (mp != NULL) {
3394 				/*
3395 				 * Adjust for the last packet that
3396 				 * could not be transmitted
3397 				 */
3398 				opackets--;
3399 				obytes -= pkt_size;
3400 				mp->b_next = next;
3401 				break;
3402 			}
3403 		}
3404 		mp = next;
3405 	}
3406 
3407 done:
3408 	src_mcip->mci_stat_obytes += obytes;
3409 	src_mcip->mci_stat_opackets += opackets;
3410 	src_mcip->mci_stat_oerrors += oerrors;
3411 
3412 	if (stats != NULL) {
3413 		stats->ts_opackets = opackets;
3414 		stats->ts_obytes = obytes;
3415 		stats->ts_oerrors = oerrors;
3416 	}
3417 	return (mp);
3418 }
3419 
3420 /*
3421  * mac_tx_srs_ring_present
3422  *
3423  * Returns whether the specified ring is part of the specified SRS.
3424  */
3425 boolean_t
3426 mac_tx_srs_ring_present(mac_soft_ring_set_t *srs, mac_ring_t *tx_ring)
3427 {
3428 	int i;
3429 	mac_soft_ring_t *soft_ring;
3430 
3431 	if (srs->srs_tx.st_arg2 == tx_ring)
3432 		return (B_TRUE);
3433 
3434 	for (i = 0; i < srs->srs_oth_ring_count; i++) {
3435 		soft_ring =  srs->srs_oth_soft_rings[i];
3436 		if (soft_ring->s_ring_tx_arg2 == tx_ring)
3437 			return (B_TRUE);
3438 	}
3439 
3440 	return (B_FALSE);
3441 }
3442 
3443 /*
3444  * mac_tx_srs_wakeup
3445  *
3446  * Called when Tx desc become available. Wakeup the appropriate worker
3447  * thread after resetting the SRS_TX_BLOCKED/S_RING_BLOCK bit in the
3448  * state field.
3449  */
3450 void
3451 mac_tx_srs_wakeup(mac_soft_ring_set_t *mac_srs, mac_ring_handle_t ring)
3452 {
3453 	int i;
3454 	mac_soft_ring_t *sringp;
3455 	mac_srs_tx_t *srs_tx = &mac_srs->srs_tx;
3456 
3457 	mutex_enter(&mac_srs->srs_lock);
3458 	if (TX_SINGLE_RING_MODE(mac_srs)) {
3459 		if (srs_tx->st_arg2 == ring &&
3460 		    mac_srs->srs_state & SRS_TX_BLOCKED) {
3461 			mac_srs->srs_state &= ~SRS_TX_BLOCKED;
3462 			srs_tx->st_unblocked_cnt++;
3463 			cv_signal(&mac_srs->srs_async);
3464 		}
3465 		/*
3466 		 * A wakeup can come before tx_srs_drain() could
3467 		 * grab srs lock and set SRS_TX_BLOCKED. So
3468 		 * always set woken_up flag when we come here.
3469 		 */
3470 		srs_tx->st_woken_up = B_TRUE;
3471 		mutex_exit(&mac_srs->srs_lock);
3472 		return;
3473 	}
3474 
3475 	/* If you are here, it is for FANOUT or BW_FANOUT case */
3476 	ASSERT(TX_MULTI_RING_MODE(mac_srs));
3477 	for (i = 0; i < mac_srs->srs_oth_ring_count; i++) {
3478 		sringp = mac_srs->srs_oth_soft_rings[i];
3479 		mutex_enter(&sringp->s_ring_lock);
3480 		if (sringp->s_ring_tx_arg2 == ring) {
3481 			if (sringp->s_ring_state & S_RING_BLOCK) {
3482 				sringp->s_ring_state &= ~S_RING_BLOCK;
3483 				sringp->s_ring_unblocked_cnt++;
3484 				cv_signal(&sringp->s_ring_async);
3485 			}
3486 			sringp->s_ring_tx_woken_up = B_TRUE;
3487 		}
3488 		mutex_exit(&sringp->s_ring_lock);
3489 	}
3490 	mutex_exit(&mac_srs->srs_lock);
3491 }
3492 
3493 /*
3494  * Once the driver is done draining, send a MAC_NOTE_TX notification to unleash
3495  * the blocked clients again.
3496  */
3497 void
3498 mac_tx_notify(mac_impl_t *mip)
3499 {
3500 	i_mac_notify(mip, MAC_NOTE_TX);
3501 }
3502 
3503 /*
3504  * RX SOFTRING RELATED FUNCTIONS
3505  *
3506  * These functions really belong in mac_soft_ring.c and here for
3507  * a short period.
3508  */
3509 
3510 #define	SOFT_RING_ENQUEUE_CHAIN(ringp, mp, tail, cnt, sz) {	       	\
3511 	/*								\
3512 	 * Enqueue our mblk chain.					\
3513 	 */								\
3514 	ASSERT(MUTEX_HELD(&(ringp)->s_ring_lock));			\
3515 									\
3516 	if ((ringp)->s_ring_last != NULL)				\
3517 		(ringp)->s_ring_last->b_next = (mp);			\
3518 	else								\
3519 		(ringp)->s_ring_first = (mp);				\
3520 	(ringp)->s_ring_last = (tail);					\
3521 	(ringp)->s_ring_count += (cnt);					\
3522 	ASSERT((ringp)->s_ring_count > 0);				\
3523 	if ((ringp)->s_ring_type & ST_RING_BW_CTL) {			\
3524 		(ringp)->s_ring_size += sz;				\
3525 	}								\
3526 }
3527 
3528 /*
3529  * Default entry point to deliver a packet chain to a MAC client.
3530  * If the MAC client has flows, do the classification with these
3531  * flows as well.
3532  */
3533 /* ARGSUSED */
3534 void
3535 mac_rx_deliver(void *arg1, mac_resource_handle_t mrh, mblk_t *mp_chain,
3536     mac_header_info_t *arg3)
3537 {
3538 	mac_client_impl_t *mcip = arg1;
3539 
3540 	if (mcip->mci_nvids == 1 &&
3541 	    !(mcip->mci_state_flags & MCIS_TAG_DISABLE)) {
3542 		/*
3543 		 * If the client has exactly one VID associated with it
3544 		 * and striping of VLAN header is not disabled,
3545 		 * remove the VLAN tag from the packet before
3546 		 * passing it on to the client's receive callback.
3547 		 * Note that this needs to be done after we dispatch
3548 		 * the packet to the promiscuous listeners of the
3549 		 * client, since they expect to see the whole
3550 		 * frame including the VLAN headers.
3551 		 */
3552 		mp_chain = mac_strip_vlan_tag_chain(mp_chain);
3553 	}
3554 
3555 	mcip->mci_rx_fn(mcip->mci_rx_arg, mrh, mp_chain, B_FALSE);
3556 }
3557 
3558 /*
3559  * mac_rx_soft_ring_process
3560  *
3561  * process a chain for a given soft ring. The number of packets queued
3562  * in the SRS and its associated soft rings (including this one) is
3563  * very small (tracked by srs_poll_pkt_cnt), then allow the entering
3564  * thread (interrupt or poll thread) to do inline processing. This
3565  * helps keep the latency down under low load.
3566  *
3567  * The proc and arg for each mblk is already stored in the mblk in
3568  * appropriate places.
3569  */
3570 /* ARGSUSED */
3571 void
3572 mac_rx_soft_ring_process(mac_client_impl_t *mcip, mac_soft_ring_t *ringp,
3573     mblk_t *mp_chain, mblk_t *tail, int cnt, size_t sz)
3574 {
3575 	mac_direct_rx_t		proc;
3576 	void			*arg1;
3577 	mac_resource_handle_t	arg2;
3578 	mac_soft_ring_set_t	*mac_srs = ringp->s_ring_set;
3579 
3580 	ASSERT(ringp != NULL);
3581 	ASSERT(mp_chain != NULL);
3582 	ASSERT(tail != NULL);
3583 	ASSERT(MUTEX_NOT_HELD(&ringp->s_ring_lock));
3584 
3585 	mutex_enter(&ringp->s_ring_lock);
3586 	ringp->s_ring_total_inpkt += cnt;
3587 	if ((mac_srs->srs_rx.sr_poll_pkt_cnt <= 1) &&
3588 	    !(ringp->s_ring_type & ST_RING_WORKER_ONLY)) {
3589 		/* If on processor or blanking on, then enqueue and return */
3590 		if (ringp->s_ring_state & S_RING_BLANK ||
3591 		    ringp->s_ring_state & S_RING_PROC) {
3592 			SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain, tail, cnt, sz);
3593 			mutex_exit(&ringp->s_ring_lock);
3594 			return;
3595 		}
3596 		proc = ringp->s_ring_rx_func;
3597 		arg1 = ringp->s_ring_rx_arg1;
3598 		arg2 = ringp->s_ring_rx_arg2;
3599 		/*
3600 		 * See if anything is already queued. If we are the
3601 		 * first packet, do inline processing else queue the
3602 		 * packet and do the drain.
3603 		 */
3604 		if (ringp->s_ring_first == NULL) {
3605 			/*
3606 			 * Fast-path, ok to process and nothing queued.
3607 			 */
3608 			ringp->s_ring_run = curthread;
3609 			ringp->s_ring_state |= (S_RING_PROC);
3610 
3611 			mutex_exit(&ringp->s_ring_lock);
3612 
3613 			/*
3614 			 * We are the chain of 1 packet so
3615 			 * go through this fast path.
3616 			 */
3617 			ASSERT(mp_chain->b_next == NULL);
3618 
3619 			(*proc)(arg1, arg2, mp_chain, NULL);
3620 
3621 			ASSERT(MUTEX_NOT_HELD(&ringp->s_ring_lock));
3622 			/*
3623 			 * If we have a soft ring set which is doing
3624 			 * bandwidth control, we need to decrement
3625 			 * srs_size and count so it the SRS can have a
3626 			 * accurate idea of what is the real data
3627 			 * queued between SRS and its soft rings. We
3628 			 * decrement the counters only when the packet
3629 			 * gets processed by both SRS and the soft ring.
3630 			 */
3631 			mutex_enter(&mac_srs->srs_lock);
3632 			MAC_UPDATE_SRS_COUNT_LOCKED(mac_srs, cnt);
3633 			MAC_UPDATE_SRS_SIZE_LOCKED(mac_srs, sz);
3634 			mutex_exit(&mac_srs->srs_lock);
3635 
3636 			mutex_enter(&ringp->s_ring_lock);
3637 			ringp->s_ring_run = NULL;
3638 			ringp->s_ring_state &= ~S_RING_PROC;
3639 			if (ringp->s_ring_state & S_RING_CLIENT_WAIT)
3640 				cv_signal(&ringp->s_ring_client_cv);
3641 
3642 			if ((ringp->s_ring_first == NULL) ||
3643 			    (ringp->s_ring_state & S_RING_BLANK)) {
3644 				/*
3645 				 * We processed inline our packet and
3646 				 * nothing new has arrived or our
3647 				 * receiver doesn't want to receive
3648 				 * any packets. We are done.
3649 				 */
3650 				mutex_exit(&ringp->s_ring_lock);
3651 				return;
3652 			}
3653 		} else {
3654 			SOFT_RING_ENQUEUE_CHAIN(ringp,
3655 			    mp_chain, tail, cnt, sz);
3656 		}
3657 
3658 		/*
3659 		 * We are here because either we couldn't do inline
3660 		 * processing (because something was already
3661 		 * queued), or we had a chain of more than one
3662 		 * packet, or something else arrived after we were
3663 		 * done with inline processing.
3664 		 */
3665 		ASSERT(MUTEX_HELD(&ringp->s_ring_lock));
3666 		ASSERT(ringp->s_ring_first != NULL);
3667 
3668 		ringp->s_ring_drain_func(ringp);
3669 		mutex_exit(&ringp->s_ring_lock);
3670 		return;
3671 	} else {
3672 		/* ST_RING_WORKER_ONLY case */
3673 		SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain, tail, cnt, sz);
3674 		mac_soft_ring_worker_wakeup(ringp);
3675 		mutex_exit(&ringp->s_ring_lock);
3676 	}
3677 }
3678 
3679 /*
3680  * TX SOFTRING RELATED FUNCTIONS
3681  *
3682  * These functions really belong in mac_soft_ring.c and here for
3683  * a short period.
3684  */
3685 
3686 #define	TX_SOFT_RING_ENQUEUE_CHAIN(ringp, mp, tail, cnt, sz) {	       	\
3687 	ASSERT(MUTEX_HELD(&ringp->s_ring_lock));			\
3688 	ringp->s_ring_state |= S_RING_ENQUEUED;				\
3689 	SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain, tail, cnt, sz);	\
3690 }
3691 
3692 /*
3693  * mac_tx_sring_queued
3694  *
3695  * When we are out of transmit descriptors and we already have a
3696  * queue that exceeds hiwat (or the client called us with
3697  * MAC_TX_NO_ENQUEUE or MAC_DROP_ON_NO_DESC flag), return the
3698  * soft ring pointer as the opaque cookie for the client enable
3699  * flow control.
3700  */
3701 static mac_tx_cookie_t
3702 mac_tx_sring_enqueue(mac_soft_ring_t *ringp, mblk_t *mp_chain, uint16_t flag,
3703     mblk_t **ret_mp)
3704 {
3705 	int cnt;
3706 	size_t sz;
3707 	mblk_t *tail;
3708 	mac_soft_ring_set_t *mac_srs = ringp->s_ring_set;
3709 	mac_tx_cookie_t cookie = NULL;
3710 	boolean_t wakeup_worker = B_TRUE;
3711 
3712 	ASSERT(MUTEX_HELD(&ringp->s_ring_lock));
3713 	MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
3714 	if (flag & MAC_DROP_ON_NO_DESC) {
3715 		mac_pkt_drop(NULL, NULL, mp_chain, B_FALSE);
3716 		/* increment freed stats */
3717 		ringp->s_ring_drops += cnt;
3718 		cookie = (mac_tx_cookie_t)ringp;
3719 	} else {
3720 		if (ringp->s_ring_first != NULL)
3721 			wakeup_worker = B_FALSE;
3722 
3723 		if (flag & MAC_TX_NO_ENQUEUE) {
3724 			/*
3725 			 * If QUEUED is not set, queue the packet
3726 			 * and let mac_tx_soft_ring_drain() set
3727 			 * the TX_BLOCKED bit for the reasons
3728 			 * explained above. Otherwise, return the
3729 			 * mblks.
3730 			 */
3731 			if (wakeup_worker) {
3732 				TX_SOFT_RING_ENQUEUE_CHAIN(ringp,
3733 				    mp_chain, tail, cnt, sz);
3734 			} else {
3735 				ringp->s_ring_state |= S_RING_WAKEUP_CLIENT;
3736 				cookie = (mac_tx_cookie_t)ringp;
3737 				*ret_mp = mp_chain;
3738 			}
3739 		} else {
3740 			boolean_t enqueue = B_TRUE;
3741 
3742 			if (ringp->s_ring_count > ringp->s_ring_tx_hiwat) {
3743 				/*
3744 				 * flow-controlled. Store ringp in cookie
3745 				 * so that it can be returned as
3746 				 * mac_tx_cookie_t to client
3747 				 */
3748 				ringp->s_ring_state |= S_RING_TX_HIWAT;
3749 				cookie = (mac_tx_cookie_t)ringp;
3750 				ringp->s_ring_hiwat_cnt++;
3751 				if (ringp->s_ring_count >
3752 				    ringp->s_ring_tx_max_q_cnt) {
3753 					/* increment freed stats */
3754 					ringp->s_ring_drops += cnt;
3755 					/*
3756 					 * b_prev may be set to the fanout hint
3757 					 * hence can't use freemsg directly
3758 					 */
3759 					mac_pkt_drop(NULL, NULL,
3760 					    mp_chain, B_FALSE);
3761 					DTRACE_PROBE1(tx_queued_hiwat,
3762 					    mac_soft_ring_t *, ringp);
3763 					enqueue = B_FALSE;
3764 				}
3765 			}
3766 			if (enqueue) {
3767 				TX_SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain,
3768 				    tail, cnt, sz);
3769 			}
3770 		}
3771 		if (wakeup_worker)
3772 			cv_signal(&ringp->s_ring_async);
3773 	}
3774 	return (cookie);
3775 }
3776 
3777 
3778 /*
3779  * mac_tx_soft_ring_process
3780  *
3781  * This routine is called when fanning out outgoing traffic among
3782  * multipe Tx rings.
3783  * Note that a soft ring is associated with a h/w Tx ring.
3784  */
3785 mac_tx_cookie_t
3786 mac_tx_soft_ring_process(mac_soft_ring_t *ringp, mblk_t *mp_chain,
3787     uint16_t flag, mblk_t **ret_mp)
3788 {
3789 	mac_soft_ring_set_t *mac_srs = ringp->s_ring_set;
3790 	int	cnt;
3791 	size_t	sz;
3792 	mblk_t	*tail;
3793 	mac_tx_cookie_t cookie = NULL;
3794 
3795 	ASSERT(ringp != NULL);
3796 	ASSERT(mp_chain != NULL);
3797 	ASSERT(MUTEX_NOT_HELD(&ringp->s_ring_lock));
3798 	/*
3799 	 * Only two modes can come here; either it can be
3800 	 * SRS_TX_BW_FANOUT or SRS_TX_FANOUT
3801 	 */
3802 	ASSERT(mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
3803 	    mac_srs->srs_tx.st_mode == SRS_TX_BW_FANOUT);
3804 
3805 	if (ringp->s_ring_type & ST_RING_WORKER_ONLY) {
3806 		/* Serialization mode */
3807 
3808 		mutex_enter(&ringp->s_ring_lock);
3809 		if (ringp->s_ring_count > ringp->s_ring_tx_hiwat) {
3810 			cookie = mac_tx_sring_enqueue(ringp, mp_chain,
3811 			    flag, ret_mp);
3812 			mutex_exit(&ringp->s_ring_lock);
3813 			return (cookie);
3814 		}
3815 		MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
3816 		TX_SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain, tail, cnt, sz);
3817 		if (ringp->s_ring_state & (S_RING_BLOCK | S_RING_PROC)) {
3818 			/*
3819 			 * If ring is blocked due to lack of Tx
3820 			 * descs, just return. Worker thread
3821 			 * will get scheduled when Tx desc's
3822 			 * become available.
3823 			 */
3824 			mutex_exit(&ringp->s_ring_lock);
3825 			return (cookie);
3826 		}
3827 		mac_soft_ring_worker_wakeup(ringp);
3828 		mutex_exit(&ringp->s_ring_lock);
3829 		return (cookie);
3830 	} else {
3831 		/* Default fanout mode */
3832 		/*
3833 		 * S_RING_BLOCKED is set when underlying NIC runs
3834 		 * out of Tx descs and messages start getting
3835 		 * queued. It won't get reset until
3836 		 * tx_srs_drain() completely drains out the
3837 		 * messages.
3838 		 */
3839 		boolean_t		is_subflow;
3840 		mac_tx_stats_t		stats;
3841 
3842 		if (ringp->s_ring_state & S_RING_ENQUEUED) {
3843 			/* Tx descs/resources not available */
3844 			mutex_enter(&ringp->s_ring_lock);
3845 			if (ringp->s_ring_state & S_RING_ENQUEUED) {
3846 				cookie = mac_tx_sring_enqueue(ringp, mp_chain,
3847 				    flag, ret_mp);
3848 				mutex_exit(&ringp->s_ring_lock);
3849 				return (cookie);
3850 			}
3851 			/*
3852 			 * While we were computing mblk count, the
3853 			 * flow control condition got relieved.
3854 			 * Continue with the transmission.
3855 			 */
3856 			mutex_exit(&ringp->s_ring_lock);
3857 		}
3858 		is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
3859 
3860 		mp_chain = mac_tx_send(ringp->s_ring_tx_arg1,
3861 		    ringp->s_ring_tx_arg2, mp_chain,
3862 		    (is_subflow ? &stats : NULL));
3863 
3864 		/*
3865 		 * Multiple threads could be here sending packets.
3866 		 * Under such conditions, it is not possible to
3867 		 * automically set S_RING_BLOCKED bit to indicate
3868 		 * out of tx desc condition. To atomically set
3869 		 * this, we queue the returned packet and do
3870 		 * the setting of S_RING_BLOCKED in
3871 		 * mac_tx_soft_ring_drain().
3872 		 */
3873 		if (mp_chain != NULL) {
3874 			mutex_enter(&ringp->s_ring_lock);
3875 			cookie =
3876 			    mac_tx_sring_enqueue(ringp, mp_chain, flag, ret_mp);
3877 			mutex_exit(&ringp->s_ring_lock);
3878 			return (cookie);
3879 		}
3880 		if (is_subflow) {
3881 			FLOW_TX_STATS_UPDATE(mac_srs->srs_flent, &stats);
3882 		}
3883 		return (NULL);
3884 	}
3885 }
3886