xref: /titanic_44/usr/src/uts/common/io/igb/igb_tx.c (revision bdf0047c9427cca40961a023475891c898579c37)
1 /*
2  * CDDL HEADER START
3  *
4  * Copyright(c) 2007-2009 Intel Corporation. All rights reserved.
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include "igb_sw.h"
29 
30 static boolean_t igb_tx(igb_tx_ring_t *, mblk_t *);
31 static int igb_tx_copy(igb_tx_ring_t *, tx_control_block_t *, mblk_t *,
32     uint32_t, boolean_t);
33 static int igb_tx_bind(igb_tx_ring_t *, tx_control_block_t *, mblk_t *,
34     uint32_t);
35 static int igb_tx_fill_ring(igb_tx_ring_t *, link_list_t *, tx_context_t *,
36     size_t);
37 static void igb_save_desc(tx_control_block_t *, uint64_t, size_t);
38 static tx_control_block_t *igb_get_free_list(igb_tx_ring_t *);
39 static int igb_get_tx_context(mblk_t *, tx_context_t *);
40 static boolean_t igb_check_tx_context(igb_tx_ring_t *, tx_context_t *);
41 static void igb_fill_tx_context(struct e1000_adv_tx_context_desc *,
42     tx_context_t *, uint32_t);
43 
44 #ifndef IGB_DEBUG
45 #pragma inline(igb_save_desc)
46 #pragma inline(igb_get_tx_context)
47 #pragma inline(igb_check_tx_context)
48 #pragma inline(igb_fill_tx_context)
49 #endif
50 
51 mblk_t *
52 igb_tx_ring_send(void *arg, mblk_t *mp)
53 {
54 	igb_tx_ring_t *tx_ring = (igb_tx_ring_t *)arg;
55 
56 	ASSERT(tx_ring != NULL);
57 
58 	if ((tx_ring->igb->igb_state & IGB_SUSPENDED) ||
59 	    (tx_ring->igb->igb_state & IGB_ERROR) ||
60 	    !(tx_ring->igb->igb_state & IGB_STARTED)) {
61 		freemsg(mp);
62 		return (NULL);
63 	}
64 
65 	return ((igb_tx(tx_ring, mp)) ? NULL : mp);
66 }
67 
68 /*
69  * igb_tx - Main transmit processing
70  *
71  * Called from igb_m_tx with an mblk ready to transmit. this
72  * routine sets up the transmit descriptors and sends data to
73  * the wire.
74  *
75  * One mblk can consist of several fragments, each fragment
76  * will be processed with different methods based on the size.
77  * For the fragments with size less than the bcopy threshold,
78  * they will be processed by using bcopy; otherwise, they will
79  * be processed by using DMA binding.
80  *
81  * To process the mblk, a tx control block is got from the
82  * free list. One tx control block contains one tx buffer, which
83  * is used to copy mblk fragments' data; and one tx DMA handle,
84  * which is used to bind a mblk fragment with DMA resource.
85  *
86  * Several small mblk fragments can be copied into one tx control
87  * block's buffer, and then the buffer will be transmitted with
88  * one tx descriptor.
89  *
90  * A large fragment only binds with one tx control block's DMA
91  * handle, and it can span several tx descriptors for transmitting.
92  *
93  * So to transmit a packet (mblk), several tx control blocks can
94  * be used. After the processing, those tx control blocks will
95  * be put to the work list.
96  */
97 static boolean_t
98 igb_tx(igb_tx_ring_t *tx_ring, mblk_t *mp)
99 {
100 	igb_t *igb = tx_ring->igb;
101 	tx_type_t current_flag, next_flag;
102 	uint32_t current_len, next_len;
103 	uint32_t desc_total;
104 	size_t mbsize;
105 	int desc_num;
106 	boolean_t copy_done, eop;
107 	mblk_t *current_mp, *next_mp, *nmp;
108 	tx_control_block_t *tcb;
109 	tx_context_t tx_context, *ctx;
110 	link_list_t pending_list;
111 	mblk_t *hdr_new_mp = NULL;
112 	mblk_t *hdr_previous_mp = NULL;
113 	mblk_t *hdr_current_mp = NULL;
114 	uint32_t hdr_frag_len;
115 	uint32_t hdr_len, len;
116 	uint32_t copy_thresh;
117 
118 	copy_thresh = igb->tx_copy_thresh;
119 
120 	/* Get the mblk size */
121 	mbsize = 0;
122 	for (nmp = mp; nmp != NULL; nmp = nmp->b_cont) {
123 		mbsize += MBLKL(nmp);
124 	}
125 
126 	if (igb->tx_hcksum_enable) {
127 		ctx = &tx_context;
128 		/*
129 		 * Retrieve offloading context information from the mblk
130 		 * that will be used to decide whether/how to fill the
131 		 * context descriptor.
132 		 */
133 		if (igb_get_tx_context(mp, ctx) != TX_CXT_SUCCESS) {
134 			freemsg(mp);
135 			return (B_TRUE);
136 		}
137 
138 		if ((ctx->lso_flag &&
139 		    (mbsize > (ctx->mac_hdr_len + IGB_LSO_MAXLEN))) ||
140 		    (!ctx->lso_flag &&
141 		    (mbsize > (igb->max_frame_size - ETHERFCSL)))) {
142 			freemsg(mp);
143 			IGB_DEBUGLOG_0(igb, "igb_tx: packet oversize");
144 			return (B_TRUE);
145 		}
146 	} else {
147 		ctx = NULL;
148 		if (mbsize > (igb->max_frame_size - ETHERFCSL)) {
149 			freemsg(mp);
150 			IGB_DEBUGLOG_0(igb, "igb_tx: packet oversize");
151 			return (B_TRUE);
152 		}
153 	}
154 
155 	/*
156 	 * Check and recycle tx descriptors.
157 	 * The recycle threshold here should be selected carefully
158 	 */
159 	if (tx_ring->tbd_free < igb->tx_recycle_thresh)
160 		tx_ring->tx_recycle(tx_ring);
161 
162 	/*
163 	 * After the recycling, if the tbd_free is less than the
164 	 * tx_overload_threshold, assert overload, return B_FALSE;
165 	 * and we need to re-schedule the tx again.
166 	 */
167 	if (tx_ring->tbd_free < igb->tx_overload_thresh) {
168 		tx_ring->reschedule = B_TRUE;
169 		IGB_DEBUG_STAT(tx_ring->stat_overload);
170 		return (B_FALSE);
171 	}
172 
173 	/*
174 	 * The software should guarantee LSO packet header(MAC+IP+TCP)
175 	 * to be within one descriptor - this is required by h/w.
176 	 * Here will reallocate and refill the header if
177 	 * the headers(MAC+IP+TCP) is physical memory non-contiguous.
178 	 */
179 	if (ctx && ctx->lso_flag) {
180 		hdr_len = ctx->mac_hdr_len + ctx->ip_hdr_len + ctx->l4_hdr_len;
181 		len = MBLKL(mp);
182 		hdr_current_mp = mp;
183 		while (len < hdr_len) {
184 			hdr_previous_mp = hdr_current_mp;
185 			hdr_current_mp = hdr_current_mp->b_cont;
186 			len += MBLKL(hdr_current_mp);
187 		}
188 		/*
189 		 * If the header and the payload are in different mblks,
190 		 * we simply force the header to be copied into pre-allocated
191 		 * page-aligned buffer.
192 		 */
193 		if (len == hdr_len)
194 			goto adjust_threshold;
195 
196 		hdr_frag_len = hdr_len - (len - MBLKL(hdr_current_mp));
197 		/*
198 		 * There are two cases we will reallocate
199 		 * a mblk for the last header fragment.
200 		 * 1. the header is in multiple mblks and
201 		 *    the last fragment shares the same mblk
202 		 *    with the payload
203 		 * 2. the header is in a single mblk shared
204 		 *    with the payload but the header crosses
205 		 *    a page.
206 		 */
207 		if ((hdr_current_mp != mp) ||
208 		    (P2NPHASE((uintptr_t)hdr_current_mp->b_rptr, igb->page_size)
209 		    < hdr_len)) {
210 			/*
211 			 * reallocate the mblk for the last header fragment,
212 			 * expect it to be copied into pre-allocated
213 			 * page-aligned buffer
214 			 */
215 			hdr_new_mp = allocb(hdr_frag_len, NULL);
216 			if (!hdr_new_mp) {
217 				return (B_FALSE);
218 			}
219 
220 			/* link the new header fragment with the other parts */
221 			bcopy(hdr_current_mp->b_rptr,
222 			    hdr_new_mp->b_rptr, hdr_frag_len);
223 			hdr_new_mp->b_wptr = hdr_new_mp->b_rptr + hdr_frag_len;
224 			hdr_new_mp->b_cont = hdr_current_mp;
225 			if (hdr_previous_mp)
226 				hdr_previous_mp->b_cont = hdr_new_mp;
227 			else
228 				mp = hdr_new_mp;
229 			hdr_current_mp->b_rptr += hdr_frag_len;
230 		}
231 adjust_threshold:
232 		/*
233 		 * adjust the bcopy threshhold to guarantee
234 		 * the header to use bcopy way
235 		 */
236 		if (copy_thresh < hdr_len)
237 			copy_thresh = hdr_len;
238 	}
239 
240 	/*
241 	 * The pending_list is a linked list that is used to save
242 	 * the tx control blocks that have packet data processed
243 	 * but have not put the data to the tx descriptor ring.
244 	 * It is used to reduce the lock contention of the tx_lock.
245 	 */
246 	LINK_LIST_INIT(&pending_list);
247 	desc_num = 0;
248 	desc_total = 0;
249 
250 	current_mp = mp;
251 	current_len = MBLKL(current_mp);
252 	/*
253 	 * Decide which method to use for the first fragment
254 	 */
255 	current_flag = (current_len <= copy_thresh) ?
256 	    USE_COPY : USE_DMA;
257 	/*
258 	 * If the mblk includes several contiguous small fragments,
259 	 * they may be copied into one buffer. This flag is used to
260 	 * indicate whether there are pending fragments that need to
261 	 * be copied to the current tx buffer.
262 	 *
263 	 * If this flag is B_TRUE, it indicates that a new tx control
264 	 * block is needed to process the next fragment using either
265 	 * copy or DMA binding.
266 	 *
267 	 * Otherwise, it indicates that the next fragment will be
268 	 * copied to the current tx buffer that is maintained by the
269 	 * current tx control block. No new tx control block is needed.
270 	 */
271 	copy_done = B_TRUE;
272 	while (current_mp) {
273 		next_mp = current_mp->b_cont;
274 		eop = (next_mp == NULL); /* Last fragment of the packet? */
275 		next_len = eop ? 0: MBLKL(next_mp);
276 
277 		/*
278 		 * When the current fragment is an empty fragment, if
279 		 * the next fragment will still be copied to the current
280 		 * tx buffer, we cannot skip this fragment here. Because
281 		 * the copy processing is pending for completion. We have
282 		 * to process this empty fragment in the tx_copy routine.
283 		 *
284 		 * If the copy processing is completed or a DMA binding
285 		 * processing is just completed, we can just skip this
286 		 * empty fragment.
287 		 */
288 		if ((current_len == 0) && (copy_done)) {
289 			current_mp = next_mp;
290 			current_len = next_len;
291 			current_flag = (current_len <= copy_thresh) ?
292 			    USE_COPY : USE_DMA;
293 			continue;
294 		}
295 
296 		if (copy_done) {
297 			/*
298 			 * Get a new tx control block from the free list
299 			 */
300 			tcb = igb_get_free_list(tx_ring);
301 
302 			if (tcb == NULL) {
303 				IGB_DEBUG_STAT(tx_ring->stat_fail_no_tcb);
304 				goto tx_failure;
305 			}
306 
307 			/*
308 			 * Push the tx control block to the pending list
309 			 * to avoid using lock too early
310 			 */
311 			LIST_PUSH_TAIL(&pending_list, &tcb->link);
312 		}
313 
314 		if (current_flag == USE_COPY) {
315 			/*
316 			 * Check whether to use bcopy or DMA binding to process
317 			 * the next fragment, and if using bcopy, whether we
318 			 * need to continue copying the next fragment into the
319 			 * current tx buffer.
320 			 */
321 			ASSERT((tcb->tx_buf.len + current_len) <=
322 			    tcb->tx_buf.size);
323 
324 			if (eop) {
325 				/*
326 				 * This is the last fragment of the packet, so
327 				 * the copy processing will be completed with
328 				 * this fragment.
329 				 */
330 				next_flag = USE_NONE;
331 				copy_done = B_TRUE;
332 			} else if ((tcb->tx_buf.len + current_len + next_len) >
333 			    tcb->tx_buf.size) {
334 				/*
335 				 * If the next fragment is too large to be
336 				 * copied to the current tx buffer, we need
337 				 * to complete the current copy processing.
338 				 */
339 				next_flag = (next_len > copy_thresh) ?
340 				    USE_DMA: USE_COPY;
341 				copy_done = B_TRUE;
342 			} else if (next_len > copy_thresh) {
343 				/*
344 				 * The next fragment needs to be processed with
345 				 * DMA binding. So the copy prcessing will be
346 				 * completed with the current fragment.
347 				 */
348 				next_flag = USE_DMA;
349 				copy_done = B_TRUE;
350 			} else {
351 				/*
352 				 * Continue to copy the next fragment to the
353 				 * current tx buffer.
354 				 */
355 				next_flag = USE_COPY;
356 				copy_done = B_FALSE;
357 			}
358 
359 			desc_num = igb_tx_copy(tx_ring, tcb, current_mp,
360 			    current_len, copy_done);
361 		} else {
362 			/*
363 			 * Check whether to use bcopy or DMA binding to process
364 			 * the next fragment.
365 			 */
366 			next_flag = (next_len > copy_thresh) ?
367 			    USE_DMA: USE_COPY;
368 			ASSERT(copy_done == B_TRUE);
369 
370 			desc_num = igb_tx_bind(tx_ring, tcb, current_mp,
371 			    current_len);
372 		}
373 
374 		if (desc_num > 0)
375 			desc_total += desc_num;
376 		else if (desc_num < 0)
377 			goto tx_failure;
378 
379 		current_mp = next_mp;
380 		current_len = next_len;
381 		current_flag = next_flag;
382 	}
383 
384 	/*
385 	 * Attach the mblk to the last tx control block
386 	 */
387 	ASSERT(tcb);
388 	ASSERT(tcb->mp == NULL);
389 	tcb->mp = mp;
390 
391 	/*
392 	 * Before fill the tx descriptor ring with the data, we need to
393 	 * ensure there are adequate free descriptors for transmit
394 	 * (including one context descriptor).
395 	 */
396 	if (tx_ring->tbd_free < (desc_total + 1)) {
397 		tx_ring->tx_recycle(tx_ring);
398 	}
399 
400 	mutex_enter(&tx_ring->tx_lock);
401 
402 	/*
403 	 * If the number of free tx descriptors is not enough for transmit
404 	 * then return failure.
405 	 *
406 	 * Note: we must put this check under the mutex protection to
407 	 * ensure the correctness when multiple threads access it in
408 	 * parallel.
409 	 */
410 	if (tx_ring->tbd_free < (desc_total + 1)) {
411 		IGB_DEBUG_STAT(tx_ring->stat_fail_no_tbd);
412 		mutex_exit(&tx_ring->tx_lock);
413 		goto tx_failure;
414 	}
415 
416 	desc_num = igb_tx_fill_ring(tx_ring, &pending_list, ctx, mbsize);
417 
418 	ASSERT((desc_num == desc_total) || (desc_num == (desc_total + 1)));
419 
420 	/* Update per-ring tx statistics */
421 	tx_ring->tx_pkts++;
422 	tx_ring->tx_bytes += mbsize;
423 
424 	mutex_exit(&tx_ring->tx_lock);
425 
426 	return (B_TRUE);
427 
428 tx_failure:
429 	/*
430 	 * If new mblk has been allocted for the last header
431 	 * fragment of a LSO packet, we should restore the
432 	 * modified mp.
433 	 */
434 	if (hdr_new_mp) {
435 		hdr_new_mp->b_cont = NULL;
436 		freeb(hdr_new_mp);
437 		hdr_current_mp->b_rptr -= hdr_frag_len;
438 		if (hdr_previous_mp)
439 			hdr_previous_mp->b_cont = hdr_current_mp;
440 		else
441 			mp = hdr_current_mp;
442 	}
443 
444 	/*
445 	 * Discard the mblk and free the used resources
446 	 */
447 	tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
448 	while (tcb) {
449 		tcb->mp = NULL;
450 
451 		igb_free_tcb(tcb);
452 
453 		tcb = (tx_control_block_t *)
454 		    LIST_GET_NEXT(&pending_list, &tcb->link);
455 	}
456 
457 	/*
458 	 * Return the tx control blocks in the pending list to the free list.
459 	 */
460 	igb_put_free_list(tx_ring, &pending_list);
461 
462 	/* Transmit failed, do not drop the mblk, rechedule the transmit */
463 	tx_ring->reschedule = B_TRUE;
464 
465 	return (B_FALSE);
466 }
467 
468 /*
469  * igb_tx_copy
470  *
471  * Copy the mblk fragment to the pre-allocated tx buffer
472  */
473 static int
474 igb_tx_copy(igb_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
475     uint32_t len, boolean_t copy_done)
476 {
477 	dma_buffer_t *tx_buf;
478 	uint32_t desc_num;
479 	_NOTE(ARGUNUSED(tx_ring));
480 
481 	tx_buf = &tcb->tx_buf;
482 
483 	/*
484 	 * Copy the packet data of the mblk fragment into the
485 	 * pre-allocated tx buffer, which is maintained by the
486 	 * tx control block.
487 	 *
488 	 * Several mblk fragments can be copied into one tx buffer.
489 	 * The destination address of the current copied fragment in
490 	 * the tx buffer is next to the end of the previous copied
491 	 * fragment.
492 	 */
493 	if (len > 0) {
494 		bcopy(mp->b_rptr, tx_buf->address + tx_buf->len, len);
495 
496 		tx_buf->len += len;
497 		tcb->frag_num++;
498 	}
499 
500 	desc_num = 0;
501 
502 	/*
503 	 * If it is the last fragment copied to the current tx buffer,
504 	 * in other words, if there's no remaining fragment or the remaining
505 	 * fragment requires a new tx control block to process, we need to
506 	 * complete the current copy processing by syncing up the current
507 	 * DMA buffer and saving the descriptor data.
508 	 */
509 	if (copy_done) {
510 		/*
511 		 * Sync the DMA buffer of the packet data
512 		 */
513 		DMA_SYNC(tx_buf, DDI_DMA_SYNC_FORDEV);
514 
515 		tcb->tx_type = USE_COPY;
516 
517 		/*
518 		 * Save the address and length to the private data structure
519 		 * of the tx control block, which will be used to fill the
520 		 * tx descriptor ring after all the fragments are processed.
521 		 */
522 		igb_save_desc(tcb, tx_buf->dma_address, tx_buf->len);
523 		desc_num++;
524 	}
525 
526 	return (desc_num);
527 }
528 
529 /*
530  * igb_tx_bind
531  *
532  * Bind the mblk fragment with DMA
533  */
534 static int
535 igb_tx_bind(igb_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
536     uint32_t len)
537 {
538 	int status, i;
539 	ddi_dma_cookie_t dma_cookie;
540 	uint_t ncookies;
541 	int desc_num;
542 
543 	/*
544 	 * Use DMA binding to process the mblk fragment
545 	 */
546 	status = ddi_dma_addr_bind_handle(tcb->tx_dma_handle, NULL,
547 	    (caddr_t)mp->b_rptr, len,
548 	    DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_DONTWAIT,
549 	    0, &dma_cookie, &ncookies);
550 
551 	if (status != DDI_DMA_MAPPED) {
552 		IGB_DEBUG_STAT(tx_ring->stat_fail_dma_bind);
553 		return (-1);
554 	}
555 
556 	tcb->frag_num++;
557 	tcb->tx_type = USE_DMA;
558 	/*
559 	 * Each fragment can span several cookies. One cookie will have
560 	 * one tx descriptor to transmit.
561 	 */
562 	desc_num = 0;
563 	for (i = ncookies; i > 0; i--) {
564 		/*
565 		 * Save the address and length to the private data structure
566 		 * of the tx control block, which will be used to fill the
567 		 * tx descriptor ring after all the fragments are processed.
568 		 */
569 		igb_save_desc(tcb,
570 		    dma_cookie.dmac_laddress,
571 		    dma_cookie.dmac_size);
572 
573 		desc_num++;
574 
575 		if (i > 1)
576 			ddi_dma_nextcookie(tcb->tx_dma_handle, &dma_cookie);
577 	}
578 
579 	return (desc_num);
580 }
581 
582 /*
583  * igb_get_tx_context
584  *
585  * Get the tx context information from the mblk
586  */
587 static int
588 igb_get_tx_context(mblk_t *mp, tx_context_t *ctx)
589 {
590 	uint32_t start;
591 	uint32_t flags;
592 	uint32_t lso_flag;
593 	uint32_t mss;
594 	uint32_t len;
595 	uint32_t size;
596 	uint32_t offset;
597 	unsigned char *pos;
598 	ushort_t etype;
599 	uint32_t mac_hdr_len;
600 	uint32_t l4_proto;
601 	uint32_t l4_hdr_len;
602 
603 	ASSERT(mp != NULL);
604 
605 	mac_hcksum_get(mp, &start, NULL, NULL, NULL, &flags);
606 	bzero(ctx, sizeof (tx_context_t));
607 
608 	ctx->hcksum_flags = flags;
609 
610 	if (flags == 0)
611 		return (TX_CXT_SUCCESS);
612 
613 	mac_lso_get(mp, &mss, &lso_flag);
614 	ctx->mss = mss;
615 	ctx->lso_flag = (lso_flag == HW_LSO);
616 
617 	/*
618 	 * LSO relies on tx h/w checksum, so here the packet will be
619 	 * dropped if the h/w checksum flags are not set.
620 	 */
621 	if (ctx->lso_flag) {
622 		if (!((ctx->hcksum_flags & HCK_PARTIALCKSUM) &&
623 		    (ctx->hcksum_flags & HCK_IPV4_HDRCKSUM))) {
624 			IGB_DEBUGLOG_0(NULL, "igb_tx: h/w "
625 			    "checksum flags are not set for LSO");
626 			return (TX_CXT_E_LSO_CSUM);
627 		}
628 	}
629 
630 	etype = 0;
631 	mac_hdr_len = 0;
632 	l4_proto = 0;
633 
634 	/*
635 	 * Firstly get the position of the ether_type/ether_tpid.
636 	 * Here we don't assume the ether (VLAN) header is fully included
637 	 * in one mblk fragment, so we go thourgh the fragments to parse
638 	 * the ether type.
639 	 */
640 	size = len = MBLKL(mp);
641 	offset = offsetof(struct ether_header, ether_type);
642 	while (size <= offset) {
643 		mp = mp->b_cont;
644 		ASSERT(mp != NULL);
645 		len = MBLKL(mp);
646 		size += len;
647 	}
648 	pos = mp->b_rptr + offset + len - size;
649 
650 	etype = ntohs(*(ushort_t *)(uintptr_t)pos);
651 	if (etype == ETHERTYPE_VLAN) {
652 		/*
653 		 * Get the position of the ether_type in VLAN header
654 		 */
655 		offset = offsetof(struct ether_vlan_header, ether_type);
656 		while (size <= offset) {
657 			mp = mp->b_cont;
658 			ASSERT(mp != NULL);
659 			len = MBLKL(mp);
660 			size += len;
661 		}
662 		pos = mp->b_rptr + offset + len - size;
663 
664 		etype = ntohs(*(ushort_t *)(uintptr_t)pos);
665 		mac_hdr_len = sizeof (struct ether_vlan_header);
666 	} else {
667 		mac_hdr_len = sizeof (struct ether_header);
668 	}
669 
670 	/*
671 	 * Here we assume the IP(V6) header is fully included in one
672 	 * mblk fragment.
673 	 */
674 	switch (etype) {
675 	case ETHERTYPE_IP:
676 		offset = mac_hdr_len;
677 		while (size <= offset) {
678 			mp = mp->b_cont;
679 			ASSERT(mp != NULL);
680 			len = MBLKL(mp);
681 			size += len;
682 		}
683 		pos = mp->b_rptr + offset + len - size;
684 
685 		if (ctx->lso_flag) {
686 			*((uint16_t *)(uintptr_t)(pos + offsetof(ipha_t,
687 			    ipha_length))) = 0;
688 
689 			/*
690 			 * To utilize igb LSO, here need to fill
691 			 * the tcp checksum field of the packet with the
692 			 * following pseudo-header checksum:
693 			 * (ip_source_addr, ip_destination_addr, l4_proto)
694 			 * and also need to fill the ip header checksum
695 			 * with zero. Currently the tcp/ip stack has done
696 			 * these.
697 			 */
698 		}
699 
700 		l4_proto = *(uint8_t *)(pos + offsetof(ipha_t, ipha_protocol));
701 		break;
702 	case ETHERTYPE_IPV6:
703 		offset = offsetof(ip6_t, ip6_nxt) + mac_hdr_len;
704 		while (size <= offset) {
705 			mp = mp->b_cont;
706 			ASSERT(mp != NULL);
707 			len = MBLKL(mp);
708 			size += len;
709 		}
710 		pos = mp->b_rptr + offset + len - size;
711 
712 		l4_proto = *(uint8_t *)pos;
713 		break;
714 	default:
715 		/* Unrecoverable error */
716 		IGB_DEBUGLOG_0(NULL, "Ethernet type field error with "
717 		    "tx hcksum flag set");
718 		return (TX_CXT_E_ETHER_TYPE);
719 	}
720 
721 	if (ctx->lso_flag) {
722 		offset = mac_hdr_len + start;
723 		while (size <= offset) {
724 			mp = mp->b_cont;
725 			ASSERT(mp != NULL);
726 			len = MBLKL(mp);
727 			size += len;
728 		}
729 		pos = mp->b_rptr + offset + len - size;
730 
731 		l4_hdr_len = TCP_HDR_LENGTH((tcph_t *)pos);
732 	} else {
733 		/*
734 		 * l4 header length is only required for LSO
735 		 */
736 		l4_hdr_len = 0;
737 	}
738 
739 	ctx->mac_hdr_len = mac_hdr_len;
740 	ctx->ip_hdr_len = start;
741 	ctx->l4_proto = l4_proto;
742 	ctx->l4_hdr_len = l4_hdr_len;
743 
744 	return (TX_CXT_SUCCESS);
745 }
746 
747 /*
748  * igb_check_tx_context
749  *
750  * Check if a new context descriptor is needed
751  */
752 static boolean_t
753 igb_check_tx_context(igb_tx_ring_t *tx_ring, tx_context_t *ctx)
754 {
755 	tx_context_t *last;
756 
757 	if (ctx == NULL)
758 		return (B_FALSE);
759 
760 	/*
761 	 * Compare the context data retrieved from the mblk and the
762 	 * stored context data of the last context descriptor. The data
763 	 * need to be checked are:
764 	 *	hcksum_flags
765 	 *	l4_proto
766 	 *	mss (only check for LSO)
767 	 *	l4_hdr_len (only check for LSO)
768 	 *	ip_hdr_len
769 	 *	mac_hdr_len
770 	 * Either one of the above data is changed, a new context descriptor
771 	 * will be needed.
772 	 */
773 	last = &tx_ring->tx_context;
774 
775 	if (ctx->hcksum_flags != 0) {
776 		if ((ctx->hcksum_flags != last->hcksum_flags) ||
777 		    (ctx->l4_proto != last->l4_proto) ||
778 		    (ctx->lso_flag && ((ctx->mss != last->mss) ||
779 		    (ctx->l4_hdr_len != last->l4_hdr_len))) ||
780 		    (ctx->ip_hdr_len != last->ip_hdr_len) ||
781 		    (ctx->mac_hdr_len != last->mac_hdr_len)) {
782 			return (B_TRUE);
783 		}
784 	}
785 
786 	return (B_FALSE);
787 }
788 
789 /*
790  * igb_fill_tx_context
791  *
792  * Fill the context descriptor with hardware checksum informations
793  */
794 static void
795 igb_fill_tx_context(struct e1000_adv_tx_context_desc *ctx_tbd,
796     tx_context_t *ctx, uint32_t ring_index)
797 {
798 	/*
799 	 * Fill the context descriptor with the checksum
800 	 * context information we've got
801 	 */
802 	ctx_tbd->vlan_macip_lens = ctx->ip_hdr_len;
803 	ctx_tbd->vlan_macip_lens |= ctx->mac_hdr_len <<
804 	    E1000_ADVTXD_MACLEN_SHIFT;
805 
806 	ctx_tbd->type_tucmd_mlhl =
807 	    E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
808 
809 	if (ctx->hcksum_flags & HCK_IPV4_HDRCKSUM)
810 		ctx_tbd->type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
811 
812 	if (ctx->hcksum_flags & HCK_PARTIALCKSUM) {
813 		switch (ctx->l4_proto) {
814 		case IPPROTO_TCP:
815 			ctx_tbd->type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
816 			break;
817 		case IPPROTO_UDP:
818 			/*
819 			 * We don't have to explicitly set:
820 			 *	ctx_tbd->type_tucmd_mlhl |=
821 			 *	    E1000_ADVTXD_TUCMD_L4T_UDP;
822 			 * Because E1000_ADVTXD_TUCMD_L4T_UDP == 0b
823 			 */
824 			break;
825 		default:
826 			/* Unrecoverable error */
827 			IGB_DEBUGLOG_0(NULL, "L4 type error with tx hcksum");
828 			break;
829 		}
830 	}
831 
832 	ctx_tbd->seqnum_seed = 0;
833 	ctx_tbd->mss_l4len_idx = ring_index << 4;
834 	if (ctx->lso_flag) {
835 		ctx_tbd->mss_l4len_idx |=
836 		    (ctx->l4_hdr_len << E1000_ADVTXD_L4LEN_SHIFT) |
837 		    (ctx->mss << E1000_ADVTXD_MSS_SHIFT);
838 	}
839 }
840 
841 /*
842  * igb_tx_fill_ring
843  *
844  * Fill the tx descriptor ring with the data
845  */
846 static int
847 igb_tx_fill_ring(igb_tx_ring_t *tx_ring, link_list_t *pending_list,
848     tx_context_t *ctx, size_t mbsize)
849 {
850 	struct e1000_hw *hw = &tx_ring->igb->hw;
851 	boolean_t load_context;
852 	uint32_t index, tcb_index, desc_num;
853 	union e1000_adv_tx_desc *tbd, *first_tbd;
854 	tx_control_block_t *tcb, *first_tcb;
855 	uint32_t hcksum_flags;
856 	int i;
857 	igb_t *igb = tx_ring->igb;
858 
859 	ASSERT(mutex_owned(&tx_ring->tx_lock));
860 
861 	tbd = NULL;
862 	first_tbd = NULL;
863 	first_tcb = NULL;
864 	desc_num = 0;
865 	hcksum_flags = 0;
866 	load_context = B_FALSE;
867 
868 	/*
869 	 * Get the index of the first tx descriptor that will be filled,
870 	 * and the index of the first work list item that will be attached
871 	 * with the first used tx control block in the pending list.
872 	 * Note: the two indexes are the same.
873 	 */
874 	index = tx_ring->tbd_tail;
875 	tcb_index = tx_ring->tbd_tail;
876 
877 	if (ctx != NULL) {
878 		hcksum_flags = ctx->hcksum_flags;
879 
880 		/*
881 		 * Check if a new context descriptor is needed for this packet
882 		 */
883 		load_context = igb_check_tx_context(tx_ring, ctx);
884 		if (load_context) {
885 			first_tcb = (tx_control_block_t *)
886 			    LIST_GET_HEAD(pending_list);
887 			tbd = &tx_ring->tbd_ring[index];
888 
889 			/*
890 			 * Fill the context descriptor with the
891 			 * hardware checksum offload informations.
892 			 */
893 			igb_fill_tx_context(
894 			    (struct e1000_adv_tx_context_desc *)tbd,
895 			    ctx, tx_ring->index);
896 
897 			index = NEXT_INDEX(index, 1, tx_ring->ring_size);
898 			desc_num++;
899 
900 			/*
901 			 * Store the checksum context data if
902 			 * a new context descriptor is added
903 			 */
904 			tx_ring->tx_context = *ctx;
905 		}
906 	}
907 
908 	first_tbd = &tx_ring->tbd_ring[index];
909 
910 	/*
911 	 * Fill tx data descriptors with the data saved in the pending list.
912 	 * The tx control blocks in the pending list are added to the work list
913 	 * at the same time.
914 	 *
915 	 * The work list is strictly 1:1 corresponding to the descriptor ring.
916 	 * One item of the work list corresponds to one tx descriptor. Because
917 	 * one tx control block can span multiple tx descriptors, the tx
918 	 * control block will be added to the first work list item that
919 	 * corresponds to the first tx descriptor generated from that tx
920 	 * control block.
921 	 */
922 	tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
923 	while (tcb != NULL) {
924 
925 		for (i = 0; i < tcb->desc_num; i++) {
926 			tbd = &tx_ring->tbd_ring[index];
927 
928 			tbd->read.buffer_addr = tcb->desc[i].address;
929 			tbd->read.cmd_type_len = tcb->desc[i].length;
930 
931 			tbd->read.cmd_type_len |= E1000_ADVTXD_DCMD_RS |
932 			    E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_DATA |
933 			    E1000_ADVTXD_DCMD_IFCS;
934 
935 			tbd->read.olinfo_status = 0;
936 
937 			index = NEXT_INDEX(index, 1, tx_ring->ring_size);
938 			desc_num++;
939 		}
940 
941 		if (first_tcb != NULL) {
942 			/*
943 			 * Count the checksum context descriptor for
944 			 * the first tx control block.
945 			 */
946 			first_tcb->desc_num++;
947 			first_tcb = NULL;
948 		}
949 
950 		/*
951 		 * Add the tx control block to the work list
952 		 */
953 		ASSERT(tx_ring->work_list[tcb_index] == NULL);
954 		tx_ring->work_list[tcb_index] = tcb;
955 
956 		tcb_index = index;
957 		tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
958 	}
959 
960 	/*
961 	 * The Insert Ethernet CRC (IFCS) bit and the checksum fields are only
962 	 * valid in the first descriptor of the packet.
963 	 * 82576 also requires the payload length setting even without LSO
964 	 */
965 	ASSERT(first_tbd != NULL);
966 	first_tbd->read.cmd_type_len |= E1000_ADVTXD_DCMD_IFCS;
967 	if (ctx != NULL && ctx->lso_flag) {
968 		first_tbd->read.cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
969 		first_tbd->read.olinfo_status |=
970 		    (mbsize - ctx->mac_hdr_len - ctx->ip_hdr_len
971 		    - ctx->l4_hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT;
972 	} else {
973 		if (hw->mac.type >= e1000_82576) {
974 			first_tbd->read.olinfo_status |=
975 			    (mbsize << E1000_ADVTXD_PAYLEN_SHIFT);
976 		}
977 	}
978 
979 	/* Set hardware checksum bits */
980 	if (hcksum_flags != 0) {
981 		if (hcksum_flags & HCK_IPV4_HDRCKSUM)
982 			first_tbd->read.olinfo_status |=
983 			    E1000_TXD_POPTS_IXSM << 8;
984 		if (hcksum_flags & HCK_PARTIALCKSUM)
985 			first_tbd->read.olinfo_status |=
986 			    E1000_TXD_POPTS_TXSM << 8;
987 		first_tbd->read.olinfo_status |= tx_ring->index << 4;
988 	}
989 
990 	/*
991 	 * The last descriptor of packet needs End Of Packet (EOP),
992 	 * and Report Status (RS) bits set
993 	 */
994 	ASSERT(tbd != NULL);
995 	tbd->read.cmd_type_len |=
996 	    E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_RS;
997 
998 	IGB_DEBUG_STAT(tx_ring->stat_pkt_cnt);
999 
1000 	/*
1001 	 * Sync the DMA buffer of the tx descriptor ring
1002 	 */
1003 	DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORDEV);
1004 
1005 	/*
1006 	 * Update the number of the free tx descriptors.
1007 	 * The mutual exclusion between the transmission and the recycling
1008 	 * (for the tx descriptor ring and the work list) is implemented
1009 	 * with the atomic operation on the number of the free tx descriptors.
1010 	 *
1011 	 * Note: we should always decrement the counter tbd_free before
1012 	 * advancing the hardware TDT pointer to avoid the race condition -
1013 	 * before the counter tbd_free is decremented, the transmit of the
1014 	 * tx descriptors has done and the counter tbd_free is increased by
1015 	 * the tx recycling.
1016 	 */
1017 	i = igb_atomic_reserve(&tx_ring->tbd_free, desc_num);
1018 	ASSERT(i >= 0);
1019 
1020 	tx_ring->tbd_tail = index;
1021 
1022 	/*
1023 	 * Advance the hardware TDT pointer of the tx descriptor ring
1024 	 */
1025 	E1000_WRITE_REG(hw, E1000_TDT(tx_ring->index), index);
1026 
1027 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
1028 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
1029 		atomic_or_32(&igb->igb_state, IGB_ERROR);
1030 	}
1031 
1032 	return (desc_num);
1033 }
1034 
1035 /*
1036  * igb_save_desc
1037  *
1038  * Save the address/length pair to the private array
1039  * of the tx control block. The address/length pairs
1040  * will be filled into the tx descriptor ring later.
1041  */
1042 static void
1043 igb_save_desc(tx_control_block_t *tcb, uint64_t address, size_t length)
1044 {
1045 	sw_desc_t *desc;
1046 
1047 	desc = &tcb->desc[tcb->desc_num];
1048 	desc->address = address;
1049 	desc->length = length;
1050 
1051 	tcb->desc_num++;
1052 }
1053 
1054 /*
1055  * igb_tx_recycle_legacy
1056  *
1057  * Recycle the tx descriptors and tx control blocks.
1058  *
1059  * The work list is traversed to check if the corresponding
1060  * tx descriptors have been transmitted. If so, the resources
1061  * bound to the tx control blocks will be freed, and those
1062  * tx control blocks will be returned to the free list.
1063  */
1064 uint32_t
1065 igb_tx_recycle_legacy(igb_tx_ring_t *tx_ring)
1066 {
1067 	uint32_t index, last_index;
1068 	int desc_num;
1069 	boolean_t desc_done;
1070 	tx_control_block_t *tcb;
1071 	link_list_t pending_list;
1072 	igb_t *igb = tx_ring->igb;
1073 
1074 	/*
1075 	 * The mutex_tryenter() is used to avoid unnecessary
1076 	 * lock contention.
1077 	 */
1078 	if (mutex_tryenter(&tx_ring->recycle_lock) == 0)
1079 		return (0);
1080 
1081 	ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
1082 
1083 	if (tx_ring->tbd_free == tx_ring->ring_size) {
1084 		tx_ring->recycle_fail = 0;
1085 		tx_ring->stall_watchdog = 0;
1086 		mutex_exit(&tx_ring->recycle_lock);
1087 		return (0);
1088 	}
1089 
1090 	/*
1091 	 * Sync the DMA buffer of the tx descriptor ring
1092 	 */
1093 	DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
1094 
1095 	if (igb_check_dma_handle(
1096 	    tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
1097 		mutex_exit(&tx_ring->recycle_lock);
1098 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
1099 		atomic_or_32(&igb->igb_state, IGB_ERROR);
1100 		return (0);
1101 	}
1102 
1103 	LINK_LIST_INIT(&pending_list);
1104 	desc_num = 0;
1105 	index = tx_ring->tbd_head;	/* Index of next tbd/tcb to recycle */
1106 
1107 	tcb = tx_ring->work_list[index];
1108 	ASSERT(tcb != NULL);
1109 
1110 	desc_done = B_TRUE;
1111 	while (desc_done && (tcb != NULL)) {
1112 
1113 		/*
1114 		 * Get the last tx descriptor of the tx control block.
1115 		 * If the last tx descriptor is done, it is done with
1116 		 * all the tx descriptors of the tx control block.
1117 		 * Then the tx control block and all the corresponding
1118 		 * tx descriptors can be recycled.
1119 		 */
1120 		last_index = NEXT_INDEX(index, tcb->desc_num - 1,
1121 		    tx_ring->ring_size);
1122 
1123 		/*
1124 		 * Check if the Descriptor Done bit is set
1125 		 */
1126 		desc_done = tx_ring->tbd_ring[last_index].wb.status &
1127 		    E1000_TXD_STAT_DD;
1128 		if (desc_done) {
1129 			/*
1130 			 * Strip off the tx control block from the work list,
1131 			 * and add it to the pending list.
1132 			 */
1133 			tx_ring->work_list[index] = NULL;
1134 			LIST_PUSH_TAIL(&pending_list, &tcb->link);
1135 
1136 			/*
1137 			 * Count the total number of the tx descriptors recycled
1138 			 */
1139 			desc_num += tcb->desc_num;
1140 
1141 			/*
1142 			 * Advance the index of the tx descriptor ring
1143 			 */
1144 			index = NEXT_INDEX(last_index, 1, tx_ring->ring_size);
1145 
1146 			tcb = tx_ring->work_list[index];
1147 		}
1148 	}
1149 
1150 	/*
1151 	 * If no tx descriptors are recycled, no need to do more processing
1152 	 */
1153 	if (desc_num == 0) {
1154 		tx_ring->recycle_fail++;
1155 		mutex_exit(&tx_ring->recycle_lock);
1156 		return (0);
1157 	}
1158 
1159 	tx_ring->recycle_fail = 0;
1160 	tx_ring->stall_watchdog = 0;
1161 
1162 	/*
1163 	 * Update the head index of the tx descriptor ring
1164 	 */
1165 	tx_ring->tbd_head = index;
1166 
1167 	/*
1168 	 * Update the number of the free tx descriptors with atomic operations
1169 	 */
1170 	atomic_add_32(&tx_ring->tbd_free, desc_num);
1171 
1172 	mutex_exit(&tx_ring->recycle_lock);
1173 
1174 	/*
1175 	 * Free the resources used by the tx control blocks
1176 	 * in the pending list
1177 	 */
1178 	tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
1179 	while (tcb != NULL) {
1180 		/*
1181 		 * Release the resources occupied by the tx control block
1182 		 */
1183 		igb_free_tcb(tcb);
1184 
1185 		tcb = (tx_control_block_t *)
1186 		    LIST_GET_NEXT(&pending_list, &tcb->link);
1187 	}
1188 
1189 	/*
1190 	 * Add the tx control blocks in the pending list to the free list.
1191 	 */
1192 	igb_put_free_list(tx_ring, &pending_list);
1193 
1194 	return (desc_num);
1195 }
1196 
1197 /*
1198  * igb_tx_recycle_head_wb
1199  *
1200  * Check the head write-back, and recycle all the transmitted
1201  * tx descriptors and tx control blocks.
1202  */
1203 uint32_t
1204 igb_tx_recycle_head_wb(igb_tx_ring_t *tx_ring)
1205 {
1206 	uint32_t index;
1207 	uint32_t head_wb;
1208 	int desc_num;
1209 	tx_control_block_t *tcb;
1210 	link_list_t pending_list;
1211 	igb_t *igb = tx_ring->igb;
1212 
1213 	/*
1214 	 * The mutex_tryenter() is used to avoid unnecessary
1215 	 * lock contention.
1216 	 */
1217 	if (mutex_tryenter(&tx_ring->recycle_lock) == 0)
1218 		return (0);
1219 
1220 	ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
1221 
1222 	if (tx_ring->tbd_free == tx_ring->ring_size) {
1223 		tx_ring->recycle_fail = 0;
1224 		tx_ring->stall_watchdog = 0;
1225 		mutex_exit(&tx_ring->recycle_lock);
1226 		return (0);
1227 	}
1228 
1229 	/*
1230 	 * Sync the DMA buffer of the tx descriptor ring
1231 	 *
1232 	 * Note: For head write-back mode, the tx descriptors will not
1233 	 * be written back, but the head write-back value is stored at
1234 	 * the last extra tbd at the end of the DMA area, we still need
1235 	 * to sync the head write-back value for kernel.
1236 	 *
1237 	 * DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
1238 	 */
1239 	(void) ddi_dma_sync(tx_ring->tbd_area.dma_handle,
1240 	    sizeof (union e1000_adv_tx_desc) * tx_ring->ring_size,
1241 	    sizeof (uint32_t),
1242 	    DDI_DMA_SYNC_FORKERNEL);
1243 
1244 	if (igb_check_dma_handle(
1245 	    tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
1246 		mutex_exit(&tx_ring->recycle_lock);
1247 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
1248 		atomic_or_32(&igb->igb_state, IGB_ERROR);
1249 		return (0);
1250 	}
1251 
1252 	LINK_LIST_INIT(&pending_list);
1253 	desc_num = 0;
1254 	index = tx_ring->tbd_head;	/* Next index to clean */
1255 
1256 	/*
1257 	 * Get the value of head write-back
1258 	 */
1259 	head_wb = *tx_ring->tbd_head_wb;
1260 	while (index != head_wb) {
1261 		tcb = tx_ring->work_list[index];
1262 		ASSERT(tcb != NULL);
1263 
1264 		if (OFFSET(index, head_wb, tx_ring->ring_size) <
1265 		    tcb->desc_num) {
1266 			/*
1267 			 * The current tx control block is not
1268 			 * completely transmitted, stop recycling
1269 			 */
1270 			break;
1271 		}
1272 
1273 		/*
1274 		 * Strip off the tx control block from the work list,
1275 		 * and add it to the pending list.
1276 		 */
1277 		tx_ring->work_list[index] = NULL;
1278 		LIST_PUSH_TAIL(&pending_list, &tcb->link);
1279 
1280 		/*
1281 		 * Advance the index of the tx descriptor ring
1282 		 */
1283 		index = NEXT_INDEX(index, tcb->desc_num, tx_ring->ring_size);
1284 
1285 		/*
1286 		 * Count the total number of the tx descriptors recycled
1287 		 */
1288 		desc_num += tcb->desc_num;
1289 	}
1290 
1291 	/*
1292 	 * If no tx descriptors are recycled, no need to do more processing
1293 	 */
1294 	if (desc_num == 0) {
1295 		tx_ring->recycle_fail++;
1296 		mutex_exit(&tx_ring->recycle_lock);
1297 		return (0);
1298 	}
1299 
1300 	tx_ring->recycle_fail = 0;
1301 	tx_ring->stall_watchdog = 0;
1302 
1303 	/*
1304 	 * Update the head index of the tx descriptor ring
1305 	 */
1306 	tx_ring->tbd_head = index;
1307 
1308 	/*
1309 	 * Update the number of the free tx descriptors with atomic operations
1310 	 */
1311 	atomic_add_32(&tx_ring->tbd_free, desc_num);
1312 
1313 	mutex_exit(&tx_ring->recycle_lock);
1314 
1315 	/*
1316 	 * Free the resources used by the tx control blocks
1317 	 * in the pending list
1318 	 */
1319 	tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
1320 	while (tcb) {
1321 		/*
1322 		 * Release the resources occupied by the tx control block
1323 		 */
1324 		igb_free_tcb(tcb);
1325 
1326 		tcb = (tx_control_block_t *)
1327 		    LIST_GET_NEXT(&pending_list, &tcb->link);
1328 	}
1329 
1330 	/*
1331 	 * Add the tx control blocks in the pending list to the free list.
1332 	 */
1333 	igb_put_free_list(tx_ring, &pending_list);
1334 
1335 	return (desc_num);
1336 }
1337 
1338 /*
1339  * igb_free_tcb - free up the tx control block
1340  *
1341  * Free the resources of the tx control block, including
1342  * unbind the previously bound DMA handle, and reset other
1343  * control fields.
1344  */
1345 void
1346 igb_free_tcb(tx_control_block_t *tcb)
1347 {
1348 	switch (tcb->tx_type) {
1349 	case USE_COPY:
1350 		/*
1351 		 * Reset the buffer length that is used for copy
1352 		 */
1353 		tcb->tx_buf.len = 0;
1354 		break;
1355 	case USE_DMA:
1356 		/*
1357 		 * Release the DMA resource that is used for
1358 		 * DMA binding.
1359 		 */
1360 		(void) ddi_dma_unbind_handle(tcb->tx_dma_handle);
1361 		break;
1362 	default:
1363 		break;
1364 	}
1365 
1366 	/*
1367 	 * Free the mblk
1368 	 */
1369 	if (tcb->mp != NULL) {
1370 		freemsg(tcb->mp);
1371 		tcb->mp = NULL;
1372 	}
1373 
1374 	tcb->tx_type = USE_NONE;
1375 	tcb->frag_num = 0;
1376 	tcb->desc_num = 0;
1377 }
1378 
1379 /*
1380  * igb_get_free_list - Get a free tx control block from the free list
1381  *
1382  * The atomic operation on the number of the available tx control block
1383  * in the free list is used to keep this routine mutual exclusive with
1384  * the routine igb_put_check_list.
1385  */
1386 static tx_control_block_t *
1387 igb_get_free_list(igb_tx_ring_t *tx_ring)
1388 {
1389 	tx_control_block_t *tcb;
1390 
1391 	/*
1392 	 * Check and update the number of the free tx control block
1393 	 * in the free list.
1394 	 */
1395 	if (igb_atomic_reserve(&tx_ring->tcb_free, 1) < 0)
1396 		return (NULL);
1397 
1398 	mutex_enter(&tx_ring->tcb_head_lock);
1399 
1400 	tcb = tx_ring->free_list[tx_ring->tcb_head];
1401 	ASSERT(tcb != NULL);
1402 	tx_ring->free_list[tx_ring->tcb_head] = NULL;
1403 	tx_ring->tcb_head = NEXT_INDEX(tx_ring->tcb_head, 1,
1404 	    tx_ring->free_list_size);
1405 
1406 	mutex_exit(&tx_ring->tcb_head_lock);
1407 
1408 	return (tcb);
1409 }
1410 
1411 /*
1412  * igb_put_free_list
1413  *
1414  * Put a list of used tx control blocks back to the free list
1415  *
1416  * A mutex is used here to ensure the serialization. The mutual exclusion
1417  * between igb_get_free_list and igb_put_free_list is implemented with
1418  * the atomic operation on the counter tcb_free.
1419  */
1420 void
1421 igb_put_free_list(igb_tx_ring_t *tx_ring, link_list_t *pending_list)
1422 {
1423 	uint32_t index;
1424 	int tcb_num;
1425 	tx_control_block_t *tcb;
1426 
1427 	mutex_enter(&tx_ring->tcb_tail_lock);
1428 
1429 	index = tx_ring->tcb_tail;
1430 
1431 	tcb_num = 0;
1432 	tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
1433 	while (tcb != NULL) {
1434 		ASSERT(tx_ring->free_list[index] == NULL);
1435 		tx_ring->free_list[index] = tcb;
1436 
1437 		tcb_num++;
1438 
1439 		index = NEXT_INDEX(index, 1, tx_ring->free_list_size);
1440 
1441 		tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
1442 	}
1443 
1444 	tx_ring->tcb_tail = index;
1445 
1446 	/*
1447 	 * Update the number of the free tx control block
1448 	 * in the free list. This operation must be placed
1449 	 * under the protection of the lock.
1450 	 */
1451 	atomic_add_32(&tx_ring->tcb_free, tcb_num);
1452 
1453 	mutex_exit(&tx_ring->tcb_tail_lock);
1454 }
1455