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