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