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