xref: /illumos-gate/usr/src/uts/common/io/mac/mac_sched.c (revision 5279807d7e1818eac6f90ac640b7a89cdb37522d)
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_srs)->srs_state & SRS_LATENCY_OPT) ||		\
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 (!(mac_srs->srs_state & SRS_PAUSE) &&
1368 				    srs_rx->sr_poll_pkt_cnt <=
1369 				    srs_rx->sr_lowat) {
1370 					srs_rx->sr_poll_again++;
1371 					goto check_again;
1372 				}
1373 				/*
1374 				 * We are already above low water mark
1375 				 * so stay in the polling mode but no
1376 				 * need to poll. Once we dip below
1377 				 * the polling threshold, the processing
1378 				 * thread (soft ring) will signal us
1379 				 * to poll again (MAC_UPDATE_SRS_COUNT)
1380 				 */
1381 				srs_rx->sr_poll_drain_no_poll++;
1382 				mac_srs->srs_state &= ~(SRS_PROC|SRS_GET_PKTS);
1383 				/*
1384 				 * In B/W control case, its possible
1385 				 * that the backlog built up due to
1386 				 * B/W limit being reached and packets
1387 				 * are queued only in SRS. In this case,
1388 				 * we should schedule worker thread
1389 				 * since no one else will wake us up.
1390 				 */
1391 				if ((mac_srs->srs_type & SRST_BW_CONTROL) &&
1392 				    (mac_srs->srs_tid == NULL)) {
1393 					mac_srs->srs_tid =
1394 					    timeout(mac_srs_fire, mac_srs, 1);
1395 					srs_rx->sr_poll_worker_wakeup++;
1396 				}
1397 			} else {
1398 				/*
1399 				 * Wakeup the worker thread for more processing.
1400 				 * We optimize for throughput in this case.
1401 				 */
1402 				mac_srs->srs_state &= ~(SRS_PROC|SRS_GET_PKTS);
1403 				MAC_SRS_WORKER_WAKEUP(mac_srs);
1404 				srs_rx->sr_poll_sig_worker++;
1405 			}
1406 		} else if ((mac_srs->srs_first == NULL) &&
1407 		    !(mac_srs->srs_state & SRS_WORKER)) {
1408 			/*
1409 			 * There is nothing queued in SRS and
1410 			 * no worker thread running. Plus we
1411 			 * didn't get anything from the H/W
1412 			 * as well (head == NULL);
1413 			 */
1414 			ASSERT(head == NULL);
1415 			mac_srs->srs_state &=
1416 			    ~(SRS_PROC|SRS_GET_PKTS);
1417 
1418 			/*
1419 			 * If we have a packets in soft ring, don't allow
1420 			 * more packets to come into this SRS by keeping the
1421 			 * interrupts off but not polling the H/W. The
1422 			 * poll thread will get signaled as soon as
1423 			 * srs_poll_pkt_cnt dips below poll threshold.
1424 			 */
1425 			if (srs_rx->sr_poll_pkt_cnt == 0) {
1426 				srs_rx->sr_poll_intr_enable++;
1427 				MAC_SRS_POLLING_OFF(mac_srs);
1428 			} else {
1429 				/*
1430 				 * We know nothing is queued in SRS
1431 				 * since we are here after checking
1432 				 * srs_first is NULL. The backlog
1433 				 * is entirely due to packets queued
1434 				 * in Soft ring which will wake us up
1435 				 * and get the interface out of polling
1436 				 * mode once the backlog dips below
1437 				 * sr_poll_thres.
1438 				 */
1439 				srs_rx->sr_poll_no_poll++;
1440 			}
1441 		} else {
1442 			/*
1443 			 * Worker thread is already running.
1444 			 * Nothing much to do. If the polling
1445 			 * was enabled, worker thread will deal
1446 			 * with that.
1447 			 */
1448 			mac_srs->srs_state &= ~SRS_GET_PKTS;
1449 			srs_rx->sr_poll_goto_sleep++;
1450 		}
1451 	}
1452 done:
1453 	mac_srs->srs_state |= SRS_POLL_THR_QUIESCED;
1454 	cv_signal(&mac_srs->srs_async);
1455 	/*
1456 	 * If this is a temporary quiesce then wait for the restart signal
1457 	 * from the srs worker. Then clear the flags and signal the srs worker
1458 	 * to ensure a positive handshake and go back to start.
1459 	 */
1460 	while (!(mac_srs->srs_state & (SRS_CONDEMNED | SRS_POLL_THR_RESTART)))
1461 		cv_wait(async, lock);
1462 	if (mac_srs->srs_state & SRS_POLL_THR_RESTART) {
1463 		ASSERT(!(mac_srs->srs_state & SRS_CONDEMNED));
1464 		mac_srs->srs_state &=
1465 		    ~(SRS_POLL_THR_QUIESCED | SRS_POLL_THR_RESTART);
1466 		cv_signal(&mac_srs->srs_async);
1467 		goto start;
1468 	} else {
1469 		mac_srs->srs_state |= SRS_POLL_THR_EXITED;
1470 		cv_signal(&mac_srs->srs_async);
1471 		CALLB_CPR_EXIT(&cprinfo);
1472 		thread_exit();
1473 	}
1474 }
1475 
1476 /*
1477  * mac_srs_pick_chain
1478  *
1479  * In Bandwidth control case, checks how many packets can be processed
1480  * and return them in a sub chain.
1481  */
1482 static mblk_t *
1483 mac_srs_pick_chain(mac_soft_ring_set_t *mac_srs, mblk_t **chain_tail,
1484     size_t *chain_sz, int *chain_cnt)
1485 {
1486 	mblk_t 			*head = NULL;
1487 	mblk_t 			*tail = NULL;
1488 	size_t			sz;
1489 	size_t 			tsz = 0;
1490 	int			cnt = 0;
1491 	mblk_t 			*mp;
1492 
1493 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
1494 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1495 	if (((mac_srs->srs_bw->mac_bw_used + mac_srs->srs_size) <=
1496 	    mac_srs->srs_bw->mac_bw_limit) ||
1497 	    (mac_srs->srs_bw->mac_bw_limit == 0)) {
1498 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1499 		head = mac_srs->srs_first;
1500 		mac_srs->srs_first = NULL;
1501 		*chain_tail = mac_srs->srs_last;
1502 		mac_srs->srs_last = NULL;
1503 		*chain_sz = mac_srs->srs_size;
1504 		*chain_cnt = mac_srs->srs_count;
1505 		mac_srs->srs_count = 0;
1506 		mac_srs->srs_size = 0;
1507 		return (head);
1508 	}
1509 
1510 	/*
1511 	 * Can't clear the entire backlog.
1512 	 * Need to find how many packets to pick
1513 	 */
1514 	ASSERT(MUTEX_HELD(&mac_srs->srs_bw->mac_bw_lock));
1515 	while ((mp = mac_srs->srs_first) != NULL) {
1516 		sz = msgdsize(mp);
1517 		if ((tsz + sz + mac_srs->srs_bw->mac_bw_used) >
1518 		    mac_srs->srs_bw->mac_bw_limit) {
1519 			if (!(mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED))
1520 				mac_srs->srs_bw->mac_bw_state |=
1521 				    SRS_BW_ENFORCED;
1522 			break;
1523 		}
1524 
1525 		/*
1526 		 * The _size & cnt is  decremented from the softrings
1527 		 * when they send up the packet for polling to work
1528 		 * properly.
1529 		 */
1530 		tsz += sz;
1531 		cnt++;
1532 		mac_srs->srs_count--;
1533 		mac_srs->srs_size -= sz;
1534 		if (tail != NULL)
1535 			tail->b_next = mp;
1536 		else
1537 			head = mp;
1538 		tail = mp;
1539 		mac_srs->srs_first = mac_srs->srs_first->b_next;
1540 	}
1541 	mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1542 	if (mac_srs->srs_first == NULL)
1543 		mac_srs->srs_last = NULL;
1544 
1545 	if (tail != NULL)
1546 		tail->b_next = NULL;
1547 	*chain_tail = tail;
1548 	*chain_cnt = cnt;
1549 	*chain_sz = tsz;
1550 
1551 	return (head);
1552 }
1553 
1554 /*
1555  * mac_rx_srs_drain
1556  *
1557  * The SRS drain routine. Gets to run to clear the queue. Any thread
1558  * (worker, interrupt, poll) can call this based on processing model.
1559  * The first thing we do is disable interrupts if possible and then
1560  * drain the queue. we also try to poll the underlying hardware if
1561  * there is a dedicated hardware Rx ring assigned to this SRS.
1562  *
1563  * There is a equivalent drain routine in bandwidth control mode
1564  * mac_rx_srs_drain_bw. There is some code duplication between the two
1565  * routines but they are highly performance sensitive and are easier
1566  * to read/debug if they stay separate. Any code changes here might
1567  * also apply to mac_rx_srs_drain_bw as well.
1568  */
1569 void
1570 mac_rx_srs_drain(mac_soft_ring_set_t *mac_srs, uint_t proc_type)
1571 {
1572 	mblk_t 			*head;
1573 	mblk_t			*tail;
1574 	timeout_id_t 		tid;
1575 	int			cnt = 0;
1576 	mac_client_impl_t	*mcip = mac_srs->srs_mcip;
1577 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
1578 
1579 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
1580 	ASSERT(!(mac_srs->srs_type & SRST_BW_CONTROL));
1581 
1582 	/* If we are blanked i.e. can't do upcalls, then we are done */
1583 	if (mac_srs->srs_state & (SRS_BLANK | SRS_PAUSE)) {
1584 		ASSERT((mac_srs->srs_type & SRST_NO_SOFT_RINGS) ||
1585 		    (mac_srs->srs_state & SRS_PAUSE));
1586 		goto out;
1587 	}
1588 
1589 	if (mac_srs->srs_first == NULL)
1590 		goto out;
1591 
1592 	if (!(mac_srs->srs_state & SRS_LATENCY_OPT) &&
1593 	    (srs_rx->sr_poll_pkt_cnt <= srs_rx->sr_lowat)) {
1594 		/*
1595 		 * In the normal case, the SRS worker thread does no
1596 		 * work and we wait for a backlog to build up before
1597 		 * we switch into polling mode. In case we are
1598 		 * optimizing for throughput, we use the worker thread
1599 		 * as well. The goal is to let worker thread process
1600 		 * the queue and poll thread to feed packets into
1601 		 * the queue. As such, we should signal the poll
1602 		 * thread to try and get more packets.
1603 		 *
1604 		 * We could have pulled this check in the POLL_RING
1605 		 * macro itself but keeping it explicit here makes
1606 		 * the architecture more human understandable.
1607 		 */
1608 		MAC_SRS_POLL_RING(mac_srs);
1609 	}
1610 
1611 again:
1612 	head = mac_srs->srs_first;
1613 	mac_srs->srs_first = NULL;
1614 	tail = mac_srs->srs_last;
1615 	mac_srs->srs_last = NULL;
1616 	cnt = mac_srs->srs_count;
1617 	mac_srs->srs_count = 0;
1618 
1619 	ASSERT(head != NULL);
1620 	ASSERT(tail != NULL);
1621 
1622 	if ((tid = mac_srs->srs_tid) != 0)
1623 		mac_srs->srs_tid = 0;
1624 
1625 	mac_srs->srs_state |= (SRS_PROC|proc_type);
1626 
1627 
1628 	/*
1629 	 * mcip is NULL for broadcast and multicast flows. The promisc
1630 	 * callbacks for broadcast and multicast packets are delivered from
1631 	 * mac_rx() and we don't need to worry about that case in this path
1632 	 */
1633 	if (mcip != NULL && mcip->mci_promisc_list != NULL) {
1634 		mutex_exit(&mac_srs->srs_lock);
1635 		mac_promisc_client_dispatch(mcip, head);
1636 		mutex_enter(&mac_srs->srs_lock);
1637 	}
1638 
1639 	/*
1640 	 * Check if SRS itself is doing the processing
1641 	 * This direct path does not apply when subflows are present. In this
1642 	 * case, packets need to be dispatched to a soft ring according to the
1643 	 * flow's bandwidth and other resources contraints.
1644 	 */
1645 	if (mac_srs->srs_type & SRST_NO_SOFT_RINGS) {
1646 		mac_direct_rx_t		proc;
1647 		void			*arg1;
1648 		mac_resource_handle_t	arg2;
1649 
1650 		/*
1651 		 * This is the case when a Rx is directly
1652 		 * assigned and we have a fully classified
1653 		 * protocol chain. We can deal with it in
1654 		 * one shot.
1655 		 */
1656 		proc = srs_rx->sr_func;
1657 		arg1 = srs_rx->sr_arg1;
1658 		arg2 = srs_rx->sr_arg2;
1659 
1660 		mac_srs->srs_state |= SRS_CLIENT_PROC;
1661 		mutex_exit(&mac_srs->srs_lock);
1662 		if (tid != 0) {
1663 			(void) untimeout(tid);
1664 			tid = 0;
1665 		}
1666 
1667 		proc(arg1, arg2, head, NULL);
1668 		/*
1669 		 * Decrement the size and count here itelf
1670 		 * since the packet has been processed.
1671 		 */
1672 		mutex_enter(&mac_srs->srs_lock);
1673 		MAC_UPDATE_SRS_COUNT_LOCKED(mac_srs, cnt);
1674 		if (mac_srs->srs_state & SRS_CLIENT_WAIT)
1675 			cv_signal(&mac_srs->srs_client_cv);
1676 		mac_srs->srs_state &= ~SRS_CLIENT_PROC;
1677 	} else {
1678 		/* Some kind of softrings based fanout is required */
1679 		mutex_exit(&mac_srs->srs_lock);
1680 		if (tid != 0) {
1681 			(void) untimeout(tid);
1682 			tid = 0;
1683 		}
1684 
1685 		/*
1686 		 * Since the fanout routines can deal with chains,
1687 		 * shoot the entire chain up.
1688 		 */
1689 		if (mac_srs->srs_type & SRST_FANOUT_SRC_IP)
1690 			mac_rx_srs_fanout(mac_srs, head);
1691 		else
1692 			mac_rx_srs_proto_fanout(mac_srs, head);
1693 		mutex_enter(&mac_srs->srs_lock);
1694 	}
1695 
1696 	if (!(mac_srs->srs_state & (SRS_LATENCY_OPT|SRS_BLANK|SRS_PAUSE))) {
1697 		/*
1698 		 * In case we are optimizing for throughput, we
1699 		 * should try and keep the worker thread running
1700 		 * as much as possible. Send the poll thread down
1701 		 * to check one more time if something else
1702 		 * arrived. In the meanwhile, if poll thread had
1703 		 * collected something due to earlier signal,
1704 		 * process it now.
1705 		 */
1706 		if (srs_rx->sr_poll_pkt_cnt <= srs_rx->sr_lowat) {
1707 			srs_rx->sr_drain_poll_sig++;
1708 			MAC_SRS_POLL_RING(mac_srs);
1709 		}
1710 		if (mac_srs->srs_first != NULL) {
1711 			srs_rx->sr_drain_again++;
1712 			goto again;
1713 		}
1714 	}
1715 
1716 out:
1717 	if (mac_srs->srs_state & SRS_GET_PKTS) {
1718 		/*
1719 		 * Poll thread is already running. Leave the
1720 		 * SRS_RPOC set and hand over the control to
1721 		 * poll thread.
1722 		 */
1723 		mac_srs->srs_state &= ~proc_type;
1724 		srs_rx->sr_drain_poll_running++;
1725 		return;
1726 	}
1727 
1728 	/*
1729 	 * Even if there are no packets queued in SRS, we
1730 	 * need to make sure that the shared counter is
1731 	 * clear and any associated softrings have cleared
1732 	 * all the backlog. Otherwise, leave the interface
1733 	 * in polling mode and the poll thread will get
1734 	 * signalled once the count goes down to zero.
1735 	 *
1736 	 * If someone is already draining the queue (SRS_PROC is
1737 	 * set) when the srs_poll_pkt_cnt goes down to zero,
1738 	 * then it means that drain is already running and we
1739 	 * will turn off polling at that time if there is
1740 	 * no backlog.
1741 	 *
1742 	 * As long as there are packets queued either
1743 	 * in soft ring set or its soft rings, we will leave
1744 	 * the interface in polling mode (even if the drain
1745 	 * was done being the interrupt thread). We signal
1746 	 * the poll thread as well if we have dipped below
1747 	 * low water mark.
1748 	 *
1749 	 * NOTE: We can't use the MAC_SRS_POLLING_ON macro
1750 	 * since that turn polling on only for worker thread.
1751 	 * Its not worth turning polling on for interrupt
1752 	 * thread (since NIC will not issue another interrupt)
1753 	 * unless a backlog builds up.
1754 	 */
1755 	if ((srs_rx->sr_poll_pkt_cnt > 0) &&
1756 	    (mac_srs->srs_state & SRS_POLLING_CAPAB)) {
1757 		mac_srs->srs_state &= ~(SRS_PROC|proc_type);
1758 		srs_rx->sr_drain_keep_polling++;
1759 		MAC_SRS_POLLING_ON(mac_srs);
1760 		if (srs_rx->sr_poll_pkt_cnt <= srs_rx->sr_lowat)
1761 			MAC_SRS_POLL_RING(mac_srs);
1762 		return;
1763 	}
1764 
1765 	/* Nothing else to do. Get out of poll mode */
1766 	MAC_SRS_POLLING_OFF(mac_srs);
1767 	mac_srs->srs_state &= ~(SRS_PROC|proc_type);
1768 	srs_rx->sr_drain_finish_intr++;
1769 }
1770 
1771 /*
1772  * mac_rx_srs_drain_bw
1773  *
1774  * The SRS BW drain routine. Gets to run to clear the queue. Any thread
1775  * (worker, interrupt, poll) can call this based on processing model.
1776  * The first thing we do is disable interrupts if possible and then
1777  * drain the queue. we also try to poll the underlying hardware if
1778  * there is a dedicated hardware Rx ring assigned to this SRS.
1779  *
1780  * There is a equivalent drain routine in non bandwidth control mode
1781  * mac_rx_srs_drain. There is some code duplication between the two
1782  * routines but they are highly performance sensitive and are easier
1783  * to read/debug if they stay separate. Any code changes here might
1784  * also apply to mac_rx_srs_drain as well.
1785  */
1786 void
1787 mac_rx_srs_drain_bw(mac_soft_ring_set_t *mac_srs, uint_t proc_type)
1788 {
1789 	mblk_t 			*head;
1790 	mblk_t			*tail;
1791 	timeout_id_t 		tid;
1792 	size_t			sz = 0;
1793 	int			cnt = 0;
1794 	mac_client_impl_t	*mcip = mac_srs->srs_mcip;
1795 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
1796 
1797 	ASSERT(MUTEX_HELD(&mac_srs->srs_lock));
1798 	ASSERT(mac_srs->srs_type & SRST_BW_CONTROL);
1799 again:
1800 	/* Check if we are doing B/W control */
1801 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1802 	if (mac_srs->srs_bw->mac_bw_curr_time != lbolt) {
1803 		mac_srs->srs_bw->mac_bw_curr_time = lbolt;
1804 		mac_srs->srs_bw->mac_bw_used = 0;
1805 		if (mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)
1806 			mac_srs->srs_bw->mac_bw_state &= ~SRS_BW_ENFORCED;
1807 	} else if (mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED) {
1808 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1809 		goto done;
1810 	} else if (mac_srs->srs_bw->mac_bw_used >
1811 	    mac_srs->srs_bw->mac_bw_limit) {
1812 		mac_srs->srs_bw->mac_bw_state |= SRS_BW_ENFORCED;
1813 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1814 		goto done;
1815 	}
1816 	mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1817 
1818 	/* If we are blanked i.e. can't do upcalls, then we are done */
1819 	if (mac_srs->srs_state & (SRS_BLANK | SRS_PAUSE)) {
1820 		ASSERT((mac_srs->srs_type & SRST_NO_SOFT_RINGS) ||
1821 		    (mac_srs->srs_state & SRS_PAUSE));
1822 		goto done;
1823 	}
1824 
1825 	sz = 0;
1826 	cnt = 0;
1827 	if ((head = mac_srs_pick_chain(mac_srs, &tail, &sz, &cnt)) == NULL) {
1828 		/*
1829 		 * We couldn't pick up a single packet.
1830 		 */
1831 		mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1832 		if ((mac_srs->srs_bw->mac_bw_used == 0) &&
1833 		    (mac_srs->srs_size != 0) &&
1834 		    !(mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)) {
1835 			/*
1836 			 * Seems like configured B/W doesn't
1837 			 * even allow processing of 1 packet
1838 			 * per tick.
1839 			 *
1840 			 * XXX: raise the limit to processing
1841 			 * at least 1 packet per tick.
1842 			 */
1843 			mac_srs->srs_bw->mac_bw_limit +=
1844 			    mac_srs->srs_bw->mac_bw_limit;
1845 			mac_srs->srs_bw->mac_bw_drop_threshold +=
1846 			    mac_srs->srs_bw->mac_bw_drop_threshold;
1847 			cmn_err(CE_NOTE, "mac_rx_srs_drain: srs(%p) "
1848 			    "raised B/W limit to %d since not even a "
1849 			    "single packet can be processed per "
1850 			    "tick %d\n", (void *)mac_srs,
1851 			    (int)mac_srs->srs_bw->mac_bw_limit,
1852 			    (int)msgdsize(mac_srs->srs_first));
1853 		}
1854 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1855 		goto done;
1856 	}
1857 
1858 	ASSERT(head != NULL);
1859 	ASSERT(tail != NULL);
1860 
1861 	/* zero bandwidth: drop all and return to interrupt mode */
1862 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1863 	if (mac_srs->srs_bw->mac_bw_limit == 0) {
1864 		srs_rx->sr_drop_count += cnt;
1865 		ASSERT(mac_srs->srs_bw->mac_bw_sz >= sz);
1866 		mac_srs->srs_bw->mac_bw_sz -= sz;
1867 		mac_srs->srs_bw->mac_bw_drop_bytes += sz;
1868 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1869 		mac_pkt_drop(NULL, NULL, head, B_FALSE);
1870 		goto leave_poll;
1871 	} else {
1872 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1873 	}
1874 
1875 	if ((tid = mac_srs->srs_tid) != 0)
1876 		mac_srs->srs_tid = 0;
1877 
1878 	mac_srs->srs_state |= (SRS_PROC|proc_type);
1879 	MAC_SRS_WORKER_POLLING_ON(mac_srs);
1880 
1881 	/*
1882 	 * mcip is NULL for broadcast and multicast flows. The promisc
1883 	 * callbacks for broadcast and multicast packets are delivered from
1884 	 * mac_rx() and we don't need to worry about that case in this path
1885 	 */
1886 	if (mcip != NULL && mcip->mci_promisc_list != NULL) {
1887 		mutex_exit(&mac_srs->srs_lock);
1888 		mac_promisc_client_dispatch(mcip, head);
1889 		mutex_enter(&mac_srs->srs_lock);
1890 	}
1891 
1892 	/*
1893 	 * Check if SRS itself is doing the processing
1894 	 * This direct path does not apply when subflows are present. In this
1895 	 * case, packets need to be dispatched to a soft ring according to the
1896 	 * flow's bandwidth and other resources contraints.
1897 	 */
1898 	if (mac_srs->srs_type & SRST_NO_SOFT_RINGS) {
1899 		mac_direct_rx_t		proc;
1900 		void			*arg1;
1901 		mac_resource_handle_t	arg2;
1902 
1903 		/*
1904 		 * This is the case when a Rx is directly
1905 		 * assigned and we have a fully classified
1906 		 * protocol chain. We can deal with it in
1907 		 * one shot.
1908 		 */
1909 		proc = srs_rx->sr_func;
1910 		arg1 = srs_rx->sr_arg1;
1911 		arg2 = srs_rx->sr_arg2;
1912 
1913 		mac_srs->srs_state |= SRS_CLIENT_PROC;
1914 		mutex_exit(&mac_srs->srs_lock);
1915 		if (tid != 0) {
1916 			(void) untimeout(tid);
1917 			tid = 0;
1918 		}
1919 
1920 		proc(arg1, arg2, head, NULL);
1921 		/*
1922 		 * Decrement the size and count here itelf
1923 		 * since the packet has been processed.
1924 		 */
1925 		mutex_enter(&mac_srs->srs_lock);
1926 		MAC_UPDATE_SRS_COUNT_LOCKED(mac_srs, cnt);
1927 		MAC_UPDATE_SRS_SIZE_LOCKED(mac_srs, sz);
1928 
1929 		if (mac_srs->srs_state & SRS_CLIENT_WAIT)
1930 			cv_signal(&mac_srs->srs_client_cv);
1931 		mac_srs->srs_state &= ~SRS_CLIENT_PROC;
1932 	} else {
1933 		/* Some kind of softrings based fanout is required */
1934 		mutex_exit(&mac_srs->srs_lock);
1935 		if (tid != 0) {
1936 			(void) untimeout(tid);
1937 			tid = 0;
1938 		}
1939 
1940 		/*
1941 		 * Since the fanout routines can deal with chains,
1942 		 * shoot the entire chain up.
1943 		 */
1944 		if (mac_srs->srs_type & SRST_FANOUT_SRC_IP)
1945 			mac_rx_srs_fanout(mac_srs, head);
1946 		else
1947 			mac_rx_srs_proto_fanout(mac_srs, head);
1948 		mutex_enter(&mac_srs->srs_lock);
1949 	}
1950 
1951 	/*
1952 	 * Send the poll thread to pick up any packets arrived
1953 	 * so far. This also serves as the last check in case
1954 	 * nothing else is queued in the SRS. The poll thread
1955 	 * is signalled only in the case the drain was done
1956 	 * by the worker thread and SRS_WORKER is set. The
1957 	 * worker thread can run in parallel as long as the
1958 	 * SRS_WORKER flag is set. We we have nothing else to
1959 	 * process, we can exit while leaving SRS_PROC set
1960 	 * which gives the poll thread control to process and
1961 	 * cleanup once it returns from the NIC.
1962 	 *
1963 	 * If we have nothing else to process, we need to
1964 	 * ensure that we keep holding the srs_lock till
1965 	 * all the checks below are done and control is
1966 	 * handed to the poll thread if it was running.
1967 	 */
1968 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
1969 	if (!(mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)) {
1970 		if (mac_srs->srs_first != NULL) {
1971 			if (proc_type == SRS_WORKER) {
1972 				mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1973 				if (srs_rx->sr_poll_pkt_cnt <=
1974 				    srs_rx->sr_lowat)
1975 					MAC_SRS_POLL_RING(mac_srs);
1976 				goto again;
1977 			} else {
1978 				cv_signal(&mac_srs->srs_async);
1979 			}
1980 		}
1981 	}
1982 	mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
1983 
1984 done:
1985 
1986 	if (mac_srs->srs_state & SRS_GET_PKTS) {
1987 		/*
1988 		 * Poll thread is already running. Leave the
1989 		 * SRS_RPOC set and hand over the control to
1990 		 * poll thread.
1991 		 */
1992 		mac_srs->srs_state &= ~proc_type;
1993 		return;
1994 	}
1995 
1996 	/*
1997 	 * If we can't process packets because we have exceeded
1998 	 * B/W limit for this tick, just set the timeout
1999 	 * and leave.
2000 	 *
2001 	 * Even if there are no packets queued in SRS, we
2002 	 * need to make sure that the shared counter is
2003 	 * clear and any associated softrings have cleared
2004 	 * all the backlog. Otherwise, leave the interface
2005 	 * in polling mode and the poll thread will get
2006 	 * signalled once the count goes down to zero.
2007 	 *
2008 	 * If someone is already draining the queue (SRS_PROC is
2009 	 * set) when the srs_poll_pkt_cnt goes down to zero,
2010 	 * then it means that drain is already running and we
2011 	 * will turn off polling at that time if there is
2012 	 * no backlog. As long as there are packets queued either
2013 	 * is soft ring set or its soft rings, we will leave
2014 	 * the interface in polling mode.
2015 	 */
2016 	mutex_enter(&mac_srs->srs_bw->mac_bw_lock);
2017 	if ((mac_srs->srs_state & SRS_POLLING_CAPAB) &&
2018 	    ((mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED) ||
2019 	    (srs_rx->sr_poll_pkt_cnt > 0))) {
2020 		MAC_SRS_POLLING_ON(mac_srs);
2021 		mac_srs->srs_state &= ~(SRS_PROC|proc_type);
2022 		if ((mac_srs->srs_first != NULL) &&
2023 		    (mac_srs->srs_tid == NULL))
2024 			mac_srs->srs_tid = timeout(mac_srs_fire,
2025 			    mac_srs, 1);
2026 		mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
2027 		return;
2028 	}
2029 	mutex_exit(&mac_srs->srs_bw->mac_bw_lock);
2030 
2031 leave_poll:
2032 
2033 	/* Nothing else to do. Get out of poll mode */
2034 	MAC_SRS_POLLING_OFF(mac_srs);
2035 	mac_srs->srs_state &= ~(SRS_PROC|proc_type);
2036 }
2037 
2038 /*
2039  * mac_srs_worker
2040  *
2041  * The SRS worker routine. Drains the queue when no one else is
2042  * processing it.
2043  */
2044 void
2045 mac_srs_worker(mac_soft_ring_set_t *mac_srs)
2046 {
2047 	kmutex_t 		*lock = &mac_srs->srs_lock;
2048 	kcondvar_t 		*async = &mac_srs->srs_async;
2049 	callb_cpr_t		cprinfo;
2050 	boolean_t		bw_ctl_flag;
2051 
2052 	CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, "srs_worker");
2053 	mutex_enter(lock);
2054 
2055 start:
2056 	for (;;) {
2057 		bw_ctl_flag = B_FALSE;
2058 		if (mac_srs->srs_type & SRST_BW_CONTROL) {
2059 			MAC_SRS_BW_LOCK(mac_srs);
2060 			MAC_SRS_CHECK_BW_CONTROL(mac_srs);
2061 			if (mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)
2062 				bw_ctl_flag = B_TRUE;
2063 			MAC_SRS_BW_UNLOCK(mac_srs);
2064 		}
2065 		/*
2066 		 * The SRS_BW_ENFORCED flag may change since we have dropped
2067 		 * the mac_bw_lock. However the drain function can handle both
2068 		 * a drainable SRS or a bandwidth controlled SRS, and the
2069 		 * effect of scheduling a timeout is to wakeup the worker
2070 		 * thread which in turn will call the drain function. Since
2071 		 * we release the srs_lock atomically only in the cv_wait there
2072 		 * isn't a fear of waiting for ever.
2073 		 */
2074 		while (((mac_srs->srs_state & SRS_PROC) ||
2075 		    (mac_srs->srs_first == NULL) || bw_ctl_flag ||
2076 		    (mac_srs->srs_state & SRS_TX_BLOCKED)) &&
2077 		    !(mac_srs->srs_state & SRS_PAUSE)) {
2078 			/*
2079 			 * If we have packets queued and we are here
2080 			 * because B/W control is in place, we better
2081 			 * schedule the worker wakeup after 1 tick
2082 			 * to see if bandwidth control can be relaxed.
2083 			 */
2084 			if (bw_ctl_flag && mac_srs->srs_tid == NULL) {
2085 				/*
2086 				 * We need to ensure that a timer  is already
2087 				 * scheduled or we force  schedule one for
2088 				 * later so that we can continue processing
2089 				 * after this  quanta is over.
2090 				 */
2091 				mac_srs->srs_tid = timeout(mac_srs_fire,
2092 				    mac_srs, 1);
2093 			}
2094 wait:
2095 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
2096 			cv_wait(async, lock);
2097 			CALLB_CPR_SAFE_END(&cprinfo, lock);
2098 
2099 			if (mac_srs->srs_state & SRS_PAUSE)
2100 				goto done;
2101 			if (mac_srs->srs_state & SRS_PROC)
2102 				goto wait;
2103 
2104 			if (mac_srs->srs_first != NULL &&
2105 			    mac_srs->srs_type & SRST_BW_CONTROL) {
2106 				MAC_SRS_BW_LOCK(mac_srs);
2107 				if (mac_srs->srs_bw->mac_bw_state &
2108 				    SRS_BW_ENFORCED) {
2109 					MAC_SRS_CHECK_BW_CONTROL(mac_srs);
2110 				}
2111 				bw_ctl_flag = mac_srs->srs_bw->mac_bw_state &
2112 				    SRS_BW_ENFORCED;
2113 				MAC_SRS_BW_UNLOCK(mac_srs);
2114 			}
2115 		}
2116 
2117 		if (mac_srs->srs_state & SRS_PAUSE)
2118 			goto done;
2119 		mac_srs->srs_drain_func(mac_srs, SRS_WORKER);
2120 	}
2121 done:
2122 	/*
2123 	 * The Rx SRS quiesce logic first cuts off packet supply to the SRS
2124 	 * from both hard and soft classifications and waits for such threads
2125 	 * to finish before signaling the worker. So at this point the only
2126 	 * thread left that could be competing with the worker is the poll
2127 	 * thread. In the case of Tx, there shouldn't be any thread holding
2128 	 * SRS_PROC at this point.
2129 	 */
2130 	if (!(mac_srs->srs_state & SRS_PROC)) {
2131 		mac_srs->srs_state |= SRS_PROC;
2132 	} else {
2133 		ASSERT((mac_srs->srs_type & SRST_TX) == 0);
2134 		/*
2135 		 * Poll thread still owns the SRS and is still running
2136 		 */
2137 		ASSERT((mac_srs->srs_poll_thr == NULL) ||
2138 		    ((mac_srs->srs_state & SRS_POLL_THR_OWNER) ==
2139 		    SRS_POLL_THR_OWNER));
2140 	}
2141 	mac_srs_worker_quiesce(mac_srs);
2142 	/*
2143 	 * Wait for the SRS_RESTART or SRS_CONDEMNED signal from the initiator
2144 	 * of the quiesce operation
2145 	 */
2146 	while (!(mac_srs->srs_state & (SRS_CONDEMNED | SRS_RESTART)))
2147 		cv_wait(&mac_srs->srs_async, &mac_srs->srs_lock);
2148 
2149 	if (mac_srs->srs_state & SRS_RESTART) {
2150 		ASSERT(!(mac_srs->srs_state & SRS_CONDEMNED));
2151 		mac_srs_worker_restart(mac_srs);
2152 		mac_srs->srs_state &= ~SRS_PROC;
2153 		goto start;
2154 	}
2155 
2156 	if (!(mac_srs->srs_state & SRS_CONDEMNED_DONE))
2157 		mac_srs_worker_quiesce(mac_srs);
2158 
2159 	mac_srs->srs_state &= ~SRS_PROC;
2160 	/* The macro drops the srs_lock */
2161 	CALLB_CPR_EXIT(&cprinfo);
2162 	thread_exit();
2163 }
2164 
2165 /*
2166  * mac_rx_srs_subflow_process
2167  *
2168  * Receive side routine called from interrupt path when there are
2169  * sub flows present on this SRS.
2170  */
2171 /* ARGSUSED */
2172 void
2173 mac_rx_srs_subflow_process(void *arg, mac_resource_handle_t srs,
2174     mblk_t *mp_chain, boolean_t loopback)
2175 {
2176 	flow_entry_t		*flent = NULL;
2177 	flow_entry_t		*prev_flent = NULL;
2178 	mblk_t			*mp = NULL;
2179 	mblk_t			*tail = NULL;
2180 	mac_soft_ring_set_t	*mac_srs = (mac_soft_ring_set_t *)srs;
2181 	mac_client_impl_t	*mcip;
2182 
2183 	mcip = mac_srs->srs_mcip;
2184 	ASSERT(mcip != NULL);
2185 
2186 	/*
2187 	 * We need to determine the SRS for every packet
2188 	 * by walking the flow table, if we don't get any,
2189 	 * then we proceed using the SRS we came with.
2190 	 */
2191 	mp = tail = mp_chain;
2192 	while (mp != NULL) {
2193 
2194 		/*
2195 		 * We will increment the stats for the mactching subflow.
2196 		 * when we get the bytes/pkt count for the classified packets
2197 		 * later in mac_rx_srs_process.
2198 		 */
2199 		(void) mac_flow_lookup(mcip->mci_subflow_tab, mp,
2200 		    FLOW_INBOUND, &flent);
2201 
2202 		if (mp == mp_chain || flent == prev_flent) {
2203 			if (prev_flent != NULL)
2204 				FLOW_REFRELE(prev_flent);
2205 			prev_flent = flent;
2206 			flent = NULL;
2207 			tail = mp;
2208 			mp = mp->b_next;
2209 			continue;
2210 		}
2211 		tail->b_next = NULL;
2212 		/*
2213 		 * A null indicates, this is for the mac_srs itself.
2214 		 * XXX-venu : probably assert for fe_rx_srs_cnt == 0.
2215 		 */
2216 		if (prev_flent == NULL || prev_flent->fe_rx_srs_cnt == 0) {
2217 			mac_rx_srs_process(arg,
2218 			    (mac_resource_handle_t)mac_srs, mp_chain,
2219 			    loopback);
2220 		} else {
2221 			(prev_flent->fe_cb_fn)(prev_flent->fe_cb_arg1,
2222 			    prev_flent->fe_cb_arg2, mp_chain, loopback);
2223 			FLOW_REFRELE(prev_flent);
2224 		}
2225 		prev_flent = flent;
2226 		flent = NULL;
2227 		mp_chain = mp;
2228 		tail = mp;
2229 		mp = mp->b_next;
2230 	}
2231 	/* Last chain */
2232 	ASSERT(mp_chain != NULL);
2233 	if (prev_flent == NULL || prev_flent->fe_rx_srs_cnt == 0) {
2234 		mac_rx_srs_process(arg,
2235 		    (mac_resource_handle_t)mac_srs, mp_chain, loopback);
2236 	} else {
2237 		(prev_flent->fe_cb_fn)(prev_flent->fe_cb_arg1,
2238 		    prev_flent->fe_cb_arg2, mp_chain, loopback);
2239 		FLOW_REFRELE(prev_flent);
2240 	}
2241 }
2242 
2243 /*
2244  * mac_rx_srs_process
2245  *
2246  * Receive side routine called from the interrupt path.
2247  *
2248  * loopback is set to force a context switch on the loopback
2249  * path between MAC clients.
2250  */
2251 /* ARGSUSED */
2252 void
2253 mac_rx_srs_process(void *arg, mac_resource_handle_t srs, mblk_t *mp_chain,
2254     boolean_t loopback)
2255 {
2256 	mac_soft_ring_set_t	*mac_srs = (mac_soft_ring_set_t *)srs;
2257 	mblk_t			*mp, *tail, *head;
2258 	int			count = 0;
2259 	int			count1;
2260 	size_t			sz = 0;
2261 	size_t			chain_sz, sz1;
2262 	mac_bw_ctl_t		*mac_bw;
2263 	mac_client_impl_t	*smcip;
2264 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
2265 
2266 	/*
2267 	 * Set the tail, count and sz. We set the sz irrespective
2268 	 * of whether we are doing B/W control or not for the
2269 	 * purpose of updating the stats.
2270 	 */
2271 	mp = tail = mp_chain;
2272 	while (mp != NULL) {
2273 		tail = mp;
2274 		count++;
2275 		sz += msgdsize(mp);
2276 		mp = mp->b_next;
2277 	}
2278 
2279 	mutex_enter(&mac_srs->srs_lock);
2280 	smcip = mac_srs->srs_mcip;
2281 
2282 	if (mac_srs->srs_type & SRST_FLOW || smcip == NULL) {
2283 		FLOW_STAT_UPDATE(mac_srs->srs_flent, rbytes, sz);
2284 		FLOW_STAT_UPDATE(mac_srs->srs_flent, ipackets, count);
2285 	}
2286 	if (smcip != NULL) {
2287 		smcip->mci_stat_ibytes += sz;
2288 		smcip->mci_stat_ipackets += count;
2289 	}
2290 
2291 	/*
2292 	 * If the SRS in already being processed; has been blanked;
2293 	 * can be processed by worker thread only; or the B/W limit
2294 	 * has been reached, then queue the chain and check if
2295 	 * worker thread needs to be awakend.
2296 	 */
2297 	if (mac_srs->srs_type & SRST_BW_CONTROL) {
2298 		mac_bw = mac_srs->srs_bw;
2299 		ASSERT(mac_bw != NULL);
2300 		mutex_enter(&mac_bw->mac_bw_lock);
2301 		/* Count the packets and bytes via interrupt */
2302 		srs_rx->sr_intr_count += count;
2303 		mac_bw->mac_bw_intr += sz;
2304 		if (mac_bw->mac_bw_limit == 0) {
2305 			/* zero bandwidth: drop all */
2306 			srs_rx->sr_drop_count += count;
2307 			mac_bw->mac_bw_drop_bytes += sz;
2308 			mutex_exit(&mac_bw->mac_bw_lock);
2309 			mutex_exit(&mac_srs->srs_lock);
2310 			mac_pkt_drop(NULL, NULL, mp_chain, B_FALSE);
2311 			return;
2312 		} else {
2313 			if ((mac_bw->mac_bw_sz + sz) <=
2314 			    mac_bw->mac_bw_drop_threshold) {
2315 				mutex_exit(&mac_bw->mac_bw_lock);
2316 				MAC_RX_SRS_ENQUEUE_CHAIN(mac_srs, mp_chain,
2317 				    tail, count, sz);
2318 			} else {
2319 				mp = mp_chain;
2320 				chain_sz = 0;
2321 				count1 = 0;
2322 				tail = NULL;
2323 				head = NULL;
2324 				while (mp != NULL) {
2325 					sz1 = msgdsize(mp);
2326 					if (mac_bw->mac_bw_sz + chain_sz + sz1 >
2327 					    mac_bw->mac_bw_drop_threshold)
2328 						break;
2329 					chain_sz += sz1;
2330 					count1++;
2331 					tail = mp;
2332 					mp = mp->b_next;
2333 				}
2334 				mutex_exit(&mac_bw->mac_bw_lock);
2335 				if (tail != NULL) {
2336 					head = tail->b_next;
2337 					tail->b_next = NULL;
2338 					MAC_RX_SRS_ENQUEUE_CHAIN(mac_srs,
2339 					    mp_chain, tail, count1, chain_sz);
2340 					sz -= chain_sz;
2341 					count -= count1;
2342 				} else {
2343 					/* Can't pick up any */
2344 					head = mp_chain;
2345 				}
2346 				if (head != NULL) {
2347 					/* Drop any packet over the threshold */
2348 					srs_rx->sr_drop_count += count;
2349 					mutex_enter(&mac_bw->mac_bw_lock);
2350 					mac_bw->mac_bw_drop_bytes += sz;
2351 					mutex_exit(&mac_bw->mac_bw_lock);
2352 					freemsgchain(head);
2353 				}
2354 			}
2355 			MAC_SRS_WORKER_WAKEUP(mac_srs);
2356 			mutex_exit(&mac_srs->srs_lock);
2357 			return;
2358 		}
2359 	}
2360 
2361 	/*
2362 	 * If the total number of packets queued in the SRS and
2363 	 * its associated soft rings exceeds the max allowed,
2364 	 * then drop the chain. If we are polling capable, this
2365 	 * shouldn't be happening.
2366 	 */
2367 	if (!(mac_srs->srs_type & SRST_BW_CONTROL) &&
2368 	    (srs_rx->sr_poll_pkt_cnt > srs_rx->sr_hiwat)) {
2369 		mac_bw = mac_srs->srs_bw;
2370 		srs_rx->sr_drop_count += count;
2371 		mutex_enter(&mac_bw->mac_bw_lock);
2372 		mac_bw->mac_bw_drop_bytes += sz;
2373 		mutex_exit(&mac_bw->mac_bw_lock);
2374 		freemsgchain(mp_chain);
2375 		mutex_exit(&mac_srs->srs_lock);
2376 		return;
2377 	}
2378 
2379 	MAC_RX_SRS_ENQUEUE_CHAIN(mac_srs, mp_chain, tail, count, sz);
2380 	/* Count the packets entering via interrupt path */
2381 	srs_rx->sr_intr_count += count;
2382 
2383 	if (!(mac_srs->srs_state & SRS_PROC)) {
2384 		/*
2385 		 * If we are coming via loopback or if we are not
2386 		 * optimizing for latency, we should signal the
2387 		 * worker thread.
2388 		 */
2389 		if (loopback || !(mac_srs->srs_state & SRS_LATENCY_OPT)) {
2390 			/*
2391 			 * For loopback, We need to let the worker take
2392 			 * over as we don't want to continue in the same
2393 			 * thread even if we can. This could lead to stack
2394 			 * overflows and may also end up using
2395 			 * resources (cpu) incorrectly.
2396 			 */
2397 			cv_signal(&mac_srs->srs_async);
2398 		} else {
2399 			/*
2400 			 * Seems like no one is processing the SRS and
2401 			 * there is no backlog. We also inline process
2402 			 * our packet if its a single packet in non
2403 			 * latency optimized case (in latency optimized
2404 			 * case, we inline process chains of any size).
2405 			 */
2406 			mac_srs->srs_drain_func(mac_srs, SRS_PROC_FAST);
2407 		}
2408 	}
2409 	mutex_exit(&mac_srs->srs_lock);
2410 }
2411 
2412 /* TX SIDE ROUTINES (RUNTIME) */
2413 
2414 /*
2415  * mac_tx_srs_no_desc
2416  *
2417  * This routine is called by Tx single ring default mode
2418  * when Tx ring runs out of descs.
2419  */
2420 mac_tx_cookie_t
2421 mac_tx_srs_no_desc(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2422     uint16_t flag, mblk_t **ret_mp)
2423 {
2424 	mac_tx_cookie_t cookie = NULL;
2425 	mac_srs_tx_t *srs_tx = &mac_srs->srs_tx;
2426 	boolean_t wakeup_worker = B_TRUE;
2427 	uint32_t tx_mode = srs_tx->st_mode;
2428 	int cnt, sz;
2429 	mblk_t *tail;
2430 
2431 	ASSERT(tx_mode == SRS_TX_DEFAULT || tx_mode == SRS_TX_BW);
2432 	if (flag & MAC_DROP_ON_NO_DESC) {
2433 		MAC_TX_SRS_DROP_MESSAGE(mac_srs, mp_chain, cookie);
2434 	} else {
2435 		if (mac_srs->srs_first != NULL)
2436 			wakeup_worker = B_FALSE;
2437 		MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
2438 		if (flag & MAC_TX_NO_ENQUEUE) {
2439 			/*
2440 			 * If TX_QUEUED is not set, queue the
2441 			 * packet and let mac_tx_srs_drain()
2442 			 * set the TX_BLOCKED bit for the
2443 			 * reasons explained above. Otherwise,
2444 			 * return the mblks.
2445 			 */
2446 			if (wakeup_worker) {
2447 				MAC_TX_SRS_ENQUEUE_CHAIN(mac_srs,
2448 				    mp_chain, tail, cnt, sz);
2449 			} else {
2450 				MAC_TX_SET_NO_ENQUEUE(mac_srs,
2451 				    mp_chain, ret_mp, cookie);
2452 			}
2453 		} else {
2454 			MAC_TX_SRS_TEST_HIWAT(mac_srs, mp_chain,
2455 			    tail, cnt, sz, cookie);
2456 		}
2457 		if (wakeup_worker)
2458 			cv_signal(&mac_srs->srs_async);
2459 	}
2460 	return (cookie);
2461 }
2462 
2463 /*
2464  * mac_tx_srs_enqueue
2465  *
2466  * This routine is called when Tx SRS is operating in either serializer
2467  * or bandwidth mode. In serializer mode, a packet will get enqueued
2468  * when a thread cannot enter SRS exclusively. In bandwidth mode,
2469  * packets gets queued if allowed byte-count limit for a tick is
2470  * exceeded. The action that gets taken when MAC_DROP_ON_NO_DESC and
2471  * MAC_TX_NO_ENQUEUE is set is different than when operaing in either
2472  * the default mode or fanout mode. Here packets get dropped or
2473  * returned back to the caller only after hi-watermark worth of data
2474  * is queued.
2475  */
2476 static mac_tx_cookie_t
2477 mac_tx_srs_enqueue(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2478     uint16_t flag, uintptr_t fanout_hint, mblk_t **ret_mp)
2479 {
2480 	mac_tx_cookie_t cookie = NULL;
2481 	int cnt, sz;
2482 	mblk_t *tail;
2483 	boolean_t wakeup_worker = B_TRUE;
2484 
2485 	/*
2486 	 * Ignore fanout hint if we don't have multiple tx rings.
2487 	 */
2488 	if (!TX_MULTI_RING_MODE(mac_srs))
2489 		fanout_hint = 0;
2490 
2491 	if (mac_srs->srs_first != NULL)
2492 		wakeup_worker = B_FALSE;
2493 	MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
2494 	if (flag & MAC_DROP_ON_NO_DESC) {
2495 		if (mac_srs->srs_count > mac_srs->srs_tx.st_hiwat) {
2496 			MAC_TX_SRS_DROP_MESSAGE(mac_srs, mp_chain, cookie);
2497 		} else {
2498 			MAC_TX_SRS_ENQUEUE_CHAIN(mac_srs,
2499 			    mp_chain, tail, cnt, sz);
2500 		}
2501 	} else if (flag & MAC_TX_NO_ENQUEUE) {
2502 		if ((mac_srs->srs_count > mac_srs->srs_tx.st_hiwat) ||
2503 		    (mac_srs->srs_state & SRS_TX_WAKEUP_CLIENT)) {
2504 			MAC_TX_SET_NO_ENQUEUE(mac_srs, mp_chain,
2505 			    ret_mp, cookie);
2506 		} else {
2507 			mp_chain->b_prev = (mblk_t *)fanout_hint;
2508 			MAC_TX_SRS_ENQUEUE_CHAIN(mac_srs,
2509 			    mp_chain, tail, cnt, sz);
2510 		}
2511 	} else {
2512 		/*
2513 		 * If you are BW_ENFORCED, just enqueue the
2514 		 * packet. srs_worker will drain it at the
2515 		 * prescribed rate. Before enqueueing, save
2516 		 * the fanout hint.
2517 		 */
2518 		mp_chain->b_prev = (mblk_t *)fanout_hint;
2519 		MAC_TX_SRS_TEST_HIWAT(mac_srs, mp_chain,
2520 		    tail, cnt, sz, cookie);
2521 	}
2522 	if (wakeup_worker)
2523 		cv_signal(&mac_srs->srs_async);
2524 	return (cookie);
2525 }
2526 
2527 /*
2528  * There are five tx modes:
2529  *
2530  * 1) Default mode (SRS_TX_DEFAULT)
2531  * 2) Serialization mode (SRS_TX_SERIALIZE)
2532  * 3) Fanout mode (SRS_TX_FANOUT)
2533  * 4) Bandwdith mode (SRS_TX_BW)
2534  * 5) Fanout and Bandwidth mode (SRS_TX_BW_FANOUT)
2535  *
2536  * The tx mode in which an SRS operates is decided in mac_tx_srs_setup()
2537  * based on the number of Tx rings requested for an SRS and whether
2538  * bandwidth control is requested or not.
2539  *
2540  * In the default mode (i.e., no fanout/no bandwidth), the SRS acts as a
2541  * pass-thru. Packets will go directly to mac_tx_send(). When the underlying
2542  * Tx ring runs out of Tx descs, it starts queueing up packets in SRS.
2543  * When flow-control is relieved, the srs_worker drains the queued
2544  * packets and informs blocked clients to restart sending packets.
2545  *
2546  * In the SRS_TX_SERIALIZE mode, all calls to mac_tx() are serialized.
2547  *
2548  * In the SRS_TX_FANOUT mode, packets will be fanned out to multiple
2549  * Tx rings. Each Tx ring will have a soft ring associated with it.
2550  * These soft rings will be hung off the Tx SRS. Queueing if it happens
2551  * due to lack of Tx desc will be in individual soft ring (and not srs)
2552  * associated with Tx ring.
2553  *
2554  * In the TX_BW mode, tx srs will allow packets to go down to Tx ring
2555  * only if bw is available. Otherwise the packets will be queued in
2556  * SRS. If fanout to multiple Tx rings is configured, the packets will
2557  * be fanned out among the soft rings associated with the Tx rings.
2558  *
2559  * Four flags are used in srs_state for indicating flow control
2560  * conditions : SRS_TX_BLOCKED, SRS_TX_HIWAT, SRS_TX_WAKEUP_CLIENT.
2561  * SRS_TX_BLOCKED indicates out of Tx descs. SRS expects a wakeup from the
2562  * driver below.
2563  * SRS_TX_HIWAT indicates packet count enqueued in Tx SRS exceeded Tx hiwat
2564  * and flow-control pressure is applied back to clients. The clients expect
2565  * wakeup when flow-control is relieved.
2566  * SRS_TX_WAKEUP_CLIENT get set when (flag == MAC_TX_NO_ENQUEUE) and mblk
2567  * got returned back to client either due to lack of Tx descs or due to bw
2568  * control reasons. The clients expect a wakeup when condition is relieved.
2569  *
2570  * The fourth argument to mac_tx() is the flag. Normally it will be 0 but
2571  * some clients set the following values too: MAC_DROP_ON_NO_DESC,
2572  * MAC_TX_NO_ENQUEUE
2573  * Mac clients that do not want packets to be enqueued in the mac layer set
2574  * MAC_DROP_ON_NO_DESC value. The packets won't be queued in the Tx SRS or
2575  * Tx soft rings but instead get dropped when the NIC runs out of desc. The
2576  * behaviour of this flag is different when the Tx is running in serializer
2577  * or bandwidth mode. Under these (Serializer, bandwidth) modes, the packet
2578  * get dropped when Tx high watermark is reached.
2579  * There are some mac clients like vsw, aggr that want the mblks to be
2580  * returned back to clients instead of being queued in Tx SRS (or Tx soft
2581  * rings) under flow-control (i.e., out of desc or exceeding bw limits)
2582  * conditions. These clients call mac_tx() with MAC_TX_NO_ENQUEUE flag set.
2583  * In the default and Tx fanout mode, the un-transmitted mblks will be
2584  * returned back to the clients when the driver runs out of Tx descs.
2585  * SRS_TX_WAKEUP_CLIENT (or S_RING_WAKEUP_CLIENT) will be set in SRS (or
2586  * soft ring) so that the clients can be woken up when Tx desc become
2587  * available. When running in serializer or bandwidth mode mode,
2588  * SRS_TX_WAKEUP_CLIENT will be set when tx hi-watermark is reached.
2589  */
2590 
2591 mac_tx_func_t
2592 mac_tx_get_func(uint32_t mode)
2593 {
2594 	return (mac_tx_mode_list[mode].mac_tx_func);
2595 }
2596 
2597 /* ARGSUSED */
2598 static mac_tx_cookie_t
2599 mac_tx_single_ring_mode(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2600     uintptr_t fanout_hint, uint16_t flag, mblk_t **ret_mp)
2601 {
2602 	mac_srs_tx_t		*srs_tx = &mac_srs->srs_tx;
2603 	boolean_t		is_subflow;
2604 	mac_tx_stats_t		stats;
2605 	mac_tx_cookie_t		cookie = NULL;
2606 
2607 	ASSERT(srs_tx->st_mode == SRS_TX_DEFAULT);
2608 
2609 	/* Regular case with a single Tx ring */
2610 	/*
2611 	 * SRS_TX_BLOCKED is set when underlying NIC runs
2612 	 * out of Tx descs and messages start getting
2613 	 * queued. It won't get reset until
2614 	 * tx_srs_drain() completely drains out the
2615 	 * messages.
2616 	 */
2617 	if ((mac_srs->srs_state & SRS_ENQUEUED) != 0) {
2618 		/* Tx descs/resources not available */
2619 		mutex_enter(&mac_srs->srs_lock);
2620 		if ((mac_srs->srs_state & SRS_ENQUEUED) != 0) {
2621 			cookie = mac_tx_srs_no_desc(mac_srs, mp_chain,
2622 			    flag, ret_mp);
2623 			mutex_exit(&mac_srs->srs_lock);
2624 			return (cookie);
2625 		}
2626 		/*
2627 		 * While we were computing mblk count, the
2628 		 * flow control condition got relieved.
2629 		 * Continue with the transmission.
2630 		 */
2631 		mutex_exit(&mac_srs->srs_lock);
2632 	}
2633 
2634 	is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
2635 
2636 	mp_chain = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
2637 	    mp_chain, (is_subflow ? &stats : NULL));
2638 
2639 	/*
2640 	 * Multiple threads could be here sending packets.
2641 	 * Under such conditions, it is not possible to
2642 	 * automically set SRS_TX_BLOCKED bit to indicate
2643 	 * out of tx desc condition. To atomically set
2644 	 * this, we queue the returned packet and do
2645 	 * the setting of SRS_TX_BLOCKED in
2646 	 * mac_tx_srs_drain().
2647 	 */
2648 	if (mp_chain != NULL) {
2649 		mutex_enter(&mac_srs->srs_lock);
2650 		cookie = mac_tx_srs_no_desc(mac_srs, mp_chain, flag, ret_mp);
2651 		mutex_exit(&mac_srs->srs_lock);
2652 		return (cookie);
2653 	}
2654 
2655 	if (is_subflow)
2656 		FLOW_TX_STATS_UPDATE(mac_srs->srs_flent, &stats);
2657 
2658 	return (NULL);
2659 }
2660 
2661 /*
2662  * mac_tx_serialize_mode
2663  *
2664  * This is an experimental mode implemented as per the request of PAE.
2665  * In this mode, all callers attempting to send a packet to the NIC
2666  * will get serialized. Only one thread at any time will access the
2667  * NIC to send the packet out.
2668  */
2669 /* ARGSUSED */
2670 static mac_tx_cookie_t
2671 mac_tx_serializer_mode(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2672     uintptr_t fanout_hint, uint16_t flag, mblk_t **ret_mp)
2673 {
2674 	boolean_t		is_subflow;
2675 	mac_tx_stats_t		stats;
2676 	mac_tx_cookie_t		cookie = NULL;
2677 	mac_srs_tx_t		*srs_tx = &mac_srs->srs_tx;
2678 
2679 	/* Single ring, serialize below */
2680 	ASSERT(srs_tx->st_mode == SRS_TX_SERIALIZE);
2681 	mutex_enter(&mac_srs->srs_lock);
2682 	if ((mac_srs->srs_first != NULL) ||
2683 	    (mac_srs->srs_state & SRS_PROC)) {
2684 		/*
2685 		 * In serialization mode, queue all packets until
2686 		 * TX_HIWAT is set.
2687 		 * If drop bit is set, drop if TX_HIWAT is set.
2688 		 * If no_enqueue is set, still enqueue until hiwat
2689 		 * is set and return mblks after TX_HIWAT is set.
2690 		 */
2691 		cookie = mac_tx_srs_enqueue(mac_srs, mp_chain,
2692 		    flag, NULL, ret_mp);
2693 		mutex_exit(&mac_srs->srs_lock);
2694 		return (cookie);
2695 	}
2696 	/*
2697 	 * No packets queued, nothing on proc and no flow
2698 	 * control condition. Fast-path, ok. Do inline
2699 	 * processing.
2700 	 */
2701 	mac_srs->srs_state |= SRS_PROC;
2702 	mutex_exit(&mac_srs->srs_lock);
2703 
2704 	is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
2705 
2706 	mp_chain = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
2707 	    mp_chain, (is_subflow ? &stats : NULL));
2708 
2709 	mutex_enter(&mac_srs->srs_lock);
2710 	mac_srs->srs_state &= ~SRS_PROC;
2711 	if (mp_chain != NULL) {
2712 		cookie = mac_tx_srs_enqueue(mac_srs,
2713 		    mp_chain, flag, NULL, ret_mp);
2714 	}
2715 	if (mac_srs->srs_first != NULL) {
2716 		/*
2717 		 * We processed inline our packet and a new
2718 		 * packet/s got queued while we were
2719 		 * processing. Wakeup srs worker
2720 		 */
2721 		cv_signal(&mac_srs->srs_async);
2722 	}
2723 	mutex_exit(&mac_srs->srs_lock);
2724 
2725 	if (is_subflow && cookie == NULL)
2726 		FLOW_TX_STATS_UPDATE(mac_srs->srs_flent, &stats);
2727 
2728 	return (cookie);
2729 }
2730 
2731 /*
2732  * mac_tx_fanout_mode
2733  *
2734  * In this mode, the SRS will have access to multiple Tx rings to send
2735  * the packet out. The fanout hint that is passed as an argument is
2736  * used to find an appropriate ring to fanout the traffic. Each Tx
2737  * ring, in turn,  will have a soft ring associated with it. If a Tx
2738  * ring runs out of Tx desc's the returned packet will be queued in
2739  * the soft ring associated with that Tx ring. The srs itself will not
2740  * queue any packets.
2741  */
2742 
2743 #define	MAC_TX_SOFT_RING_PROCESS(chain) {		       		\
2744 	index = COMPUTE_INDEX(hash, mac_srs->srs_oth_ring_count),	\
2745 	softring = mac_srs->srs_oth_soft_rings[index];			\
2746 	cookie = mac_tx_soft_ring_process(softring, chain, flag, ret_mp); \
2747 	DTRACE_PROBE2(tx__fanout, uint64_t, hash, uint_t, index);	\
2748 }
2749 
2750 static mac_tx_cookie_t
2751 mac_tx_fanout_mode(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2752     uintptr_t fanout_hint, uint16_t flag, mblk_t **ret_mp)
2753 {
2754 	mac_soft_ring_t		*softring;
2755 	uint64_t		hash;
2756 	uint_t			index;
2757 	mac_tx_cookie_t		cookie = NULL;
2758 
2759 	ASSERT(mac_srs->srs_tx.st_mode == SRS_TX_FANOUT);
2760 	if (fanout_hint != 0) {
2761 		/*
2762 		 * The hint is specified by the caller, simply pass the
2763 		 * whole chain to the soft ring.
2764 		 */
2765 		hash = HASH_HINT(fanout_hint);
2766 		MAC_TX_SOFT_RING_PROCESS(mp_chain);
2767 	} else {
2768 		mblk_t *last_mp, *cur_mp, *sub_chain;
2769 		uint64_t last_hash = 0;
2770 		uint_t media = mac_srs->srs_mcip->mci_mip->mi_info.mi_media;
2771 
2772 		/*
2773 		 * Compute the hash from the contents (headers) of the
2774 		 * packets of the mblk chain. Split the chains into
2775 		 * subchains of the same conversation.
2776 		 *
2777 		 * Since there may be more than one ring used for
2778 		 * sub-chains of the same call, and since the caller
2779 		 * does not maintain per conversation state since it
2780 		 * passed a zero hint, unsent subchains will be
2781 		 * dropped.
2782 		 */
2783 
2784 		flag |= MAC_DROP_ON_NO_DESC;
2785 		ret_mp = NULL;
2786 
2787 		ASSERT(ret_mp == NULL);
2788 
2789 		sub_chain = NULL;
2790 		last_mp = NULL;
2791 
2792 		for (cur_mp = mp_chain; cur_mp != NULL;
2793 		    cur_mp = cur_mp->b_next) {
2794 			hash = mac_pkt_hash(media, cur_mp, MAC_PKT_HASH_L4,
2795 			    B_TRUE);
2796 			if (last_hash != 0 && hash != last_hash) {
2797 				/*
2798 				 * Starting a different subchain, send current
2799 				 * chain out.
2800 				 */
2801 				ASSERT(last_mp != NULL);
2802 				last_mp->b_next = NULL;
2803 				MAC_TX_SOFT_RING_PROCESS(sub_chain);
2804 				sub_chain = NULL;
2805 			}
2806 
2807 			/* add packet to subchain */
2808 			if (sub_chain == NULL)
2809 				sub_chain = cur_mp;
2810 			last_mp = cur_mp;
2811 			last_hash = hash;
2812 		}
2813 
2814 		if (sub_chain != NULL) {
2815 			/* send last subchain */
2816 			ASSERT(last_mp != NULL);
2817 			last_mp->b_next = NULL;
2818 			MAC_TX_SOFT_RING_PROCESS(sub_chain);
2819 		}
2820 
2821 		cookie = NULL;
2822 	}
2823 
2824 	return (cookie);
2825 }
2826 
2827 /*
2828  * mac_tx_bw_mode
2829  *
2830  * In the bandwidth mode, Tx srs will allow packets to go down to Tx ring
2831  * only if bw is available. Otherwise the packets will be queued in
2832  * SRS. If the SRS has multiple Tx rings, then packets will get fanned
2833  * out to a Tx rings.
2834  */
2835 static mac_tx_cookie_t
2836 mac_tx_bw_mode(mac_soft_ring_set_t *mac_srs, mblk_t *mp_chain,
2837     uintptr_t fanout_hint, uint16_t flag, mblk_t **ret_mp)
2838 {
2839 	int			cnt, sz;
2840 	mblk_t			*tail;
2841 	mac_tx_cookie_t		cookie = NULL;
2842 	mac_srs_tx_t		*srs_tx = &mac_srs->srs_tx;
2843 
2844 	ASSERT(TX_BANDWIDTH_MODE(mac_srs));
2845 	ASSERT(mac_srs->srs_type & SRST_BW_CONTROL);
2846 	mutex_enter(&mac_srs->srs_lock);
2847 	if (mac_srs->srs_bw->mac_bw_limit == 0) {
2848 		/*
2849 		 * zero bandwidth, no traffic is sent: drop the packets,
2850 		 * or return the whole chain if the caller requests all
2851 		 * unsent packets back.
2852 		 */
2853 		if (flag & MAC_TX_NO_ENQUEUE) {
2854 			cookie = (mac_tx_cookie_t)mac_srs;
2855 			*ret_mp = mp_chain;
2856 		} else {
2857 			MAC_TX_SRS_DROP_MESSAGE(mac_srs, mp_chain, cookie);
2858 		}
2859 		mutex_exit(&mac_srs->srs_lock);
2860 		return (cookie);
2861 	} else if ((mac_srs->srs_first != NULL) ||
2862 	    (mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED)) {
2863 		cookie = mac_tx_srs_enqueue(mac_srs, mp_chain, flag,
2864 		    fanout_hint, ret_mp);
2865 		mutex_exit(&mac_srs->srs_lock);
2866 		return (cookie);
2867 	}
2868 	MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
2869 	if (mac_srs->srs_bw->mac_bw_curr_time != lbolt) {
2870 		mac_srs->srs_bw->mac_bw_curr_time = lbolt;
2871 		mac_srs->srs_bw->mac_bw_used = 0;
2872 	} else if (mac_srs->srs_bw->mac_bw_used >
2873 	    mac_srs->srs_bw->mac_bw_limit) {
2874 		mac_srs->srs_bw->mac_bw_state |= SRS_BW_ENFORCED;
2875 		MAC_TX_SRS_ENQUEUE_CHAIN(mac_srs,
2876 		    mp_chain, tail, cnt, sz);
2877 		/*
2878 		 * Wakeup worker thread. Note that worker
2879 		 * thread has to be woken up so that it
2880 		 * can fire up the timer to be woken up
2881 		 * on the next tick. Also once
2882 		 * BW_ENFORCED is set, it can only be
2883 		 * reset by srs_worker thread. Until then
2884 		 * all packets will get queued up in SRS
2885 		 * and hence this this code path won't be
2886 		 * entered until BW_ENFORCED is reset.
2887 		 */
2888 		cv_signal(&mac_srs->srs_async);
2889 		mutex_exit(&mac_srs->srs_lock);
2890 		return (cookie);
2891 	}
2892 
2893 	mac_srs->srs_bw->mac_bw_used += sz;
2894 	mutex_exit(&mac_srs->srs_lock);
2895 
2896 	if (srs_tx->st_mode == SRS_TX_BW_FANOUT) {
2897 		mac_soft_ring_t *softring;
2898 		uint_t indx, hash;
2899 
2900 		hash = HASH_HINT(fanout_hint);
2901 		indx = COMPUTE_INDEX(hash,
2902 		    mac_srs->srs_oth_ring_count);
2903 		softring = mac_srs->srs_oth_soft_rings[indx];
2904 		return (mac_tx_soft_ring_process(softring, mp_chain, flag,
2905 		    ret_mp));
2906 	} else {
2907 		boolean_t		is_subflow;
2908 		mac_tx_stats_t		stats;
2909 
2910 		is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
2911 
2912 		mp_chain = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
2913 		    mp_chain, (is_subflow ? &stats : NULL));
2914 
2915 		if (mp_chain != NULL) {
2916 			mutex_enter(&mac_srs->srs_lock);
2917 			MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
2918 			if (mac_srs->srs_bw->mac_bw_used > sz)
2919 				mac_srs->srs_bw->mac_bw_used -= sz;
2920 			else
2921 				mac_srs->srs_bw->mac_bw_used = 0;
2922 			cookie = mac_tx_srs_enqueue(mac_srs, mp_chain, flag,
2923 			    fanout_hint, ret_mp);
2924 			mutex_exit(&mac_srs->srs_lock);
2925 			return (cookie);
2926 		}
2927 		if (is_subflow)
2928 			FLOW_TX_STATS_UPDATE(mac_srs->srs_flent, &stats);
2929 
2930 		return (NULL);
2931 	}
2932 }
2933 
2934 /* ARGSUSED */
2935 void
2936 mac_tx_srs_drain(mac_soft_ring_set_t *mac_srs, uint_t proc_type)
2937 {
2938 	mblk_t			*head, *tail;
2939 	size_t			sz;
2940 	uint32_t		tx_mode;
2941 	uint_t			saved_pkt_count;
2942 	boolean_t		is_subflow;
2943 	mac_tx_stats_t		stats;
2944 	mac_srs_tx_t		*srs_tx = &mac_srs->srs_tx;
2945 
2946 	saved_pkt_count = 0;
2947 	ASSERT(mutex_owned(&mac_srs->srs_lock));
2948 	ASSERT(!(mac_srs->srs_state & SRS_PROC));
2949 
2950 	mac_srs->srs_state |= SRS_PROC;
2951 
2952 	is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
2953 	tx_mode = srs_tx->st_mode;
2954 	if (tx_mode == SRS_TX_DEFAULT || tx_mode == SRS_TX_SERIALIZE) {
2955 		if (mac_srs->srs_first != NULL) {
2956 			head = mac_srs->srs_first;
2957 			tail = mac_srs->srs_last;
2958 			saved_pkt_count = mac_srs->srs_count;
2959 			mac_srs->srs_first = NULL;
2960 			mac_srs->srs_last = NULL;
2961 			mac_srs->srs_count = 0;
2962 			mutex_exit(&mac_srs->srs_lock);
2963 
2964 			head = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
2965 			    head, &stats);
2966 
2967 			mutex_enter(&mac_srs->srs_lock);
2968 			if (head != NULL) {
2969 				/* Device out of tx desc, set block */
2970 				if (head->b_next == NULL)
2971 					VERIFY(head == tail);
2972 				tail->b_next = mac_srs->srs_first;
2973 				mac_srs->srs_first = head;
2974 				mac_srs->srs_count +=
2975 				    (saved_pkt_count - stats.ts_opackets);
2976 				if (mac_srs->srs_last == NULL)
2977 					mac_srs->srs_last = tail;
2978 				MAC_TX_SRS_BLOCK(mac_srs, head);
2979 			} else {
2980 				srs_tx->st_woken_up = B_FALSE;
2981 				if (is_subflow) {
2982 					FLOW_TX_STATS_UPDATE(
2983 					    mac_srs->srs_flent, &stats);
2984 				}
2985 			}
2986 		}
2987 	} else if (tx_mode == SRS_TX_BW) {
2988 		/*
2989 		 * We are here because the timer fired and we have some data
2990 		 * to tranmit. Also mac_tx_srs_worker should have reset
2991 		 * SRS_BW_ENFORCED flag
2992 		 */
2993 		ASSERT(!(mac_srs->srs_bw->mac_bw_state & SRS_BW_ENFORCED));
2994 		head = tail = mac_srs->srs_first;
2995 		while (mac_srs->srs_first != NULL) {
2996 			tail = mac_srs->srs_first;
2997 			tail->b_prev = NULL;
2998 			mac_srs->srs_first = tail->b_next;
2999 			if (mac_srs->srs_first == NULL)
3000 				mac_srs->srs_last = NULL;
3001 			mac_srs->srs_count--;
3002 			sz = msgdsize(tail);
3003 			mac_srs->srs_size -= sz;
3004 			saved_pkt_count++;
3005 			MAC_TX_UPDATE_BW_INFO(mac_srs, sz);
3006 
3007 			if (mac_srs->srs_bw->mac_bw_used <
3008 			    mac_srs->srs_bw->mac_bw_limit)
3009 				continue;
3010 
3011 			if (mac_srs->srs_bw->mac_bw_curr_time != lbolt) {
3012 				mac_srs->srs_bw->mac_bw_curr_time = lbolt;
3013 				mac_srs->srs_bw->mac_bw_used = sz;
3014 				continue;
3015 			}
3016 			mac_srs->srs_bw->mac_bw_state |= SRS_BW_ENFORCED;
3017 			break;
3018 		}
3019 
3020 		ASSERT((head == NULL && tail == NULL) ||
3021 		    (head != NULL && tail != NULL));
3022 		if (tail != NULL) {
3023 			tail->b_next = NULL;
3024 			mutex_exit(&mac_srs->srs_lock);
3025 
3026 			head = mac_tx_send(srs_tx->st_arg1, srs_tx->st_arg2,
3027 			    head, &stats);
3028 
3029 			mutex_enter(&mac_srs->srs_lock);
3030 			if (head != NULL) {
3031 				uint_t size_sent;
3032 
3033 				/* Device out of tx desc, set block */
3034 				if (head->b_next == NULL)
3035 					VERIFY(head == tail);
3036 				tail->b_next = mac_srs->srs_first;
3037 				mac_srs->srs_first = head;
3038 				mac_srs->srs_count +=
3039 				    (saved_pkt_count - stats.ts_opackets);
3040 				if (mac_srs->srs_last == NULL)
3041 					mac_srs->srs_last = tail;
3042 				size_sent = sz - stats.ts_obytes;
3043 				mac_srs->srs_size += size_sent;
3044 				mac_srs->srs_bw->mac_bw_sz += size_sent;
3045 				if (mac_srs->srs_bw->mac_bw_used > size_sent) {
3046 					mac_srs->srs_bw->mac_bw_used -=
3047 					    size_sent;
3048 				} else {
3049 					mac_srs->srs_bw->mac_bw_used = 0;
3050 				}
3051 				MAC_TX_SRS_BLOCK(mac_srs, head);
3052 			} else {
3053 				srs_tx->st_woken_up = B_FALSE;
3054 				if (is_subflow) {
3055 					FLOW_TX_STATS_UPDATE(
3056 					    mac_srs->srs_flent, &stats);
3057 				}
3058 			}
3059 		}
3060 	} else if (tx_mode == SRS_TX_BW_FANOUT) {
3061 		mblk_t *prev;
3062 		mac_soft_ring_t *softring;
3063 		uint64_t hint;
3064 
3065 		/*
3066 		 * We are here because the timer fired and we
3067 		 * have some quota to tranmit.
3068 		 */
3069 		prev = NULL;
3070 		head = tail = mac_srs->srs_first;
3071 		while (mac_srs->srs_first != NULL) {
3072 			tail = mac_srs->srs_first;
3073 			mac_srs->srs_first = tail->b_next;
3074 			if (mac_srs->srs_first == NULL)
3075 				mac_srs->srs_last = NULL;
3076 			mac_srs->srs_count--;
3077 			sz = msgdsize(tail);
3078 			mac_srs->srs_size -= sz;
3079 			mac_srs->srs_bw->mac_bw_used += sz;
3080 			if (prev == NULL)
3081 				hint = (ulong_t)tail->b_prev;
3082 			if (hint != (ulong_t)tail->b_prev) {
3083 				prev->b_next = NULL;
3084 				mutex_exit(&mac_srs->srs_lock);
3085 				TX_SRS_TO_SOFT_RING(mac_srs, head, hint);
3086 				head = tail;
3087 				hint = (ulong_t)tail->b_prev;
3088 				mutex_enter(&mac_srs->srs_lock);
3089 			}
3090 
3091 			prev = tail;
3092 			tail->b_prev = NULL;
3093 			if (mac_srs->srs_bw->mac_bw_used <
3094 			    mac_srs->srs_bw->mac_bw_limit)
3095 				continue;
3096 
3097 			if (mac_srs->srs_bw->mac_bw_curr_time != lbolt) {
3098 				mac_srs->srs_bw->mac_bw_curr_time = lbolt;
3099 				mac_srs->srs_bw->mac_bw_used = 0;
3100 				continue;
3101 			}
3102 			mac_srs->srs_bw->mac_bw_state |= SRS_BW_ENFORCED;
3103 			break;
3104 		}
3105 		ASSERT((head == NULL && tail == NULL) ||
3106 		    (head != NULL && tail != NULL));
3107 		if (tail != NULL) {
3108 			tail->b_next = NULL;
3109 			mutex_exit(&mac_srs->srs_lock);
3110 			TX_SRS_TO_SOFT_RING(mac_srs, head, hint);
3111 			mutex_enter(&mac_srs->srs_lock);
3112 		}
3113 	}
3114 	/*
3115 	 * SRS_TX_FANOUT case not considered here because packets
3116 	 * won't be queued in the SRS for this case. Packets will
3117 	 * be sent directly to soft rings underneath and if there
3118 	 * is any queueing at all, it would be in Tx side soft
3119 	 * rings.
3120 	 */
3121 
3122 	/*
3123 	 * When srs_count becomes 0, reset SRS_TX_HIWAT and
3124 	 * SRS_TX_WAKEUP_CLIENT and wakeup registered clients.
3125 	 */
3126 	if (mac_srs->srs_count == 0 && (mac_srs->srs_state &
3127 	    (SRS_TX_HIWAT | SRS_TX_WAKEUP_CLIENT | SRS_ENQUEUED))) {
3128 		mac_tx_notify_cb_t *mtnfp;
3129 		mac_cb_t *mcb;
3130 		mac_client_impl_t *mcip = mac_srs->srs_mcip;
3131 		boolean_t wakeup_required = B_FALSE;
3132 
3133 		if (mac_srs->srs_state &
3134 		    (SRS_TX_HIWAT|SRS_TX_WAKEUP_CLIENT)) {
3135 			wakeup_required = B_TRUE;
3136 		}
3137 		mac_srs->srs_state &= ~(SRS_TX_HIWAT |
3138 		    SRS_TX_WAKEUP_CLIENT | SRS_ENQUEUED);
3139 		mutex_exit(&mac_srs->srs_lock);
3140 		if (wakeup_required) {
3141 			/* Wakeup callback registered clients */
3142 			MAC_CALLBACK_WALKER_INC(&mcip->mci_tx_notify_cb_info);
3143 			for (mcb = mcip->mci_tx_notify_cb_list; mcb != NULL;
3144 			    mcb = mcb->mcb_nextp) {
3145 				mtnfp = (mac_tx_notify_cb_t *)mcb->mcb_objp;
3146 				mtnfp->mtnf_fn(mtnfp->mtnf_arg,
3147 				    (mac_tx_cookie_t)mac_srs);
3148 			}
3149 			MAC_CALLBACK_WALKER_DCR(&mcip->mci_tx_notify_cb_info,
3150 			    &mcip->mci_tx_notify_cb_list);
3151 			/*
3152 			 * If the client is not the primary MAC client, then we
3153 			 * need to send the notification to the clients upper
3154 			 * MAC, i.e. mci_upper_mip.
3155 			 */
3156 			mac_tx_notify(mcip->mci_upper_mip != NULL ?
3157 			    mcip->mci_upper_mip : mcip->mci_mip);
3158 		}
3159 		mutex_enter(&mac_srs->srs_lock);
3160 	}
3161 	mac_srs->srs_state &= ~SRS_PROC;
3162 }
3163 
3164 /*
3165  * Given a packet, get the flow_entry that identifies the flow
3166  * to which that packet belongs. The flow_entry will contain
3167  * the transmit function to be used to send the packet. If the
3168  * function returns NULL, the packet should be sent using the
3169  * underlying NIC.
3170  */
3171 static flow_entry_t *
3172 mac_tx_classify(mac_impl_t *mip, mblk_t *mp)
3173 {
3174 	flow_entry_t		*flent = NULL;
3175 	mac_client_impl_t	*mcip;
3176 	int	err;
3177 
3178 	/*
3179 	 * Do classification on the packet.
3180 	 */
3181 	err = mac_flow_lookup(mip->mi_flow_tab, mp, FLOW_OUTBOUND, &flent);
3182 	if (err != 0)
3183 		return (NULL);
3184 
3185 	/*
3186 	 * This flent might just be an additional one on the MAC client,
3187 	 * i.e. for classification purposes (different fdesc), however
3188 	 * the resources, SRS et. al., are in the mci_flent, so if
3189 	 * this isn't the mci_flent, we need to get it.
3190 	 */
3191 	if ((mcip = flent->fe_mcip) != NULL && mcip->mci_flent != flent) {
3192 		FLOW_REFRELE(flent);
3193 		flent = mcip->mci_flent;
3194 		FLOW_TRY_REFHOLD(flent, err);
3195 		if (err != 0)
3196 			return (NULL);
3197 	}
3198 
3199 	return (flent);
3200 }
3201 
3202 /*
3203  * This macro is only meant to be used by mac_tx_send().
3204  */
3205 #define	CHECK_VID_AND_ADD_TAG(mp) {			\
3206 	if (vid_check) {				\
3207 		int err = 0;				\
3208 							\
3209 		MAC_VID_CHECK(src_mcip, (mp), err);	\
3210 		if (err != 0) {				\
3211 			freemsg((mp));			\
3212 			(mp) = next;			\
3213 			oerrors++;			\
3214 			continue;			\
3215 		}					\
3216 	}						\
3217 	if (add_tag) {					\
3218 		(mp) = mac_add_vlan_tag((mp), 0, vid);	\
3219 		if ((mp) == NULL) {			\
3220 			(mp) = next;			\
3221 			oerrors++;			\
3222 			continue;			\
3223 		}					\
3224 	}						\
3225 }
3226 
3227 mblk_t *
3228 mac_tx_send(mac_client_handle_t mch, mac_ring_handle_t ring, mblk_t *mp_chain,
3229     mac_tx_stats_t *stats)
3230 {
3231 	mac_client_impl_t *src_mcip = (mac_client_impl_t *)mch;
3232 	mac_impl_t *mip = src_mcip->mci_mip;
3233 	uint_t obytes = 0, opackets = 0, oerrors = 0;
3234 	mblk_t *mp = NULL, *next;
3235 	boolean_t vid_check, add_tag;
3236 	uint16_t vid = 0;
3237 
3238 	if (mip->mi_nclients > 1) {
3239 		vid_check = MAC_VID_CHECK_NEEDED(src_mcip);
3240 		add_tag = MAC_TAG_NEEDED(src_mcip);
3241 		if (add_tag)
3242 			vid = mac_client_vid(mch);
3243 	} else {
3244 		ASSERT(mip->mi_nclients == 1);
3245 		vid_check = add_tag = B_FALSE;
3246 	}
3247 
3248 	/*
3249 	 * Fastpath: if there's only one client, and there's no
3250 	 * multicast listeners, we simply send the packet down to the
3251 	 * underlying NIC.
3252 	 */
3253 	if (mip->mi_nactiveclients == 1 && mip->mi_promisc_list == NULL)  {
3254 		DTRACE_PROBE2(fastpath,
3255 		    mac_client_impl_t *, src_mcip, mblk_t *, mp_chain);
3256 
3257 		mp = mp_chain;
3258 		while (mp != NULL) {
3259 			next = mp->b_next;
3260 			mp->b_next = NULL;
3261 			opackets++;
3262 			obytes += (mp->b_cont == NULL ? MBLKL(mp) :
3263 			    msgdsize(mp));
3264 
3265 			CHECK_VID_AND_ADD_TAG(mp);
3266 			MAC_TX(mip, ring, mp, src_mcip);
3267 
3268 			/*
3269 			 * If the driver is out of descriptors and does a
3270 			 * partial send it will return a chain of unsent
3271 			 * mblks. Adjust the accounting stats.
3272 			 */
3273 			if (mp != NULL) {
3274 				opackets--;
3275 				obytes -= msgdsize(mp);
3276 				mp->b_next = next;
3277 				break;
3278 			}
3279 			mp = next;
3280 		}
3281 		goto done;
3282 	}
3283 
3284 	/*
3285 	 * No fastpath, we either have more than one MAC client
3286 	 * defined on top of the same MAC, or one or more MAC
3287 	 * client promiscuous callbacks.
3288 	 */
3289 	DTRACE_PROBE3(slowpath, mac_client_impl_t *,
3290 	    src_mcip, int, mip->mi_nclients, mblk_t *, mp_chain);
3291 
3292 	mp = mp_chain;
3293 	while (mp != NULL) {
3294 		flow_entry_t *dst_flow_ent;
3295 		void *flow_cookie;
3296 		size_t	pkt_size;
3297 		mblk_t *mp1;
3298 
3299 		next = mp->b_next;
3300 		mp->b_next = NULL;
3301 		opackets++;
3302 		pkt_size = (mp->b_cont == NULL ? MBLKL(mp) : msgdsize(mp));
3303 		obytes += pkt_size;
3304 		CHECK_VID_AND_ADD_TAG(mp);
3305 
3306 		/*
3307 		 * Check if there are promiscuous mode callbacks defined.
3308 		 */
3309 		if (mip->mi_promisc_list != NULL)
3310 			mac_promisc_dispatch(mip, mp, src_mcip);
3311 
3312 		/*
3313 		 * Find the destination.
3314 		 */
3315 		dst_flow_ent = mac_tx_classify(mip, mp);
3316 
3317 		if (dst_flow_ent != NULL) {
3318 			size_t	hdrsize;
3319 			int	err = 0;
3320 
3321 			if (mip->mi_info.mi_nativemedia == DL_ETHER) {
3322 				struct ether_vlan_header *evhp =
3323 				    (struct ether_vlan_header *)mp->b_rptr;
3324 
3325 				if (ntohs(evhp->ether_tpid) == ETHERTYPE_VLAN)
3326 					hdrsize = sizeof (*evhp);
3327 				else
3328 					hdrsize = sizeof (struct ether_header);
3329 			} else {
3330 				mac_header_info_t	mhi;
3331 
3332 				err = mac_header_info((mac_handle_t)mip,
3333 				    mp, &mhi);
3334 				if (err == 0)
3335 					hdrsize = mhi.mhi_hdrsize;
3336 			}
3337 
3338 			/*
3339 			 * Got a matching flow. It's either another
3340 			 * MAC client, or a broadcast/multicast flow.
3341 			 * Make sure the packet size is within the
3342 			 * allowed size. If not drop the packet and
3343 			 * move to next packet.
3344 			 */
3345 			if (err != 0 ||
3346 			    (pkt_size - hdrsize) > mip->mi_sdu_max) {
3347 				oerrors++;
3348 				DTRACE_PROBE2(loopback__drop, size_t, pkt_size,
3349 				    mblk_t *, mp);
3350 				freemsg(mp);
3351 				mp = next;
3352 				FLOW_REFRELE(dst_flow_ent);
3353 				continue;
3354 			}
3355 			flow_cookie = mac_flow_get_client_cookie(dst_flow_ent);
3356 			if (flow_cookie != NULL) {
3357 				/*
3358 				 * The vnic_bcast_send function expects
3359 				 * to receive the sender MAC client
3360 				 * as value for arg2.
3361 				 */
3362 				mac_bcast_send(flow_cookie, src_mcip, mp,
3363 				    B_TRUE);
3364 			} else {
3365 				/*
3366 				 * loopback the packet to a
3367 				 * local MAC client. We force a context
3368 				 * switch if both source and destination
3369 				 * MAC clients are used by IP, i.e. bypass
3370 				 * is set.
3371 				 */
3372 				boolean_t do_switch;
3373 				mac_client_impl_t *dst_mcip =
3374 				    dst_flow_ent->fe_mcip;
3375 
3376 				do_switch = ((src_mcip->mci_state_flags &
3377 				    dst_mcip->mci_state_flags &
3378 				    MCIS_CLIENT_POLL_CAPABLE) != 0);
3379 
3380 				if ((mp1 = mac_fix_cksum(mp)) != NULL) {
3381 					(dst_flow_ent->fe_cb_fn)(
3382 					    dst_flow_ent->fe_cb_arg1,
3383 					    dst_flow_ent->fe_cb_arg2,
3384 					    mp1, do_switch);
3385 				}
3386 			}
3387 			FLOW_REFRELE(dst_flow_ent);
3388 		} else {
3389 			/*
3390 			 * Unknown destination, send via the underlying
3391 			 * NIC.
3392 			 */
3393 			MAC_TX(mip, ring, mp, src_mcip);
3394 			if (mp != NULL) {
3395 				/*
3396 				 * Adjust for the last packet that
3397 				 * could not be transmitted
3398 				 */
3399 				opackets--;
3400 				obytes -= pkt_size;
3401 				mp->b_next = next;
3402 				break;
3403 			}
3404 		}
3405 		mp = next;
3406 	}
3407 
3408 done:
3409 	src_mcip->mci_stat_obytes += obytes;
3410 	src_mcip->mci_stat_opackets += opackets;
3411 	src_mcip->mci_stat_oerrors += oerrors;
3412 
3413 	if (stats != NULL) {
3414 		stats->ts_opackets = opackets;
3415 		stats->ts_obytes = obytes;
3416 		stats->ts_oerrors = oerrors;
3417 	}
3418 	return (mp);
3419 }
3420 
3421 /*
3422  * mac_tx_srs_ring_present
3423  *
3424  * Returns whether the specified ring is part of the specified SRS.
3425  */
3426 boolean_t
3427 mac_tx_srs_ring_present(mac_soft_ring_set_t *srs, mac_ring_t *tx_ring)
3428 {
3429 	int i;
3430 	mac_soft_ring_t *soft_ring;
3431 
3432 	if (srs->srs_tx.st_arg2 == tx_ring)
3433 		return (B_TRUE);
3434 
3435 	for (i = 0; i < srs->srs_oth_ring_count; i++) {
3436 		soft_ring =  srs->srs_oth_soft_rings[i];
3437 		if (soft_ring->s_ring_tx_arg2 == tx_ring)
3438 			return (B_TRUE);
3439 	}
3440 
3441 	return (B_FALSE);
3442 }
3443 
3444 /*
3445  * mac_tx_srs_wakeup
3446  *
3447  * Called when Tx desc become available. Wakeup the appropriate worker
3448  * thread after resetting the SRS_TX_BLOCKED/S_RING_BLOCK bit in the
3449  * state field.
3450  */
3451 void
3452 mac_tx_srs_wakeup(mac_soft_ring_set_t *mac_srs, mac_ring_handle_t ring)
3453 {
3454 	int i;
3455 	mac_soft_ring_t *sringp;
3456 	mac_srs_tx_t *srs_tx = &mac_srs->srs_tx;
3457 
3458 	mutex_enter(&mac_srs->srs_lock);
3459 	if (TX_SINGLE_RING_MODE(mac_srs)) {
3460 		if (srs_tx->st_arg2 == ring &&
3461 		    mac_srs->srs_state & SRS_TX_BLOCKED) {
3462 			mac_srs->srs_state &= ~SRS_TX_BLOCKED;
3463 			srs_tx->st_unblocked_cnt++;
3464 			cv_signal(&mac_srs->srs_async);
3465 		}
3466 		/*
3467 		 * A wakeup can come before tx_srs_drain() could
3468 		 * grab srs lock and set SRS_TX_BLOCKED. So
3469 		 * always set woken_up flag when we come here.
3470 		 */
3471 		srs_tx->st_woken_up = B_TRUE;
3472 		mutex_exit(&mac_srs->srs_lock);
3473 		return;
3474 	}
3475 
3476 	/* If you are here, it is for FANOUT or BW_FANOUT case */
3477 	ASSERT(TX_MULTI_RING_MODE(mac_srs));
3478 	for (i = 0; i < mac_srs->srs_oth_ring_count; i++) {
3479 		sringp = mac_srs->srs_oth_soft_rings[i];
3480 		mutex_enter(&sringp->s_ring_lock);
3481 		if (sringp->s_ring_tx_arg2 == ring) {
3482 			if (sringp->s_ring_state & S_RING_BLOCK) {
3483 				sringp->s_ring_state &= ~S_RING_BLOCK;
3484 				sringp->s_ring_unblocked_cnt++;
3485 				cv_signal(&sringp->s_ring_async);
3486 			}
3487 			sringp->s_ring_tx_woken_up = B_TRUE;
3488 		}
3489 		mutex_exit(&sringp->s_ring_lock);
3490 	}
3491 	mutex_exit(&mac_srs->srs_lock);
3492 }
3493 
3494 /*
3495  * Once the driver is done draining, send a MAC_NOTE_TX notification to unleash
3496  * the blocked clients again.
3497  */
3498 void
3499 mac_tx_notify(mac_impl_t *mip)
3500 {
3501 	i_mac_notify(mip, MAC_NOTE_TX);
3502 }
3503 
3504 /*
3505  * RX SOFTRING RELATED FUNCTIONS
3506  *
3507  * These functions really belong in mac_soft_ring.c and here for
3508  * a short period.
3509  */
3510 
3511 #define	SOFT_RING_ENQUEUE_CHAIN(ringp, mp, tail, cnt, sz) {	       	\
3512 	/*								\
3513 	 * Enqueue our mblk chain.					\
3514 	 */								\
3515 	ASSERT(MUTEX_HELD(&(ringp)->s_ring_lock));			\
3516 									\
3517 	if ((ringp)->s_ring_last != NULL)				\
3518 		(ringp)->s_ring_last->b_next = (mp);			\
3519 	else								\
3520 		(ringp)->s_ring_first = (mp);				\
3521 	(ringp)->s_ring_last = (tail);					\
3522 	(ringp)->s_ring_count += (cnt);					\
3523 	ASSERT((ringp)->s_ring_count > 0);				\
3524 	if ((ringp)->s_ring_type & ST_RING_BW_CTL) {			\
3525 		(ringp)->s_ring_size += sz;				\
3526 	}								\
3527 }
3528 
3529 /*
3530  * Default entry point to deliver a packet chain to a MAC client.
3531  * If the MAC client has flows, do the classification with these
3532  * flows as well.
3533  */
3534 /* ARGSUSED */
3535 void
3536 mac_rx_deliver(void *arg1, mac_resource_handle_t mrh, mblk_t *mp_chain,
3537     mac_header_info_t *arg3)
3538 {
3539 	mac_client_impl_t *mcip = arg1;
3540 
3541 	if (mcip->mci_nvids == 1 &&
3542 	    !(mcip->mci_state_flags & MCIS_STRIP_DISABLE)) {
3543 		/*
3544 		 * If the client has exactly one VID associated with it
3545 		 * and striping of VLAN header is not disabled,
3546 		 * remove the VLAN tag from the packet before
3547 		 * passing it on to the client's receive callback.
3548 		 * Note that this needs to be done after we dispatch
3549 		 * the packet to the promiscuous listeners of the
3550 		 * client, since they expect to see the whole
3551 		 * frame including the VLAN headers.
3552 		 */
3553 		mp_chain = mac_strip_vlan_tag_chain(mp_chain);
3554 	}
3555 
3556 	mcip->mci_rx_fn(mcip->mci_rx_arg, mrh, mp_chain, B_FALSE);
3557 }
3558 
3559 /*
3560  * mac_rx_soft_ring_process
3561  *
3562  * process a chain for a given soft ring. The number of packets queued
3563  * in the SRS and its associated soft rings (including this one) is
3564  * very small (tracked by srs_poll_pkt_cnt), then allow the entering
3565  * thread (interrupt or poll thread) to do inline processing. This
3566  * helps keep the latency down under low load.
3567  *
3568  * The proc and arg for each mblk is already stored in the mblk in
3569  * appropriate places.
3570  */
3571 /* ARGSUSED */
3572 void
3573 mac_rx_soft_ring_process(mac_client_impl_t *mcip, mac_soft_ring_t *ringp,
3574     mblk_t *mp_chain, mblk_t *tail, int cnt, size_t sz)
3575 {
3576 	mac_direct_rx_t		proc;
3577 	void			*arg1;
3578 	mac_resource_handle_t	arg2;
3579 	mac_soft_ring_set_t	*mac_srs = ringp->s_ring_set;
3580 
3581 	ASSERT(ringp != NULL);
3582 	ASSERT(mp_chain != NULL);
3583 	ASSERT(tail != NULL);
3584 	ASSERT(MUTEX_NOT_HELD(&ringp->s_ring_lock));
3585 
3586 	mutex_enter(&ringp->s_ring_lock);
3587 	ringp->s_ring_total_inpkt += cnt;
3588 	if ((mac_srs->srs_rx.sr_poll_pkt_cnt <= 1) &&
3589 	    !(ringp->s_ring_type & ST_RING_WORKER_ONLY)) {
3590 		/* If on processor or blanking on, then enqueue and return */
3591 		if (ringp->s_ring_state & S_RING_BLANK ||
3592 		    ringp->s_ring_state & S_RING_PROC) {
3593 			SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain, tail, cnt, sz);
3594 			mutex_exit(&ringp->s_ring_lock);
3595 			return;
3596 		}
3597 		proc = ringp->s_ring_rx_func;
3598 		arg1 = ringp->s_ring_rx_arg1;
3599 		arg2 = ringp->s_ring_rx_arg2;
3600 		/*
3601 		 * See if anything is already queued. If we are the
3602 		 * first packet, do inline processing else queue the
3603 		 * packet and do the drain.
3604 		 */
3605 		if (ringp->s_ring_first == NULL) {
3606 			/*
3607 			 * Fast-path, ok to process and nothing queued.
3608 			 */
3609 			ringp->s_ring_run = curthread;
3610 			ringp->s_ring_state |= (S_RING_PROC);
3611 
3612 			mutex_exit(&ringp->s_ring_lock);
3613 
3614 			/*
3615 			 * We are the chain of 1 packet so
3616 			 * go through this fast path.
3617 			 */
3618 			ASSERT(mp_chain->b_next == NULL);
3619 
3620 			(*proc)(arg1, arg2, mp_chain, NULL);
3621 
3622 			ASSERT(MUTEX_NOT_HELD(&ringp->s_ring_lock));
3623 			/*
3624 			 * If we have a soft ring set which is doing
3625 			 * bandwidth control, we need to decrement
3626 			 * srs_size and count so it the SRS can have a
3627 			 * accurate idea of what is the real data
3628 			 * queued between SRS and its soft rings. We
3629 			 * decrement the counters only when the packet
3630 			 * gets processed by both SRS and the soft ring.
3631 			 */
3632 			mutex_enter(&mac_srs->srs_lock);
3633 			MAC_UPDATE_SRS_COUNT_LOCKED(mac_srs, cnt);
3634 			MAC_UPDATE_SRS_SIZE_LOCKED(mac_srs, sz);
3635 			mutex_exit(&mac_srs->srs_lock);
3636 
3637 			mutex_enter(&ringp->s_ring_lock);
3638 			ringp->s_ring_run = NULL;
3639 			ringp->s_ring_state &= ~S_RING_PROC;
3640 			if (ringp->s_ring_state & S_RING_CLIENT_WAIT)
3641 				cv_signal(&ringp->s_ring_client_cv);
3642 
3643 			if ((ringp->s_ring_first == NULL) ||
3644 			    (ringp->s_ring_state & S_RING_BLANK)) {
3645 				/*
3646 				 * We processed inline our packet and
3647 				 * nothing new has arrived or our
3648 				 * receiver doesn't want to receive
3649 				 * any packets. We are done.
3650 				 */
3651 				mutex_exit(&ringp->s_ring_lock);
3652 				return;
3653 			}
3654 		} else {
3655 			SOFT_RING_ENQUEUE_CHAIN(ringp,
3656 			    mp_chain, tail, cnt, sz);
3657 		}
3658 
3659 		/*
3660 		 * We are here because either we couldn't do inline
3661 		 * processing (because something was already
3662 		 * queued), or we had a chain of more than one
3663 		 * packet, or something else arrived after we were
3664 		 * done with inline processing.
3665 		 */
3666 		ASSERT(MUTEX_HELD(&ringp->s_ring_lock));
3667 		ASSERT(ringp->s_ring_first != NULL);
3668 
3669 		ringp->s_ring_drain_func(ringp);
3670 		mutex_exit(&ringp->s_ring_lock);
3671 		return;
3672 	} else {
3673 		/* ST_RING_WORKER_ONLY case */
3674 		SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain, tail, cnt, sz);
3675 		mac_soft_ring_worker_wakeup(ringp);
3676 		mutex_exit(&ringp->s_ring_lock);
3677 	}
3678 }
3679 
3680 /*
3681  * TX SOFTRING RELATED FUNCTIONS
3682  *
3683  * These functions really belong in mac_soft_ring.c and here for
3684  * a short period.
3685  */
3686 
3687 #define	TX_SOFT_RING_ENQUEUE_CHAIN(ringp, mp, tail, cnt, sz) {	       	\
3688 	ASSERT(MUTEX_HELD(&ringp->s_ring_lock));			\
3689 	ringp->s_ring_state |= S_RING_ENQUEUED;				\
3690 	SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain, tail, cnt, sz);	\
3691 }
3692 
3693 /*
3694  * mac_tx_sring_queued
3695  *
3696  * When we are out of transmit descriptors and we already have a
3697  * queue that exceeds hiwat (or the client called us with
3698  * MAC_TX_NO_ENQUEUE or MAC_DROP_ON_NO_DESC flag), return the
3699  * soft ring pointer as the opaque cookie for the client enable
3700  * flow control.
3701  */
3702 static mac_tx_cookie_t
3703 mac_tx_sring_enqueue(mac_soft_ring_t *ringp, mblk_t *mp_chain, uint16_t flag,
3704     mblk_t **ret_mp)
3705 {
3706 	int cnt;
3707 	size_t sz;
3708 	mblk_t *tail;
3709 	mac_soft_ring_set_t *mac_srs = ringp->s_ring_set;
3710 	mac_tx_cookie_t cookie = NULL;
3711 	boolean_t wakeup_worker = B_TRUE;
3712 
3713 	ASSERT(MUTEX_HELD(&ringp->s_ring_lock));
3714 	MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
3715 	if (flag & MAC_DROP_ON_NO_DESC) {
3716 		mac_pkt_drop(NULL, NULL, mp_chain, B_FALSE);
3717 		/* increment freed stats */
3718 		ringp->s_ring_drops += cnt;
3719 		cookie = (mac_tx_cookie_t)ringp;
3720 	} else {
3721 		if (ringp->s_ring_first != NULL)
3722 			wakeup_worker = B_FALSE;
3723 
3724 		if (flag & MAC_TX_NO_ENQUEUE) {
3725 			/*
3726 			 * If QUEUED is not set, queue the packet
3727 			 * and let mac_tx_soft_ring_drain() set
3728 			 * the TX_BLOCKED bit for the reasons
3729 			 * explained above. Otherwise, return the
3730 			 * mblks.
3731 			 */
3732 			if (wakeup_worker) {
3733 				TX_SOFT_RING_ENQUEUE_CHAIN(ringp,
3734 				    mp_chain, tail, cnt, sz);
3735 			} else {
3736 				ringp->s_ring_state |= S_RING_WAKEUP_CLIENT;
3737 				cookie = (mac_tx_cookie_t)ringp;
3738 				*ret_mp = mp_chain;
3739 			}
3740 		} else {
3741 			boolean_t enqueue = B_TRUE;
3742 
3743 			if (ringp->s_ring_count > ringp->s_ring_tx_hiwat) {
3744 				/*
3745 				 * flow-controlled. Store ringp in cookie
3746 				 * so that it can be returned as
3747 				 * mac_tx_cookie_t to client
3748 				 */
3749 				ringp->s_ring_state |= S_RING_TX_HIWAT;
3750 				cookie = (mac_tx_cookie_t)ringp;
3751 				ringp->s_ring_hiwat_cnt++;
3752 				if (ringp->s_ring_count >
3753 				    ringp->s_ring_tx_max_q_cnt) {
3754 					/* increment freed stats */
3755 					ringp->s_ring_drops += cnt;
3756 					/*
3757 					 * b_prev may be set to the fanout hint
3758 					 * hence can't use freemsg directly
3759 					 */
3760 					mac_pkt_drop(NULL, NULL,
3761 					    mp_chain, B_FALSE);
3762 					DTRACE_PROBE1(tx_queued_hiwat,
3763 					    mac_soft_ring_t *, ringp);
3764 					enqueue = B_FALSE;
3765 				}
3766 			}
3767 			if (enqueue) {
3768 				TX_SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain,
3769 				    tail, cnt, sz);
3770 			}
3771 		}
3772 		if (wakeup_worker)
3773 			cv_signal(&ringp->s_ring_async);
3774 	}
3775 	return (cookie);
3776 }
3777 
3778 
3779 /*
3780  * mac_tx_soft_ring_process
3781  *
3782  * This routine is called when fanning out outgoing traffic among
3783  * multipe Tx rings.
3784  * Note that a soft ring is associated with a h/w Tx ring.
3785  */
3786 mac_tx_cookie_t
3787 mac_tx_soft_ring_process(mac_soft_ring_t *ringp, mblk_t *mp_chain,
3788     uint16_t flag, mblk_t **ret_mp)
3789 {
3790 	mac_soft_ring_set_t *mac_srs = ringp->s_ring_set;
3791 	int	cnt;
3792 	size_t	sz;
3793 	mblk_t	*tail;
3794 	mac_tx_cookie_t cookie = NULL;
3795 
3796 	ASSERT(ringp != NULL);
3797 	ASSERT(mp_chain != NULL);
3798 	ASSERT(MUTEX_NOT_HELD(&ringp->s_ring_lock));
3799 	/*
3800 	 * Only two modes can come here; either it can be
3801 	 * SRS_TX_BW_FANOUT or SRS_TX_FANOUT
3802 	 */
3803 	ASSERT(mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
3804 	    mac_srs->srs_tx.st_mode == SRS_TX_BW_FANOUT);
3805 
3806 	if (ringp->s_ring_type & ST_RING_WORKER_ONLY) {
3807 		/* Serialization mode */
3808 
3809 		mutex_enter(&ringp->s_ring_lock);
3810 		if (ringp->s_ring_count > ringp->s_ring_tx_hiwat) {
3811 			cookie = mac_tx_sring_enqueue(ringp, mp_chain,
3812 			    flag, ret_mp);
3813 			mutex_exit(&ringp->s_ring_lock);
3814 			return (cookie);
3815 		}
3816 		MAC_COUNT_CHAIN(mac_srs, mp_chain, tail, cnt, sz);
3817 		TX_SOFT_RING_ENQUEUE_CHAIN(ringp, mp_chain, tail, cnt, sz);
3818 		if (ringp->s_ring_state & (S_RING_BLOCK | S_RING_PROC)) {
3819 			/*
3820 			 * If ring is blocked due to lack of Tx
3821 			 * descs, just return. Worker thread
3822 			 * will get scheduled when Tx desc's
3823 			 * become available.
3824 			 */
3825 			mutex_exit(&ringp->s_ring_lock);
3826 			return (cookie);
3827 		}
3828 		mac_soft_ring_worker_wakeup(ringp);
3829 		mutex_exit(&ringp->s_ring_lock);
3830 		return (cookie);
3831 	} else {
3832 		/* Default fanout mode */
3833 		/*
3834 		 * S_RING_BLOCKED is set when underlying NIC runs
3835 		 * out of Tx descs and messages start getting
3836 		 * queued. It won't get reset until
3837 		 * tx_srs_drain() completely drains out the
3838 		 * messages.
3839 		 */
3840 		boolean_t		is_subflow;
3841 		mac_tx_stats_t		stats;
3842 
3843 		if (ringp->s_ring_state & S_RING_ENQUEUED) {
3844 			/* Tx descs/resources not available */
3845 			mutex_enter(&ringp->s_ring_lock);
3846 			if (ringp->s_ring_state & S_RING_ENQUEUED) {
3847 				cookie = mac_tx_sring_enqueue(ringp, mp_chain,
3848 				    flag, ret_mp);
3849 				mutex_exit(&ringp->s_ring_lock);
3850 				return (cookie);
3851 			}
3852 			/*
3853 			 * While we were computing mblk count, the
3854 			 * flow control condition got relieved.
3855 			 * Continue with the transmission.
3856 			 */
3857 			mutex_exit(&ringp->s_ring_lock);
3858 		}
3859 		is_subflow = ((mac_srs->srs_type & SRST_FLOW) != 0);
3860 
3861 		mp_chain = mac_tx_send(ringp->s_ring_tx_arg1,
3862 		    ringp->s_ring_tx_arg2, mp_chain,
3863 		    (is_subflow ? &stats : NULL));
3864 
3865 		/*
3866 		 * Multiple threads could be here sending packets.
3867 		 * Under such conditions, it is not possible to
3868 		 * automically set S_RING_BLOCKED bit to indicate
3869 		 * out of tx desc condition. To atomically set
3870 		 * this, we queue the returned packet and do
3871 		 * the setting of S_RING_BLOCKED in
3872 		 * mac_tx_soft_ring_drain().
3873 		 */
3874 		if (mp_chain != NULL) {
3875 			mutex_enter(&ringp->s_ring_lock);
3876 			cookie =
3877 			    mac_tx_sring_enqueue(ringp, mp_chain, flag, ret_mp);
3878 			mutex_exit(&ringp->s_ring_lock);
3879 			return (cookie);
3880 		}
3881 		if (is_subflow) {
3882 			FLOW_TX_STATS_UPDATE(mac_srs->srs_flent, &stats);
3883 		}
3884 		return (NULL);
3885 	}
3886 }
3887